jTable swing获取输入问题
我有 getInput 方法,可以在单击输入时从文本字段中获取字符串。我创建了 while 循环来等待 onClickListener 返回 true (按 Enter 键)。这是我的代码:
public String getInput(){
jTextField1.setEditable(true);
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
temp=jTextField1KeyPressed(evt);
};
});
while(!(temp)){
}
temp=false;
jTextField1.setEditable(false);
String s= jTextField1.getText();
jTextField1.setText("");
return s;
}
private boolean jTextField1KeyPressed(KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER)
return true;
else return false;
};
现在我有一个非常奇怪的问题:如果我在 while 循环中添加 System.out.println 语句,它会完美工作,如果我删除它,while 循环永远不会退出。问题出在哪里? 提前致谢。
I've getInput method that takes string from text field on enter click. I've created while loop to wait until onClickListener returns true (pressed Enter). Here's my code:
public String getInput(){
jTextField1.setEditable(true);
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
temp=jTextField1KeyPressed(evt);
};
});
while(!(temp)){
}
temp=false;
jTextField1.setEditable(false);
String s= jTextField1.getText();
jTextField1.setText("");
return s;
}
private boolean jTextField1KeyPressed(KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER)
return true;
else return false;
};
Now I have very strange problem: if I add System.out.println sentence in while loop, it works perfectly, if I remove it, while loop never exits. Where is problem?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要一些同步,以便将
temp
的更改传播到所有所需的线程。但这是一个糟糕的开始方法 -getInput
会在while
中浪费大量 CPU 循环,没有任何好处。你可以做得更干净。首先,将
temp
声明为java.lang.Object
,并将其初始化为普通的旧对象:(或类似的东西。)
在 getInput 中,而不是这样:
把这个:
在事件处理程序中:
这样,运行 getInput 的线程就会保持睡眠状态,等待事情发生,而不是消耗 CPU 周期。
还有一些采用超时值的
wait()
变体。您可能也有兴趣使用它们。You would need some synchronization so that the changes to
temp
are propagated to all required threads. But it's a bad approach to start with -getInput
will waste a lot of CPU looping in thatwhile
for no good purpose.You could do it more cleanly. First, declare
temp
to be ajava.lang.Object
, and initialize it to a plain old Object:(or something like that.)
In getInput, instead of this:
put this:
And in the event handler:
This way, the thread running
getInput
stays asleep waiting for something to happen rather than burning CPU cycles.There are also variants of
wait()
that take a timeout value. You might be interested in using those too.您的问题称为“忙等待”。您必须等待,直到操作系统告诉您已按下 Enter 键。问一个虚拟问题“是否按下了 Enter?”、“是否按下了 Enter?”、“是否按下了 Enter?”、“是否按下了 Enter?”、“是否按下了 Enter?”、“是否按下了 Enter?”、“按下 Enter 了吗?”每秒 1000000000 次不是一个好的做法......
您可以尝试使用这个:
Your problem is called "busy waiting". You have to wait, until operating system tell you, that Enter was pressed. Asking a dummy question "Was Enter pressed?", "Was Enter pressed?", "Was Enter pressed?", "Was Enter pressed?", "Was Enter pressed?", "Was Enter pressed?", "Was Enter pressed?" 1000000000 times per second is not a good practice....
You may try to use this:
如果您从事件线程调用 getInput,您将陷入无限循环。变量 temp 可能永远不会更新,因为无法再处理任何事件。
如果您想收集简单的输入,您可能需要研究类似的解决方案
If you call your getInput from the event thread you will get stuck in an endless loop. The variable temp might never get updated because no events can be processed anymore.
If you want to gather a plain input you might want to look into a solution like