我有一个叫做driver
的类,在这个类中,我有一个gui,它具有jbutton
和jlabel
jbutton
的onclick我正在用随机数学问题更新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
MathProblems类
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
作为输出,是因为您正在toString()
上调用MathProblems
而没有覆盖它。您得到的输出是MathProblems
对象的哈希码,这是toString()
的默认行为。在MathProblems
中,您必须定义以下内容:
@Override
public String toString()
{
// return the String you want displayed.
}
您遇到的另一个问题是,将第二个
ActionListener
添加到b1
时,它将覆盖第一个。我没有尝试运行您的代码,但是我认为这将导致您的按钮始终显示“下一个问题”,并且标签将始终显示expected
中的字符串,而不会显示answer
中的字符串。您可以尝试这样的事情: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());
}
}
});
另外,您不必在
toString()
上调用b
,因为它已经是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!";
}
}
}