系统退出而不是JFrame关闭

时间:2016-08-27 07:02:40

标签: java jframe

private void close () {

WindowEvent winClosing;
winClosing = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosing); 
}

我试图在上面提到的关闭功能的帮助下关闭之前的Jframe,将此代码放在附图中突出显示的后退按钮

    this.close();
    MainMenu obj = new MainMenu();
    obj.setVisible(true);

问题是上面的代码关闭了我的整个应用程序而不是关闭现在激活的框架

enter image description here

1 个答案:

答案 0 :(得分:0)

以下是您尝试做的(我认为)的快速演示。
请注意:
一个。我认为这不是实现此功能的正确方法。考虑使用JDialog或内部框架作为辅助窗口。
湾将其视为MCVE的示例。事实上,使用一个辅助框架而不是两个辅助框架来演示问题要好得多。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MainWindow extends JFrame  {

    private JFrame frame1, frame2;

    public MainWindow() {

        super("Main");

        //make 2 other frames
        frame1 = new Frame1(this);
        frame2 = new Frame2(this);

        setSize(400, 300);

        //add listener to quit if when window closes
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent arg0) {

                System.exit(0);
            }
        });

        //add buttn top open one frame
        JButton btnFrame1 = new JButton("Frame 1");
        btnFrame1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

                //make frame 1 visible
                frame1.setVisible(true);
                //make this invisible
                setVisible(false);
            }
        });
        getContentPane().add(btnFrame1, BorderLayout.NORTH);

        //add button to open a second frame
        JButton btnFrame2 = new JButton("Frame 2");
        btnFrame2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //make frame 1 visible
                frame2.setVisible(true);
                //make this invisible
                setVisible(false);
            }
        });
        getContentPane().add(btnFrame2, BorderLayout.SOUTH);

        //make main frame visible
        setVisible(true);
    }

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

    public void showMain() {

        //make this one invisible
        frame1.setVisible(false);
        frame2.setVisible(false);

        //make main window visible
        setVisible(true);
    }
}


class Frame1 extends JFrame  {

    public Frame1(MainWindow mainWindow) {

        super("Frame 1");
        setSize(300, 200);

        //add listener to show main this window closes
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent arg0) {

                mainWindow.showMain();
            }
        });

        //add btn to hide this window and show main
        JButton btnFrame1 = new JButton("Back");
        btnFrame1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                mainWindow.showMain();
            }
        });
        getContentPane().add(btnFrame1, BorderLayout.SOUTH);
        getContentPane().setBackground(Color.CYAN);

        //make it invisible
        setVisible(false);
    }
}

class Frame2 extends JFrame  {

    public Frame2(MainWindow mainWindow) {

        super("Frame 2");
        setSize(300, 200);

        //add listener to show main this window closes
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent arg0) {

                mainWindow.showMain();
            }
        });
        JButton btnFrame1 = new JButton("Back");
        btnFrame1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                mainWindow.showMain();
            }
        });

        //add btn to hide this window and show main
        JButton btnMain = new JButton("Back");
        btnMain.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                mainWindow.showMain();
            }
        });
        getContentPane().add(btnMain, BorderLayout.SOUTH);
        getContentPane().setBackground(Color.YELLOW);

        //make it invisible
        setVisible(false);
    }
}

根据需要,不要犹豫要求澄清。