0

您好,我正在为我正在编写的加密程序编写一种菜单。我完成了它的核心,现在我想尝试为它创建一个 GUI。这是第一个菜单的代码:

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);
    }

}

} 当点击 Start Button 时,新的菜单出现了,一切都很好,但是第一个菜单保持打开状态。有没有办法关闭第一个菜单并通过第一个按钮单击打开第二个菜单?我对 GUI 很陌生,所以最简单的解决方案会很有帮助。附带说明一下,是否有一种简单的方法可以将“开始”按钮移动到下一行?谢谢

4

2 回答 2

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

于 2013-06-09T17:10:40.103 回答
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.

于 2013-06-09T17:08:09.320 回答