有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

具有多个JFrames的swing Java应用程序不会在关闭所有JFrames时终止

我正在为一个竞赛创建一个独立的数据管理程序(所以我不会发布所有的代码,只发布相关的东西)——但是,我现在正在EclipseNeon中测试它,因为它还没有完成。此外,我还使用了WindowBuilder,如果这很重要的话(我认为不应该)

我有一个JFrame(称之为“欢迎”),它在应用程序首次启动时打开。在“welcome”中,我有一个JButton,它打开一个不同的JFrame(称之为“emp”)并关闭“welcome”(通过frame.dispose()如果退出“欢迎”(默认右上角为X),则程序终止。但是,如果我单击jb按钮打开“emp”,然后以相同的方式关闭“emp”,则程序不会终止。我的问题是,当“emp”退出时,如何使程序终止

第一个JFrame(“欢迎”)的代码:

public class Welcome extends JFrame implements ActionListener {

    private JFrame frame;

    // Launch the application.
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Welcome window = new Welcome();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // Create the application.
    public Welcome() {
        initialize();
        frame.pack();
    }

    //Variable declaration.
    //A bunch of variables that aren't important
    //End variable declaration.

    // Initialize the contents of the frame.
    private void initialize() {
        //TODO comment this out better
        //Create the JFrame and set all of its stuff.
        frame = new JFrame();
        frame.setBackground(Color.BLACK);
        frame.getContentPane().setBackground(lightGrey);
        frame.setIconImage(logo.getImage());
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SpringLayout springLayout = new SpringLayout();
        frame.getContentPane().setLayout(springLayout);     

        //Add a bunch of components that also aren't important
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getActionCommand().equals("employees")) {
            frame.dispose();
            Employees emp  = new Employees();
            emp.setVisible(true);
        }
    }
}

以及我的第二个JFrame(“emp”)代码:

public class Employees extends JFrame {

    private JFrame frame2;

    /**
     * Create the application.
     */
    public Employees() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame2 = new JFrame();
        frame2.setExtendedState(MAXIMIZED_BOTH);
        frame2.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

它们都设置为在关闭时退出,但只有在我退出“欢迎”,而不是“emp”时才有效。如果有人有任何想法/解决方案,我洗耳恭听


共 (0) 个答案