【发布时间】:2015-03-06 21:49:38
【问题描述】:
不... system.exit(0) 不是我要找的。p>
我有一个带有 5 个按钮的 MainPage GUI,单击其中的 4 个按钮会为不同的事物打开单独的 GUI,一个打开聊天服务器,一个打开文件对话框,等等。但是,如果我使用 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 窗口主题”
【问题讨论】:
标签: java swing user-interface jframe