大家好我是堆棧,所以如果任何人都可以給我一個幫助,那就太好了。因此,當我在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