我想创建这个代码: 用户输入一个数值,如果他输入字符就会抛出异常 该字段会停止工作然后另一个框架显示并显示错误消息 用户关闭新框架后,一切都返回到它的方式 这意味着该领域将再次工作! 我设法让该领域停止工作,但我不知道用户是否关闭了新框架! 这里是我的尝试如果一个新的Frame显示setEditable(false),如果用户关闭它setEditable(true)
public void keyReleased(KeyEvent event) {
try{
double l,h,w;
l=Double.parseDouble(input_length.getText());
w=Double.parseDouble("0"+input_width.getText());
h=Double.parseDouble("0"+input_width.getText());
}
catch(NumberFormatException a){
input_length.setEditable(false);
input_height.setEditable(false);
input_width.setEditable(false);
JFrame ErrorFrame = new JFrame("Error");
JPanel content = new JPanel(); ;
ErrorFrame.setContentPane(content);
ErrorFrame.setSize (350, 150);
ErrorFrame.setResizable (false);
ErrorFrame.setLocation (FRAME_X_ORIGIN, 250);
content.setLayout(new FlowLayout());
JLabel text = new JLabel(" ERROR ! please Enter number only ",JLabel.CENTER);
text.setFont(new Font("Arial", Font.PLAIN, 20));
text.setForeground(Color.red);
content.add(text);
ErrorFrame.setVisible(true);
setDefaultCloseOperation(ErrorFrame.EXIT_ON_CLOSE);
int op = ErrorFrame.getDefaultCloseOperation();
if(op == 1){
input_length.setEditable(true);
input_height.setEditable(true);
input_width.setEditable(true);}
}
}
您是否使用'JFrame'作为错误信息?我认为'JDialog'会更好。 http://docs.oracle.com/javase/6/docs/api/ – twain249
我必须使用JFrame,他们想要的是 –