英文:
BlockingQueue can't read String from JTextField
问题
public class UserConsole {
protected static BlockingQueue<String> inputData;
private final static JTextArea textArea = new JTextArea();
private static JTextField textField = new JTextField("");
private void createGUI() {
final KeyListener returnAction = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == '\n') {
returnInput();
}
}
};
}
private void returnInput() {
// 这里是问题所在,BlockingQueue 抛出了 NullPointerException,这很奇怪,因为...
// ...紧接着 "System.out.println(textField.getText());" 却能正常运行。
inputData.offer(textField.getText());
System.setOut(userStream);
System.out.println(textField.getText());
textField.setText("");
System.setOut(nebulaStream);
}
}
希望以上翻译符合您的要求。如果还有其他需要翻译的内容,请继续提供。
英文:
I am having an issue creating a custom console, on the following code:
public class UserConsole {
protected static BlockingQueue<String> inputData;
private final static JTextArea textArea = new JTextArea();
private static JTextField textField = new JTextField("");
private void createGUI() {
final KeyListener returnAction = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == '\n') {
returnInput();
}
}};
}
private void returnInput() {
//Here is the problem, the BlockingQueue throws a NullPointerException, which is strange, because the...
//...right after "System.out.println(textField.getText());" works perfectly fine.
inputData.offer(textField.getText());
System.setOut(userStream);
System.out.println(textField.getText());
textField.setText("");
System.setOut(nebulaStream);
}
}
I tried searching online, but didn't find anything, also tried adding .toString()
but it does not work as well.
As far as I know, a BlockingQueue cannot be initialized... So my final question is. Why is the BlockingQueue not reading the JTextField's string, and how can it be solved?
I hope it is not something obvious that I missed, every help is appreciated!
答案1
得分: 0
> 据我所知,BlockingQueue 无法初始化... 所以我的最后一个问题是,为什么 BlockingQueue 没有读取 JTextField 的字符串,该如何解决?
我们没有看到代码的一些重要部分,但是这里有一些可能的问题。
-
我没有看到阻塞队列实际上是在哪里被实例化的。也许初始化的代码应该像这样:
protected static final BlockingQueue<String> inputData = new LinkedBlockingQueue<>();
-
我们没有看到字符串是如何从阻塞队列中移除的。如果你想要检查结果是否已经计算出来,你应该使用
poll()
方法,它会在没有结果的情况下返回null
,直到有数据被提供给阻塞队列。// 这不会等待结果,但一开始会返回 null String jtextResult = inputData.poll();
如果你 需要 等待结果,那么可以使用
take()
方法,但是你绝不应该在 GUI 线程上这样做,因为这会导致 UI 暂停,直到结果计算完成,从而抵消了使用后台线程的意义。
英文:
> As far as I know, a BlockingQueue cannot be initialized... So my final question is. Why is the BlockingQueue not reading the JTextField's string, and how can it be solved?
We aren't seeing some of the important sections of the code but here are some possible problems.
-
I don't see where the blocking queue is actually instantiated. Maybe the initialization line should be something like:
protected static final BlockingQueue<String> inputData = new LinkedBlockingQueue<>();
-
We don't see where the string is removed from the blocking queue. If you want to check to see if the result has been calculated then you should use
poll()
which will returnnull
until the reset is offered to the blocking queue.// this will not wait for the result but will return null initially String jtextResult = inputData.poll();
If you need to wait for the result then use
take()
but you should never do this on a GUI thread because your UI will pause until the result is calculated negating the need for the background thread.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论