[英]updating jlabel onclick of jbutton with methods from another class in java
我有一个叫做driver
的类,在我的gui中有一个jbutton
和一个jlabel
jbutton
的jlabel
我正在用随机数学问题更新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
作为输出,是因为要在MathProblems
上调用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()); } } });
另外,您不需要在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!"; } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.