Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- @SuppressWarnings("serial")
- public class RenderTest extends JPanel{
- private JFrame window;
- private FlowLayout topLevelLayout;
- private Slot[] slots;
- public static void main(String[] args) {
- RenderTest instance = new RenderTest();
- instance.init();
- }
- private void init(){
- window = new JFrame("Render Test");
- topLevelLayout = new FlowLayout();
- window.setLayout(topLevelLayout);
- window.setResizable(true);
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setLocationRelativeTo(null);
- slots = new Slot[]{new Slot(0), new Slot(2)};
- window.add(slots[0]);
- window.add(slots[1]);
- window.pack();
- window.setVisible(true);
- }
- private class Slot extends JPanel{
- CardLayout cardLayout;
- AddButton add;
- RemoveButton remove;
- public Slot(int index){
- this.add = new AddButton(index);
- this.remove = new RemoveButton(index);
- this.cardLayout = new CardLayout();
- this.setLayout(cardLayout);
- this.cardLayout.addLayoutComponent(add, "add");
- this.cardLayout.addLayoutComponent(remove, "show");
- topLevelLayout.addLayoutComponent("card"+index, this);
- }
- private CardLayout getCardLayout(){
- return this.cardLayout;
- }
- }
- private class AddButton extends JPanel{
- private AddButton(int index){
- JButton addButton = new JButton("+");
- addButton.setVerticalTextPosition(AbstractButton.CENTER);
- addButton.setHorizontalTextPosition(AbstractButton.CENTER);
- addButton.setActionCommand("add"+index);
- addButton.addActionListener(new Button());
- this.add(addButton);
- }
- }
- private class RemoveButton extends JPanel{
- private RemoveButton(int index){
- JButton removeButton = new JButton("-");
- removeButton.setVerticalTextPosition(AbstractButton.CENTER);
- removeButton.setHorizontalTextPosition(AbstractButton.CENTER);
- removeButton.setActionCommand("remove"+index);
- removeButton.addActionListener(new Button());
- this.add(removeButton);
- }
- }
- class Button implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- System.out.println(e.getActionCommand());
- if (e.getActionCommand().equals("add0")){
- slots[0].getCardLayout().show(getParent(), "show");
- }
- if (e.getActionCommand().equals("add1")){
- slots[1].getCardLayout().show(getParent(), "show");
- }
- if (e.getActionCommand().equals("remove0")){
- slots[0].getCardLayout().show(getParent(), "hide");
- }
- if (e.getActionCommand().equals("remove1")){
- slots[1].getCardLayout().show(getParent(), "hide");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement