0

Okay, so when I press the JButton menuselect1, I want it to create 4 new objecs, attack1 2 3 and 4, and then add them to the JPanel fightmenu. This is my code so far, it's a mini pokemon game.

First I create all my objects, and then I set the sizes and adds them to the different JPanels

public class MainFrame extends JFrame {

    JPanel mainwindow = new JPanel();
    JPanel bottom = new JPanel();
    JPanel combat = new JPanel();
    JPanel selectionmenu = new JPanel();
    JPanel fightmenu = new JPanel();
    JButton menuselect1 = new JButton("Fight");
    JButton menuselect2 = new JButton("Minimons");
    JButton menuselect3 = new JButton("Bag");
    JButton menuselect4 = new JButton("Run");
    JButton attack1 = new JButton("Tackle");
    JButton attack2 = new JButton("Lightningbolt");
    JButton attack3 = new JButton("Thunder-Shock");
    JButton attack4 = new JButton("Hyper-Beam");

    JButton poke1 = new JButton("Ekans");
    JButton poke2 = new JButton("Pikachu");
    public static void main(String[] args){
        new MainFrame();
    }
    public MainFrame(){
        super("MiniMon");
        setSize(640,640);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(mainwindow);

        // SIZES
        combat.setPreferredSize(new Dimension(640,452));
        bottom.setPreferredSize(new Dimension(640,160));
        selectionmenu.setPreferredSize(new Dimension(320,160));
        fightmenu.setPreferredSize(new Dimension(320,160));

        mainwindow.setLayout(new BorderLayout());
        mainwindow.add(combat, BorderLayout.NORTH);
        mainwindow.add(bottom, BorderLayout.SOUTH);

        combat.setLayout(new BorderLayout());
        combat.add(poke1, BorderLayout.NORTH);
        combat.add(poke2, BorderLayout.SOUTH);

        bottom.setLayout(new BorderLayout());
        bottom.add(selectionmenu, BorderLayout.EAST);
        bottom.add(fightmenu, BorderLayout.WEST);

        selectionmenu.setLayout(new GridLayout(2,2));
        selectionmenu.add(menuselect1);
        selectionmenu.add(menuselect2); 
        selectionmenu.add(menuselect3);
        selectionmenu.add(menuselect4);

        fightmenu.setLayout(new GridLayout(2,2));

        setVisible(true);
    }
}

I set up my fightmenu to use a 2x2 gridlayout, so I just need to add the 4 objects whenever I press the JButton menuselect1. I'm not really sure how to go about this. I know I should add an eventlistener, but when I tried, it did nothing at all. I tried doing this:

fightmenu.setLayout(new GridLayout(2,2));
        menuselect1.addActionListener(new ActionListener() { 
              public void actionPerformed(ActionEvent e) { 
                fightmenupress();
              }
            private void fightmenupress() {
                fightmenu.add(attack1);
                fightmenu.add(attack2);
                fightmenu.add(attack3);
                fightmenu.add(attack4);
            } 
            } );

But it just did nothing.

2 Answers2

2

When you add (or remove) components to a visible GUI, the basic code is:

panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // to repaint all the components on the panel
camickr
  • 321,443
  • 19
  • 166
  • 288
0

I added revalidate and repaint, and it worked!

private void fightmenupress() {
                fightmenu.add(attack1);
                fightmenu.add(attack2);
                fightmenu.add(attack3);
                fightmenu.add(attack4);
                fightmenu.revalidate();
                fightmenu.repaint();
            } 
            } );