如何在不结束JVM的情况下关闭Java GUI

时间:2015-03-06 21:49:39

标签: java swing user-interface jframe

并且没有... system.exit(0)不是我想要的。

我有一个带有5个按钮的MainPage GUI,单击4个按钮可以显示不同的GUI,一个打开聊天服务器,一个打开文件对话框,ETC.但是,如果我使用system.exit(0);关闭这个新的GUI它也关闭MainPage GUI,这不是我想要的。我已经研究过this.dispose();但是我不确定如何使用它。

    exit = new JButton("Exit");
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Runtime.getRuntime().exit(0); // this is the same thing as system.exit(0);
        }
    });
    exit.setBounds(BUTTON_INDENT, 4 * BUTTON_HEIGHT + 5 * BUTTON_INDENT,
            BUTTON_WIDTH, BUTTON_HEIGHT);

关键是要关闭最大化的GUI而不是最小化的GUI。

编辑:

class MyClient implements ActionListener {
Socket s;
DataInputStream dis;
DataOutputStream dos;

JButton sendButton, logoutButton, loginButton, exitButton;
JFrame chatWindow;
JTextArea txtBroadcast;
JTextArea txtMessage;
JList usersList;

// ////////////////////////
public void displayGUI() {
    chatWindow = new JFrame();
    txtBroadcast = new JTextArea(5, 30);
    txtBroadcast.setEditable(false);
    txtMessage = new JTextArea(2, 20);
    usersList = new JList();

    sendButton = new JButton("Send");
    logoutButton = new JButton("Log out");
    loginButton = new JButton("Log in");
    exitButton = new JButton("Exit");

    JPanel center1 = new JPanel();
    center1.setLayout(new BorderLayout());
    center1.add(new JLabel("Broad Cast messages from all online users",
            JLabel.CENTER), "North");
    center1.add(new JScrollPane(txtBroadcast), "Center");

    JPanel south1 = new JPanel();
    south1.setLayout(new FlowLayout());
    south1.add(new JScrollPane(txtMessage));
    south1.add(sendButton);

    JPanel south2 = new JPanel();
    south2.setLayout(new FlowLayout());
    south2.add(loginButton);
    south2.add(logoutButton);
    south2.add(exitButton);

    JPanel south = new JPanel();
    south.setLayout(new GridLayout(2, 1));
    south.add(south1);
    south.add(south2);

    JPanel east = new JPanel();
    east.setLayout(new BorderLayout());
    east.add(new JLabel("Online Users", JLabel.CENTER), "East");
    east.add(new JScrollPane(usersList), "South");

    chatWindow.add(east, "East");

    chatWindow.add(center1, "Center");
    chatWindow.add(south, "South");

    chatWindow.pack();
    chatWindow.setTitle("Login for Chat");
    chatWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    chatWindow.setVisible(true);
    sendButton.addActionListener(this);
    logoutButton.addActionListener(this);
    loginButton.addActionListener(this);
    exitButton.addActionListener(this);
    logoutButton.setEnabled(false);
    loginButton.setEnabled(true);
    txtMessage.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent fe) {
            txtMessage.selectAll();
        }
    });

    chatWindow.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent ev) {
            if (s != null) {
                JOptionPane.showMessageDialog(chatWindow,
                        "u r logged out right now. ", "Exit",
                        JOptionPane.INFORMATION_MESSAGE);
                logoutSession();
            }
            System.exit(0);
        }
    });
}

// /////////////////////////
public void actionPerformed(ActionEvent ev) {
    JButton temp = (JButton) ev.getSource();
    if (temp == sendButton) {
        if (s == null) {
            JOptionPane.showMessageDialog(chatWindow,
                    "u r not logged in. plz login first");
            return;
        }
        try {
            dos.writeUTF(txtMessage.getText());
            txtMessage.setText("");
        } catch (Exception excp) {
            txtBroadcast.append("\nsend button click :" + excp);
        }
    }
    if (temp == loginButton) {
        String uname = JOptionPane.showInputDialog(chatWindow,
                "Enter Your lovely nick name: ");
        if (uname != null)
            clientChat(uname);
    }
    if (temp == logoutButton) {
        if (s != null)
            logoutSession();
    }
    if (temp == exitButton) {
        if (s != null) {
            JOptionPane.showMessageDialog(chatWindow,
                    "u r logged out right now. ", "Exit",
                    JOptionPane.INFORMATION_MESSAGE);
            logoutSession();
        }
        System.exit(0);
    }
}

这是代码的内部类,你可以在这里看到

if (temp == exitButton) {
        if (s != null) {
            JOptionPane.showMessageDialog(chatWindow,
                    "u r logged out right now. ", "Exit",
                    JOptionPane.INFORMATION_MESSAGE);
            logoutSession();
        }
        System.exit(0);
    }

无论默认的关闭操作是什么,代码都将终止JVM。 我没有使用默认关闭操作,因为:

f.setUndecorated(true);

我没有使用默认的" Windows窗口主题"

2 个答案:

答案 0 :(得分:6)

使用JFrame上的setDefaultCloseOperation(int)命令。

frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

将处置框架,但JVM将保持活动状态 - 除非所有窗口都已关闭,如评论中所指出的那样。

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

将丢弃框架并停止JVM。


默认设置为HIDE_ON_CLOSE

答案 1 :(得分:0)

如您所知,您可以使用退出图标创建一个JButton exit = new JButton(是的,这样很好......),您可以这样做:

1)window.dispose()(发布与窗口关联的任何来源)

2)或window.setVisible(false)只是制作窗口(不可见)

3)制作窗口(JFrame)时,您可以选择响应方式(JFrame.EXIT_ON_CLOSE,JFrame.DISPOSE_ON_CLOSE,JFrame.DO_NOTHING_ON_CLOSE)

如果没有打开窗口或者只是所有操作都已完成(没有循环运行等),java程序将自动terminate

如果您没有使用 window.setUndecorated(false),您可以使用Window Listener,如下所示:

window.addWindowListener(new WindowAdapter(){

@Override
public void windowClosing(WindowEvent e) {
   //This method is called when the X buttons is Clicked/Released
 }});

Window Listeners