jTable swing获取输入问题

发布于 2024-10-24 07:05:12 字数 795 浏览 2 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谎言 2024-10-31 07:05:12

您需要一些同步,以便将 temp 的更改传播到所有所需的线程。但这是一个糟糕的开始方法 - getInput 会在 while 中浪费大量 CPU 循环,没有任何好处。

你可以做得更干净。首先,将 temp 声明为 java.lang.Object,并将其初始化为普通的旧对象:(

private Object temp = new Object();

或类似的东西。)

在 getInput 中,而不是这样:

while (!temp) { }
temp = false;

把这个:

synchronized (temp) {
  try {
    temp.wait();
  } catch (InterruptedException ie) {
    // handle this situation: something interrupted your thread before input was finished
  }
}

在事件处理程序中:

synchronized (temp) {
  temp.notify();
}

这样,运行 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 that while for no good purpose.

You could do it more cleanly. First, declare temp to be a java.lang.Object, and initialize it to a plain old Object:

private Object temp = new Object();

(or something like that.)

In getInput, instead of this:

while (!temp) { }
temp = false;

put this:

synchronized (temp) {
  try {
    temp.wait();
  } catch (InterruptedException ie) {
    // handle this situation: something interrupted your thread before input was finished
  }
}

And in the event handler:

synchronized (temp) {
  temp.notify();
}

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.

‖放下 2024-10-31 07:05:12

您的问题称为“忙等待”。您必须等待,直到操作系统告诉您已按下 Enter 键。问一个虚拟问题“是否按下了 Enter?”、“是否按下了 Enter?”、“是否按下了 Enter?”、“是否按下了 Enter?”、“是否按下了 Enter?”、“是否按下了 Enter?”、“按下 Enter 了吗?”每秒 1000000000 次不是一个好的做法......

您可以尝试使用这个:

jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyPressed(java.awt.event.KeyEvent evt) {
        temp=jTextField1KeyPressed(evt);
        if (temp==true) EnterPressed();
    };
});
void EnterPressed() {
    //enter code here
}

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:

jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyPressed(java.awt.event.KeyEvent evt) {
        temp=jTextField1KeyPressed(evt);
        if (temp==true) EnterPressed();
    };
});
void EnterPressed() {
    //enter code here
}
自控 2024-10-31 07:05:12

如果您从事件线程调用 getInput,您将陷入无限循环。变量 temp 可能永远不会更新,因为无法再处理任何事件。

如果您想收集简单的输入,您可能需要研究类似的解决方案

String info = JOptionPane.showInputDialog(this, "Please enter info");

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

String info = JOptionPane.showInputDialog(this, "Please enter info");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文