gpt4 book ai didi

java - @Overide toString 与 Integer。然后将Text设置为JLabel

转载 作者:行者123 更新时间:2023-12-02 05:55:13 25 4
gpt4 key购买 nike

expected 包含数学问题的答案。

作为一个int,我发送toString

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

然后我将setText设置为JLabel

 jl.setText(toString());

当程序运行时,它不显示答案,而是显示哈希值。

我假设这必须与 Integer.toString 一起使用,但我在 toString 方法中调用它。

我在这里做错了什么?

Driver Class

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.*;

public class Driver extends MathProblems {

MathProblems problems = new MathProblems();

String s = "Welcome Students!";
String b = "Start!";
private JFrame f;
private JPanel p;

JFrame frame = new JFrame();

JButton b1 = new JButton(b);

JLabel jl = new JLabel(s);

int i;

public Driver () {
gui();
}

public void gui() {
f = new JFrame("Flash Card Program");
p = new JPanel();
f.setLayout( new GridLayout( 2, 1 ) );
f.add(jl);
f.add(p);
p.setLayout( new GridLayout( 2, 1 ) );
p.add(b1);

jl.setHorizontalAlignment(JLabel.CENTER);

// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
f.setSize(560,400); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if(b1.getText().equals("Click For Answer"))
{
problems.run();
jl.setText(toString());
String b = "Next Question";
b1.setText(b);
}
else
{
problems.run();
jl.setText(problems.getQuestion());
String b = "Click For Answer";
b1.setText(b);

}
}
});
}


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
}
});
} // End main Method

} // End class Driver

MathProblems

import java.util.Random;

public class MathProblems {
private static final int MAX_NUMBER = 10;
private static final Random random = new Random();

private int expected = 0;
private String question = "";

public void run() {
final int a = random.nextInt(MAX_NUMBER);
final int b = random.nextInt(MAX_NUMBER);

final int type = random.nextInt(4);

switch (type) {
case 0:
add(a, b);
break;
case 1:
subtract(a, b);
break;
case 2:
multiply(a, b);
break;
case 3:
divide(a, b);
break;
}
}

private void add(final int a, final int b) {
expected = a + b;

askQuestion(a + " + " + b + " = ");
}

private void subtract(final int a, final int b) {
expected = a - b;

askQuestion(a + " - " + b + " = ");
}

private void multiply(final int a, final int b) {
expected = a * b;

askQuestion(a + " * " + b + " = ");
}

private void divide(final int a, final int b) {
expected = a / b;

askQuestion(a + " / " + b + " = ");
}

private void askQuestion(final String question) {
this.question = question;
}

public String getQuestion() {
return question;
}

public int getAnswer() {
return expected;
}

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

最佳答案

您需要将其更改为:

if(b1.getText().equals("Click For Answer"))
{
problems.run();
jl.setText(problems.toString()); // note the change here
String b = "Next Question";
b1.setText(b);
}
else
{
problems.run();
jl.setText(problems.getQuestion()); // and how you got it right here
String b = "Click For Answer";
b1.setText(b);

}

关于java - @Overide toString 与 Integer。然后将Text设置为JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23161177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com