-1

I've done a lot of research and tried a lot of things but my aim is to do this: I have three buttons at the moment. For button 1 - the Play button, I want to click it and the result is to remove ALL buttons on the screen.

Here is my code at the moment

public class Menu extends JFrame implements ActionListener {

//Variables here

public void createUI() {
    frame = new JFrame();

    frame.setTitle("Maze Game");
    frame.setSize(500, 500);

    frame.setVisible(true);
    panel.setLayout(new GridLayout(3, 1, 0, 19));
    panel.setVisible(true);
    JButton b = new JButton("Play");
    b.setSize(20, 10);
    b.setBackground(new Color(59, 89, 182));
    b.setForeground(Color.WHITE);
    b.setFocusPainted(false);
    b.setFont(new Font("Tahoma", Font.BOLD, 12));
    panel.add(b);

    JButton b0 = new JButton("Help");
    b0.setBackground(new Color(59, 89, 182));
    b0.setForeground(Color.WHITE);
    b0.setFocusPainted(false);
    b0.setFont(new Font("Tahoma", Font.BOLD, 12));
    b0.addActionListener(this);
    panel.add(b0);

    JButton b1 = new JButton("Quit");
    b1.setBackground(new Color(59, 89, 182));
    b1.setForeground(Color.WHITE);
    b1.setFocusPainted(false);
    b1.setFont(new Font("Tahoma", Font.BOLD, 12));
    b1.addActionListener(this);
    panel.add(b1);

    frame.getContentPane().add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

Now my problem is this:

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Quit")) {
        frame.dispose();
    } else if (e.getActionCommand().equals("Help")) {
        JFrame j1 = new JFrame();
        j1.setSize(200, 200);
        j1.setVisible(true);
    } else if (e.getActionCommand().equals("Play")) {
        //panel.removeAll();
        //revalidate(); 
        //repaint();
        //frame.getContentPane().remove(panel);

    }
}

I want it so when I click the "Play" button it removes the button as well as the Help button and Quit button.

I've tried the commented bits of code but with no success. Hopefully I've made things clear. To reiterate, I want to remove all the buttons (basically a blank window) after I click play.

Bobby
  • 213
  • 2
  • 7
  • Yes I understand that it is a duplicate but I have tried those things mentioned. Unless I am missing something. – Bobby May 24 '14 at 03:44
  • Did you actually add the ActionListener to the "Play" button? (based on the code you posted the answer is, no) Also, the standard code for adding/removing components from a panel is: `panel.removeAll(); panel.revalidate(); panel.repaint();` – camickr May 24 '14 at 03:49
  • *"I have tried those things mentioned"* Nowhere in your code is there mention of card layout, which is the correct way to approach this type of problem. – Andrew Thompson May 24 '14 at 03:51

1 Answers1

-1

How about adding the panel to the frame first before putting stuff in it ? please disregard this as an answer.

Dexclipse
  • 23
  • 4