简体   繁体   English

从JTextArea获取用户输入

[英]Getting user input from JTextArea

I'm trying to build a quiz program. 我正在尝试构建测验程序。

I decided that users enter their answers(numbers) into a JTextArea and the result will be shown on another JTextArea after a button click however I'm having troubles. 我决定用户将他们的答案(数字)输入到JTextArea中,并且单击按钮后,结果将显示在另一个JTextArea上,但是我遇到了麻烦。

Here is partial of my code. 这是我的部分代码。

JButton btnNewButton = new JButton("Submit!");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

                    if(textArea_3.equals("1"))
                            {
                                textArea_1.setText("Correct!");
                            }




        }
    });

I think you mean - 我想你的意思是-

    if(textArea_3.getText().equals("1"))
    {
        textArea_1.setText("Correct!");
        //your code
    }

and not 并不是

textArea_3.equals("1")

You cannot compare JTextArea instance with a String instance. 您不能将JTextArea实例与String实例进行比较。 You will always get false. 您将永远是假的。

if(textArea_3.equals("1"))

应该是if(textArea_3.getText().equals("1"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM