BlockingQueue 无法从 JTextField 中读取 String。

huangapple go评论117阅读模式
英文:

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&lt;String&gt; inputData;

	private final static JTextArea textArea = new JTextArea();
	private static JTextField textField = new JTextField(&quot;&quot;);

private void createGUI() {

    final KeyListener returnAction = new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyChar() == &#39;\n&#39;) {
					returnInput();
                   }
			}};
    }

private void returnInput() {


//Here is the problem, the BlockingQueue throws a NullPointerException, which is strange, because the...
//...right after &quot;System.out.println(textField.getText());&quot; works perfectly fine.
   inputData.offer(textField.getText());
		System.setOut(userStream);
		System.out.println(textField.getText());
		textField.setText(&quot;&quot;);
		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 的字符串,该如何解决?

我们没有看到代码的一些重要部分,但是这里有一些可能的问题。

  1. 我没有看到阻塞队列实际上是在哪里被实例化的。也许初始化的代码应该像这样:

    protected static final BlockingQueue&lt;String&gt; inputData = new LinkedBlockingQueue&lt;&gt;();
    
  2. 我们没有看到字符串是如何从阻塞队列中移除的。如果你想要检查结果是否已经计算出来,你应该使用 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.

  1. I don't see where the blocking queue is actually instantiated. Maybe the initialization line should be something like:

    protected static final BlockingQueue&lt;String&gt; inputData = new LinkedBlockingQueue&lt;&gt;();
    
  2. 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 return null 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.

huangapple
  • 本文由 发表于 2020年7月24日 04:47:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63062892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定