gpt4 book ai didi

java - 如果出现新框架 setEditable(false) ,如果用户关闭它 setEditable(true)

转载 作者:行者123 更新时间:2023-12-02 00:23:15 24 4
gpt4 key购买 nike

我想创建此代码:用户输入一个数值,如果输入字符则会抛出异常该字段将停止工作,然后出现另一个框架并显示错误消息用户关闭新框架后,一切恢复原样这意味着该领域将再次发挥作用!我设法使该字段停止工作,但我不知道用户是否关闭了新框架!这是我的尝试

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);}

}
}

最佳答案

1).不要使用新的 JFrame 来显示错误消息 - 使用 JDialog Here is how

2). h=Double.parseDouble("0"+input_width.getText()); 我认为你的意思是 input_height.getText() 这里,而不是 input_width.getText ()

3).显示错误对话框后,只需清除文本字段 - 就可以了。当用户关闭它时 - 他会看到它们是空的。

关于java - 如果出现新框架 setEditable(false) ,如果用户关闭它 setEditable(true),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10550389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com