Advertisement
Guest User

Untitled

a guest
Oct 7th, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5.  
  6. @SuppressWarnings("serial")
  7. public class RenderTest extends JPanel{
  8.     private JFrame window;
  9.     private FlowLayout topLevelLayout;
  10.     private Slot[] slots;
  11.  
  12.     public static void main(String[] args) {
  13.         RenderTest instance = new RenderTest();
  14.         instance.init();
  15.     }
  16.  
  17.     private void init(){
  18.         window = new JFrame("Render Test");
  19.        
  20.         topLevelLayout = new FlowLayout();
  21.         window.setLayout(topLevelLayout);
  22.         window.setResizable(true);
  23.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.         window.setLocationRelativeTo(null);
  25.        
  26.         slots = new Slot[]{new Slot(0), new Slot(2)};
  27.        
  28.         window.add(slots[0]);
  29.         window.add(slots[1]);
  30.        
  31.         window.pack();
  32.         window.setVisible(true);
  33.     }
  34.    
  35.     private class Slot extends JPanel{
  36.         CardLayout cardLayout;
  37.         AddButton add;
  38.         RemoveButton remove;
  39.         public Slot(int index){
  40.             this.add = new AddButton(index);
  41.             this.remove = new RemoveButton(index);
  42.             this.cardLayout = new CardLayout();
  43.             this.setLayout(cardLayout);
  44.             this.cardLayout.addLayoutComponent(add, "add");
  45.             this.cardLayout.addLayoutComponent(remove, "show");
  46.             topLevelLayout.addLayoutComponent("card"+index, this);
  47.         }
  48.         private CardLayout getCardLayout(){
  49.             return this.cardLayout;
  50.         }
  51.     }
  52.    
  53.     private class AddButton extends JPanel{
  54.         private AddButton(int index){
  55.             JButton addButton = new JButton("+");
  56.            
  57.             addButton.setVerticalTextPosition(AbstractButton.CENTER);
  58.             addButton.setHorizontalTextPosition(AbstractButton.CENTER);
  59.             addButton.setActionCommand("add"+index);
  60.             addButton.addActionListener(new Button());
  61.            
  62.             this.add(addButton);
  63.         }
  64.     }
  65.    
  66.     private class RemoveButton extends JPanel{
  67.         private RemoveButton(int index){
  68.             JButton removeButton = new JButton("-");
  69.            
  70.             removeButton.setVerticalTextPosition(AbstractButton.CENTER);
  71.             removeButton.setHorizontalTextPosition(AbstractButton.CENTER);
  72.             removeButton.setActionCommand("remove"+index);
  73.             removeButton.addActionListener(new Button());
  74.            
  75.             this.add(removeButton);
  76.         }
  77.     }
  78.    
  79.     class Button implements ActionListener{
  80.         public void actionPerformed(ActionEvent e) {
  81.             System.out.println(e.getActionCommand());
  82.            
  83.             if (e.getActionCommand().equals("add0")){
  84.                 slots[0].getCardLayout().show(getParent(), "show");
  85.             }
  86.             if (e.getActionCommand().equals("add1")){
  87.                 slots[1].getCardLayout().show(getParent(), "show");
  88.  
  89.             }
  90.             if (e.getActionCommand().equals("remove0")){
  91.                 slots[0].getCardLayout().show(getParent(), "hide");
  92.  
  93.             }
  94.             if (e.getActionCommand().equals("remove1")){
  95.                 slots[1].getCardLayout().show(getParent(), "hide");
  96.  
  97.             }
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement