大家好我是堆栈,所以如果任何人都可以给我一个帮助,那就太好了。因此,当我在jtextfield中输入一些值时,如果这个值与x * y中的值相同,则它应该正确执行,并且如果它们不相同,它们应该增加总数。但目前它总是增加总量。我认为我使用的逻辑是正确的,但我错过了一些东西。我正在使用eclipse,程序正在编译和运行。 我想问题是在actionPerformed方法的PanelQuizCountdown类中。这是代码。将价值传递给JLabel
/**The driver class of the program. Here is the JFrame
* class name RunQuizCountdown.java
* @author Kiril Anastasov
* @date 09/03/2012
*/
import java.awt.*;
import javax.swing.*;
public class RunQuizCountdown
{
public static void main(String[] args)
{
JFrame application = new JFrame();
PanelQuizCountdown panel = new PanelQuizCountdown();
application.add(panel);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(200,300);
application.setLocationByPlatform(true);
application.setVisible(true);
}
}
/** Here is the thread of the program
* class name ThreadQuizCountdown.java
* @author Kiril Anastasov
* @date 09/03/2012
*/
import javax.swing.JTextField;
public class ThreadQuizCountdown implements Runnable
{
JTextField timeField;
Thread myThread = new Thread(this);
int i = 30;
boolean go = true;
ThreadQuizCountdown(JTextField theTimeField)
{
timeField = theTimeField;
}
public void run()
{
while(go)
{
timeField.setText("" + i);
try
{
myThread.sleep(1000);
}
catch (InterruptedException ie)
{
System.out.println("thread exception");
}
if(i == 0)
{
//go = false;
myThread.stop();
}
i--;
}
}
public void begin()
{
myThread.start();
}
public void finish()
{
myThread.stop();
}
}
/** Here is the GUI of the program
* class name PanelQuizCountdown.java
* @author Kiril Anastasov
* @date 09/03/2012
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;
public class PanelQuizCountdown extends JPanel implements ActionListener
{
JTextField timeField, answerField;
JLabel messageLabel, correctLabel, totalLabel;
int x, y;
int correct;
int total;
int result;
int check;
Random randomGenerator;
ThreadQuizCountdown myQuiz;
PanelQuizCountdown()
{
timeField = new JTextField(5);
myQuiz = new ThreadQuizCountdown(timeField);
this.add(timeField);
myQuiz.begin();
randomGenerator = new Random();
x = randomGenerator.nextInt(12);
y = randomGenerator.nextInt(12);
messageLabel = new JLabel("What is the result of " + x + " * " + y);
this.add(messageLabel);
answerField = new JTextField(5);
answerField.addActionListener(this);
this.add(answerField);
correctLabel = new JLabel("You gave : " + correct + " correct answers");
this.add(correctLabel);
totalLabel = new JLabel("Of total: " + total + " questions");
this.add(totalLabel);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == answerField)
{
randomGenerator = new Random();
x = randomGenerator.nextInt(12);
y = randomGenerator.nextInt(12);
messageLabel.setText("What is the result of " + x + " * " + y);
System.out.println("Expected: " + result);
result = x * y;
String s = answerField.getText();
answerField.setText("");
check = Integer.parseInt(s);
System.out.println("Your answer: " + check);
if(result == check)
{
correct++;
total++;
}
else
{
total++;
}
correctLabel.setText("You gave : " + correct + " correct answers");
totalLabel.setText("Of total: " + total + " questions");
}
}
}
对不起,但除了旁注,我没有看到你写的代码和我的一个 – Kiril 2012-03-11 12:40:18
正确的区别。对不起,我不清楚要做什么来纠正它......我想你自从你接受了我的答案后就明白了。如果没有,让我知道,我会进一步引导你。 – aioobe 2012-03-11 15:37:51
我改正了,感谢您的时间! – Kiril 2012-03-11 16:23:48