-1

So I just started with JFrame and JMenuBar, and I tried to do this:

main file(the window class is my JFrame class):

public class main {
public static void main(String avg[]) {
    window win = new window();
    win.setVisible(true);
}
}

my menu class(the Startwin class is another JFrame class):

public menu() {
JMenu main = new JMenu("File");                        

JMenuItem start = new JMenuItem("ReStart",KeyEvent.VK_R);      
    start.setActionCommand("press OK to continue");
    start.addActionListener(this);

    JMenuItem save = new JMenuItem("Save",KeyEvent.VK_S);
    save.setActionCommand("saved");
    save.addActionListener(this); 
.
.
.
  main.add(start);                                           
  main.add(save);
.
.
.
.
  @Override
public void actionPerformed(ActionEvent e) {
window w = new window();
Startwin a = new Startwin();
if (e.getActionCommand()=="press OK to continue") {
    a.setVisible(true);
    w.setVisible(false);
}
}
}

So when I ran it, the first JFrame window didn't close. Any suggestions?

oh and by the way, sorry if the staff I wrote is unreadable...

mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

2

Three things...

First...

window w = new window();
Startwin a = new Startwin();

Means you've just created brand new instance of these classes, they have no relationship to any other instances of these classes you have previously created...

Second...

e.getActionCommand()=="press OK to continue"

Is not how String comparison is done in Java, this is comparing two different memory references, which means they are very unlikely to ever equal, instead, you should consider using something like...

"press OK to continue".equals(e.getActionCommand())

or

"press OK to continue".equalsIgnoreCase(e.getActionCommand())

If you want a case insensitive match.

Third...

Take a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? and consider using a CardLayout instead...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366