0

The button "closebutton" does not respond. I'm very new to java and I know the error has something to do with actionpreformed in the nested if. I've set up a test to see if words will print the the console when closebutton is pressed. They don't. Any help is appreciated

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class myGuiTester implements ActionListener
{
    JPanel pane = new JPanel();
    JPanel mainPanel = new JPanel();
    JFrame MenuFrame = new JFrame("Main Menu");
    JFrame Instructions = new JFrame("Instructions");
    JFrame game = new JFrame("Game");
    JPanel gamepan = new JPanel();
    JFrame inst = new JFrame("Instructions");
    JPanel instpan = new JPanel(new FlowLayout());
    JButton playButton = new JButton("Play");
    JButton instButton = new JButton("Instructions");
    JButton exitButton = new JButton("Exit");
    JButton closebutton = new JButton("Close");

    public static void main(String[] args)
    {
        new myGuiTester ();
    }

    public myGuiTester ()
    {
        MenuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        inst.setVisible(false);
        game.setVisible(false);
        JLabel title = new JLabel("    Slot Machine");
        JLabel blank = new JLabel("");
        JLabel blank1 = new JLabel("");
        JLabel blank2 = new JLabel("");
        JLabel blank3 = new JLabel("");
        playButton.addActionListener(this);
        instButton.addActionListener(this);
        JLabel game1 = new JLabel("Test");
        JLabel inst1 = new JLabel();
        exitButton.addActionListener(this);
        closebutton.addActionListener(this);
        pane.setLayout(new GridLayout(9,1));
        pane.add(blank);
        pane.add(title);
        pane.add(blank1);
        pane.add(playButton);
        pane.add(blank2);
        pane.add(instButton);
        pane.add(blank3);
        pane.add(exitButton);
        mainPanel.add(pane);

        MenuFrame.setContentPane(mainPanel);
        MenuFrame.setSize(500,400);
        MenuFrame.setVisible(true);

        //Game Panel
        game.setContentPane(gamepan);
        game.setSize(500,400);
        game1.setText("Game goes here");
        gamepan.add(game1);

        //Instructions Panel
        inst.setContentPane(instpan);
        inst.setSize(500,300);
        inst1.setText("Instructions go here.");
        instpan.add(inst1);
        instpan.add(closebutton);

    }

//ActionListener
public void actionPerformed (ActionEvent e)
    {
        if (e.getActionCommand().equals("Play"))
        {
            game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            game.setVisible(true);
            MenuFrame.setVisible(false);
            System.out.print("test play");
        }
        else if (e.getActionCommand().equals("Instructions"))
        {
            inst.setVisible(true);

                      //suspected error
            if (e.getActionCommand().equals("Close"))
            {
                System.out.print("Test inst");
                inst.setVisible(false);
            }
        }
        else if (e.getActionCommand().equals("Exit"))
            System.exit(0);
    }
}
David
  • 2,987
  • 1
  • 29
  • 35
  • 1
    Use 1 JFrame and the rest are JDialogs see this [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) – vels4j May 03 '14 at 17:46

2 Answers2

0

You must handle "Close" in the elseif- statement but not inside "Instructions. I should be its own block like:

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Play")) {
        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.setVisible(true);
        MenuFrame.setVisible(false);
        System.out.print("test play");
    } else if (e.getActionCommand().equals("Instructions")) {
        inst.setVisible(true);

        // suspected error
    }else if (e.getActionCommand().equals("Close")) {
            System.out.print("Test inst");
            inst.setVisible(false);

    } else if (e.getActionCommand().equals("Exit"))
        System.exit(0);
}
pL4Gu33
  • 2,045
  • 16
  • 38
  • That you so much! This worked. Ive been struggling with this problem for about 3 days straight and it was impeding my ability to work. Thanks again – user3599664 May 03 '14 at 18:15
0

When you check which button is called you are comparing the ActionCommand of the Event with the label of the button, which are not the same thing :

e.getActionCommand().equals("Close")

What you should do is checking if the button IS the source of the event

e.getSource() == closeButton
kgautron
  • 7,915
  • 9
  • 39
  • 60