setText to Jlabel with function

后端 未结 4 834
我寻月下人不归
我寻月下人不归 2021-01-26 06:49

I am trying to settext to JLabel with a function.

The checkResults function is not working for some reason I still get errors. I have 2 classe

相关标签:
4条回答
  • 2021-01-26 07:25

    Okay, this

    jl.setText.(answers.checkResult());
    

    should be this:

    jl.setText(answers.checkResult(expected));
    

    this:

    public String checkResult(final int answer) {
             return Integer.toString(expected);
         }
    

    should be something like this:

       public int getAnswer() {
             return expected;
       }
    

    NOW here's the REAL issue

    You shouldn't be doing this:

    String s = in.nextLine();
    

    You should add a textbox to the form to enter the value, that's why it's freezing on you. Scanner is used for command line input output

    Change this:

    String s = in.nextLine();
    int expected = Integer.parseInt(s);
    

    to

    int expected = problems.getAnswer();
    
    0 讨论(0)
  • 2021-01-26 07:27

    I think jl.setText requires a string a string value instead of passing an integer.

    In your case perhaps

    jl.setText(answers.checkResult(Integer.toString(expected)));
    

    and change

    public String checkResult(String answer) {
         return Integer.toString(expected);
     }
    

    Although Im not entirely sure what you are trying to achieve here

    0 讨论(0)
  • 2021-01-26 07:40

    As I commented on your previous question: You need to remove "problems.run()" from your first condition in the listener, otherwise the "answer" you get will be for the next question, and so on. Also, consider using doubles in your divide() method, by using ints you will lose the decimals.

    0 讨论(0)
  • 2021-01-26 07:50

    The solution to this problem is expected needs to be

     @Override
     public String toString(){
     return Integer.toString(expected);
     }
    

    Then it needs to be set to JLabel like this,

    jl.setText(problems.toString());
    
    0 讨论(0)
提交回复
热议问题