Java 程序在GUI创建时不退出
出于测试目的,我在ClientGUI类中创建了一个GUI。程序似乎永远不会退出并挂起。我在main中调用我的create函数来查看gui的外观,在此之后程序应该终止,但它会挂起。请向我解释为什么会发生这种情况Java 程序在GUI创建时不退出,java,swing,Java,Swing,出于测试目的,我在ClientGUI类中创建了一个GUI。程序似乎永远不会退出并挂起。我在main中调用我的create函数来查看gui的外观,在此之后程序应该终止,但它会挂起。请向我解释为什么会发生这种情况 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.io.*; import java.n
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ClientGUI extends JFrame{
// creates username textfield
private JLabel username = new JLabel("Username ");
private JTextField textUsername = new JTextField(10);
//creates password textfield
private JLabel password = new JLabel("Password ");
private JPasswordField passText = new JPasswordField(10);
// adding two buttons
private JButton Login = new JButton("Login");
private JButton Create = new JButton("Create");
private JPanel error;
private JFrame my_error;
private JLabel errormsg = new JLabel("Error Try Again");
private JButton cancel_me = new JButton("OK");
private String option;
private String usr_name;
private char [] ident;
private String pass;
private String passme;
public volatile boolean []go = new boolean[1];
// constructor
ClientGUI(){
super("GossApp"); //title of jframe
}
public void CreatGui(){
JPanel myPanel = new JPanel(new GridBagLayout()); // creates a panel of type gridbaglayout
GridBagConstraints GridC = new GridBagConstraints();// constraints for layout
GridC.insets = new Insets(0,10,0,10); // spacing
// position 0,0 is corner
GridC.gridx = 0;
GridC.gridy = 0;
myPanel.add(username, GridC); // adding to panel
// position 0,1 adding user text field below username
GridC.gridy = 1;
myPanel.add(textUsername, GridC);
// position 0, 2 adding password name below user
GridC.gridx = 0;
GridC.gridy = 2;
myPanel.add(password, GridC);
//position 0,3 adding password text field below password
GridC.gridy = 3;
myPanel.add(passText, GridC);
// adding login button to panel next to password text
GridC.gridx = 1;
GridC.gridy = 3;
myPanel.add(Login, GridC);
Login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*
String my_name = textUsername.getText();
char [] my_password = passText.getPassword();
String my_stringword = new String(my_password);
*/
option ="log";
whileLog(option);
}
});
// adding create button gui next to user text field
GridC.gridx = 1;
GridC.gridy = 1;
myPanel.add(Create,GridC);
Create.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
option= "create";
whileLog(option);
}
});
add(myPanel); //adds gui to jframe
setSize(270,170); // creates dimensions of frame
setLocationRelativeTo(null); // center gui
setVisible(true); // I can see!
my_error = new JFrame("Error");
error = new JPanel(new GridBagLayout());
GridC.gridx= 1;
GridC.gridy = 1;
error.add(cancel_me,GridC);
cancel_me.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
my_error.setVisible(false);
}
});
GridC.gridy = 2;
error.add(errormsg,GridC);
my_error.add(error);
my_error.pack();
my_error.setLocationRelativeTo(null);
my_error.setVisible(false);
}
public class clientserver{
public static void main(String[]args) {
ClientGUI my_gui = new ClientGUI();
my_gui.CreatGui();
}
}
你需要设置
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
对于您的ClientGui,否则会出现默认值,这使得它无法真正关闭。默认情况下,JFrame没有被正确销毁,因此JVM将保持运行
中的附加小段落,显示默认值
[……]
WindowConstants中定义的HIDE_ON_CLOSE:在调用任何已注册的WindowListener对象后自动隐藏帧
[……]
JFrame中定义的EXIT_ON_CLOSE:使用系统退出方法退出应用程序。仅在应用程序中使用此选项
[……]
默认情况下,该值设置为在关闭时隐藏。更改此属性的值会引发属性更改事件,属性名为defaultCloseOperation
[……]
我怀疑当日志阻塞EDT时,请参阅以了解原因和可能的方式,可能是因为my_error JFrame wll始终不可见而不是被破坏,这将保持JVM运行。您还需要将JFrame.setDefaultCloseOperationJFrame.EXIT_设置为关闭,以便JFrame正确关闭,否则关闭它将使其成为dispose而不是真正退出我尝试注释whilelog并得到相同的结果result@SomeJavaGuy你是个天才!!!!!!!