1 java user-interface swing 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);
Run Code Online (Sandbox Code Playgroud)
关键在于只关闭最大化的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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是代码的内部类,您可以在此处看到
if (temp == exitButton) {
if (s != null) {
JOptionPane.showMessageDialog(chatWindow,
"u r logged out right now. ", "Exit",
JOptionPane.INFORMATION_MESSAGE);
logoutSession();
}
System.exit(0);
}
Run Code Online (Sandbox Code Playgroud)
无论默认的关闭操作是什么,代码都将终止JVM.我没有使用默认关闭操作,因为:
f.setUndecorated(true);
Run Code Online (Sandbox Code Playgroud)
我没有使用默认的"Windows窗口主题"
使用setDefaultCloseOperation(int)命令JFrame.
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Run Code Online (Sandbox Code Playgroud)
将处置框架,但JVM将保持活跃 - 除非所有窗口都已关闭,正如评论中指出的那样.
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Run Code Online (Sandbox Code Playgroud)
将处置框架并停止JVM.
默认设置为HIDE_ON_CLOSE.