【发布时间】:2012-05-11 11:22:05
【问题描述】:
我想创建这段代码: 用户输入数值,如果输入字符会抛出异常 该字段将停止工作,然后出现另一个框架并显示错误消息 用户关闭新框架后,一切恢复原状 这意味着该领域将再次发挥作用! 我设法使该领域停止工作,但我不知道用户是否关闭了新框架! 这是我的尝试
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会更好。 docs.oracle.com/javase/6/docs/api -
我必须使用 JFrame ,这是他们想要的
标签: java swing exception jframe jtextfield