0

Hello I am writing a sort of Menu for an encryption program I am writing. I finished the core of it, and now i want to try to create a GUI for it. Here is the code for the first Menu:

package matrix_with_GUI;

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

public class Main_Menu extends JFrame implements ActionListener{
     private JButton action1 = new JButton ("");
     private JButton action2 = new JButton ("");
     private JPanel pane = new JPanel();
     private JLabel lbl;

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

public Main_Menu(){
    super();
    JPanel pane=new JPanel();
    setTitle ("Start Menu") ;
    JFrame frame = new JFrame("");

    setVisible(true);
    setSize (380, 260) ;
    setLocation (450, 200) ;
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ;

    action1 = new JButton("Start");
    action2 = new JButton("Exit");
    lbl = new JLabel ("Welcome to the Matrix Encoder/Decoder!!!");
    setLayout(new FlowLayout());

    add (lbl) ;
    add(action1, BorderLayout.CENTER);
    action1.addActionListener (this);
    add(action2, BorderLayout.CENTER);
    action2.addActionListener (this);
}
@Override
public void actionPerformed(ActionEvent event) {
    // TODO Auto-generated method stub
    OptionsMenu x = new OptionsMenu();
    if (event.getSource() == action1)
    {
        System.exit(0);
        x.OptionsMenu();
    }
    else if(event.getSource() == action2){
      System.exit(0);
    }

}

} When the Start Button is clicked, the new menu comes up all fine and good, but the first menu stays open. Is there a way to close this first menu AND open the second menu with the first button click? I'm very new to GUI so the simplest solution would be very helpful. On a side note is there a simple way to move the Start button to the next line? Thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
The Progenitor
  • 27
  • 2
  • 2
  • 6
  • 2
    Please check out: [the-use-of-multiple-jframes-good-bad-practice](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) – Hovercraft Full Of Eels Jun 09 '13 at 17:00
  • 1
    `System.exit(0);` Will cause the JVM to terminate. – Maroun Jun 09 '13 at 17:04
  • 1
    1) Don't extend frame or other top level containers. Instead create & use an instance of one. 2) Don't set the size of top level containers. Instead layout the content & call `pack()`. – Andrew Thompson Jun 09 '13 at 17:04
  • `System.exit(0);` causes it to terminate, but it terminates without continuing to the next menu. Unless i am putting this in the wrong place, this will not work. – The Progenitor Jun 09 '13 at 17:10

2 Answers2

2

You have 2 options: you can use a Window Listener, or you can use the dispose() method. To do the dispose() just type

* This is better to be used with subframes and 2nd level windows.*

this.dispose();

or check this link for using the window listener

Closing JFrame

Community
  • 1
  • 1
user2277872
  • 2,963
  • 1
  • 21
  • 22
1

In order to close the Main_Menu, you can just call its dispose method:

this.dispose();

instead of calling System.exit(0), which will terminate the JVM altogether.

gkalpak
  • 47,844
  • 8
  • 105
  • 118