简体   繁体   English

在文本区域中显示文本

[英]To display text in text area

I am making a game which will display a simplification question and user has to solve the question according to BODMAS rule using swings in Java. 我正在制作一个游戏,该游戏将显示一个简化的问题,用户必须使用Java中的摆动来根据BODMAS规则解决该问题。 GUI looks like this GUI看起来像这样 在此处输入图片说明

When 'New' button is pressed then new question will be displayed in text area and button label changes to 'Next' 当按下“新建”按钮时,新问题将显示在文本区域中,并且按钮标签变为“下一步” 在此处输入图片说明

But if 'Next' button is pressed it does not display next question. 但是,如果按下“下一步”按钮,则不会显示下一个问题。 operator.txt contains list of simplification question. operator.txt包含简化问题列表。 the code is as follows 代码如下

 public void actionPerformed(ActionEvent buttonEvent) {
        // TODO Auto-generated method stub
        if(buttonEvent.getActionCommand().equals("Enter")){
            Double computedVal = EvaluateString(textArea.getText());
            System.out.println(computedVal);
            checkAnswer(computedVal, textField.getText());
        }
        if(buttonEvent.getActionCommand().equals("Clear")){
            ClearTextArea();
        }
        if(buttonEvent.getActionCommand().equals("New")){
            String store = buttonEvent.getActionCommand();
            NewGameOrNext(store);   
        }
    }

private void NewGameOrNext(String store) {
        // TODO Auto-generated method stub
        if(store.equals("New")){
            buttonNew.setText("Next");
        }
        String line = null;
        try{
            BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\username\\Documents\\operator.txt"));
            while((line = reader.readLine()) != null){
                textArea.setText(line);
            }
            reader.close();
        }
        catch(IOException exception){
            exception.printStackTrace();
        }
    }

please help me with this! 请在这件事上给予我帮助!

First of all method names should NOT start with an upper case character. 首先,方法名称不应以大写字母开头。 Have you ever seen a method from the Java API start with an upper case character? 您是否见过Java API中以大写字母开头的方法? Some of your method names are correct, others are not. 您的某些方法名称正确,而其他则不正确。 Be consistent!!! 始终如一!!!

But if 'Next' button is pressed it does not display next question. 但是,如果按下“下一步”按钮,则不会显示下一个问题。

if(buttonEvent.getActionCommand().equals("New")){

The above conditions will never be true if you reset the text from "New" to "Next". 如果将文本从“新建”重置为“下一步”,上述条件将永远不会成立。

Seems like you need another if condition to test for "Next". 似乎您需要另一个条件来测试“下一步”。 Or, better yet don't change the text, just leave it as "Next". 或者,最好不要更改文本,只需将其保留为“ Next”即可。

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

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