按键事件
我正在尝试使用 NetBeans6.8 学习有关 GUI 的知识,从 Java 教程中的 GUI 部分开始。
有一个关于摄氏度-华氏度转换器的简单练习。我希望它有两个文本字段,一个用于摄氏温度,一个用于华氏温度;如果用户在摄氏文本字段中键入,他会在华氏文本字段中“打印”结果。反之亦然。
因此,我将两个文本字段都放在一个 KeyTyped 事件上,代码如下:
private void celsiusTextKeyTyped(java.awt.event.KeyEvent evt) {
int cels = Integer.parseInt(celsiusText.getText());
int fahr = (int)(cels * 1.8 + 32);
fahrText.setText(fahr + "");
}
private void fahrTextKeyTyped(java.awt.event.KeyEvent evt) {
int fahr = Integer.parseInt(fahrText.getText());
int cels = (int)(fahr / 1.8 - 32);
celsiusText.setText(cels + "");
}
它不起作用。如果我在文本字段中输入某些内容,则会出现此异常: java.lang.NumberFormatException: For input string: ""
附加侦听器的代码:
celsiusText.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
celsiusTextKeyTyped(evt);
}
});
fahrText.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
fahrTextKeyTyped(evt);
}
});
[但是,我无法修改它,它是自动生成的.]
I'm trying to learn something about GUI, using NetBeans6.8, starting with the GUI section in The java tutorial.
There is a simple exercise for a Celsius-Fahrenheit converter. I want that to have two TextFields, one for Celsius and one for Fahrenheit temperature; if the user types in the celsius text field he got the result "printed" in the fahrenheit text filed. and vice versa.
So, i put on both the textfields one KeyTyped event, here's the code:
private void celsiusTextKeyTyped(java.awt.event.KeyEvent evt) {
int cels = Integer.parseInt(celsiusText.getText());
int fahr = (int)(cels * 1.8 + 32);
fahrText.setText(fahr + "");
}
private void fahrTextKeyTyped(java.awt.event.KeyEvent evt) {
int fahr = Integer.parseInt(fahrText.getText());
int cels = (int)(fahr / 1.8 - 32);
celsiusText.setText(cels + "");
}
It doesn't work. If i type something in a textfield i got this exception: java.lang.NumberFormatException: For input string: ""
The code that attach the listeners:
celsiusText.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
celsiusTextKeyTyped(evt);
}
});
fahrText.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
fahrTextKeyTyped(evt);
}
});
[However, i can't modify it, it's autogenerated.]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方法 .getText() 返回一个字符串而不是数字,如果该字符串包含非数字字符(即字母、空格、什么都没有),那么 parseInt 将抛出 NumberFormatException。由于您使用 KeyEvent,只要您按下“7”,该事件就会在 7 输入文本框中之前触发。因此文本框仍然只包含“”,这就是错误的来源。您可能还希望监听 keyUp 事件。
您需要将代码包含在 try catch 块中。
另一种方法是您可以过滤掉 keydown 事件中的非数字,请参阅此处的示例 - http:// www.javacoffeebreak.com/java107/java107.html(创建自定义组件 - NumberTextField)
Method .getText() returns a string not a number, if that string contains non-numeric characters (i.e. a letter, a space, nothing at all) then parseInt will throw a NumberFormatException. Since your using KeyEvent, as soon as you press say "7", the event is fired before 7 is entered into the text box. Thus the text box still only contains "", which is where the error comes from. You may wish to also listen to the keyUp event instead.
You need to enclose your code in a try catch block.
An alternative is you could filter out non-numbers on keydown event, see example here - http://www.javacoffeebreak.com/java107/java107.html (Creating a custom component - NumberTextField)
我怀疑发生的情况是您使用诸如 celsiusText.addKeyListener 之类的内容添加了这些处理程序,是吗?
问题是,这不仅会为您提供所需的
KEY_TYPED
事件,还会为您提供KEY_DOWN
和KEY_UP
事件。KEY_DOWN
事件将在文本真正输入到字段之前发生,因此触发该事件的代码将看到该字段仍然为空白。尝试将空字符串转换为数字会导致格式异常。解决此问题的最简单方法是其他人发布的
try
/catch
结构。I suspect that what's happened is that you added these handlers with something like
celsiusText.addKeyListener
, yes?The thing is, that'll give you not just the
KEY_TYPED
events you wanted, but alsoKEY_DOWN
andKEY_UP
. TheKEY_DOWN
event will happen before the text is really entered into the field, so your code firing on that will see the field as blank still. Trying to convert the empty string to a number gives you a format exception.The easiest way to fix this is the
try
/catch
construct other people have been posting.您可能将操作设置为 keyDown,这意味着即使在键值“添加”到文本框之前发生,而您从中检索到的值仍然为空
“”
。You probably set action to keyDown, this mean that even occur before the key value is "added" to textbox, while You retrieve the value from it is still empty
""
.这是一个非常古老的例子。更好的方法是使用 DocumentListener,而不是 KeyListener。
That is a really old example. The better approach is to use a DocumentListener, not a KeyListener.