gpt4 book ai didi

java - 使用 java 中另一个类的方法更新 jbutton 的 jlabel onclick

转载 作者:行者123 更新时间:2023-12-01 23:03:40 27 4
gpt4 key购买 nike

我有一个名为driver的类,在这个类中我有一个gui,它有一个jbutton和一个jlabel

单击jbutton 我正在用随机数学问题更新jlabel

当程序启动时,

Jlabel = "Welcome Students"
Jbutton = "Start!"

点击

jlabel = "a math problem here"
Jbutton = "Click For Answer"

下一次点击

jlabel = "Answer for the math problem"
jbutton = "Next question"

此时 jlabel 和 jbutton 将处于循环状态交替

jlabel = alternate math problem and answer
jbutton Click For Answer and Next Question

到目前为止,我不知道如何添加第二个 onclick 函数以及数学问题的时间出现在 jlabel 中的内容是 MathProblems@49bcf06 或始终跟随 MathProblems 的一些随机数字和字母。

这是我的代码,

驱动程序类别

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

import javax.swing.*;

public class Driver extends MathProblems {

MathProblems question = new MathProblems();
MathProblems expected = 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){
String b = "Click For Answer";
b1.setText(b.toString());
jl.setText(question.toString());

}
});

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String b = "Next Question";
b1.setText(b.toString());
jl.setText(expected.toString());

}
});
}


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
new MathProblems();

}
});
} // End main Method

} // End class Driver

数学问题类

import java.util.Random;
import java.util.Scanner;

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

public void run() {
while(true) {
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) {
final int expected = a + b;

final int answer = askQuestion(a + " + " + b + "=");

checkResult(expected, answer);
}

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

final int answer = askQuestion(a + " - " + b + "=");

checkResult(expected, answer);
}

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

final int answer = askQuestion(a + " * " + b + "=");

checkResult(expected, answer);
}

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

final int answer = askQuestion(a + " / " + b + "=");

checkResult(expected, answer);
}

private int askQuestion(final String question) {
System.out.print(question);

return in.nextInt();
}

private void checkResult(final int expected, final int answer) {
if (expected == answer) {
System.out.println("Correct answer! You rock!");
} else {
System.out.println("WROOONG! You suck!");
}
}

}

最佳答案

您获得 MathProblems@49bcf06 作为输出的原因是您在 MathProblems 上调用 toString() 而没有覆盖它。您获得的输出是 MathProblems 对象的哈希码,这是 toString() 的默认行为。在 MathProblems 中,您必须定义以下内容:

@Override
public String toString()
{
// return the String you want displayed.
}

<小时/>您遇到的另一个问题是,当您将第二个 ActionListener 添加到 b1 时,它会覆盖第一个。我还没有尝试运行您的代码,但我认为这会导致您的按钮始终显示“下一个问题”,并且标签将始终显示来自 expected 的字符串,而不是来自 回答。你可以尝试这样的事情:

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

此外,您不需要对 b 调用 toString(),因为它已经是一个 String

<小时/>

编辑:

驱动程序类别:

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

import javax.swing.*;

public class Driver extends MathProblems {

MathProblems problems = new MathProblems();
private static final Scanner in = new Scanner(System.in);

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")
{
String s = in.nextLine();
int answer = Integer.parseInt(s);
String result = problems.checkResult(answer);
j1.setText(result);
String b = "Next Question";
b1.setText(b);
}
else
{
problems.run();
j1.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

<小时/>数学问题课:

import java.util.Random;
import java.util.Scanner;

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 int askQuestion(final String question) {
this.question = question;
}

public String getQuestion() {
return question;
}

public String checkResult(final int answer) {
if (expected == answer) {
return "Correct answer! You rock!";
} else {
return "WROOONG! You suck!";
}
}

}

关于java - 使用 java 中另一个类的方法更新 jbutton 的 jlabel onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23120746/

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