我正在尝试创建一个gui,用户可以使用JTextFields在其中放置一些数据。如果文本字段为空,则将显示JDialog并显示一条消息。该代码可以正常工作,但是今天不起作用。我在这里有主要功能:
public class Ex1_Start extends JFrame implements ActionListener{
Traveler currentTraveler = new Traveler();
public static void main(String[] args){
GUI gui = new GUI();
}
}
和此处的GUI类:
public class GUI extends JFrame implements ActionListener{
JFrame f = new JFrame();
JDialog j = new JDialog();
JLabel err = new JLabel();
JTextField uin1 = new JTextField();
JTextField uin2 = new JTextField();
JTextField uin3 = new JTextField();
JTextField uin4 = new JTextField();
public GUI(){
j.add(err);
if(textFieldsempty(uin1,uin2,uin3,uin4)) { \\checks if the text fields are empty
popUpError("You forgot to add a field");
} else{
//some other code
}
}
public void popUpError(String str){
System.out.println("Error");
j.setVisible(true);
err.setText(str);
}
}
当程序启动时,JFrame可以很好地显示,并且控制台显示Error字样,但是没有显示窗口,即使是很小的默认大小也没有。为什么会发生这种情况的任何想法都会很有帮助。