안녕하세요 여러분, 저에게 도움이 될만한 사람이 있다면 큰 도움이 될 것입니다. 따라서 jtextfield에 값을 입력 할 때이 값이 x * y와 같으면 정확하지 않으면 합계가 같아야 만 합계가 증가합니다. 하지만 지금은 항상 전체를 증가시키고 있습니다. 나는 내가 사용하는 논리가 맞다고 생각하지만 뭔가를 놓치고있다. 이클립스를 사용하고 있으며 프로그램이 컴파일되고 실행 중입니다. 문제는 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
오른쪽. 미안하지만, 나는 그것을 해결하기 위해 무엇을 해야할지 명확하지 않았습니다 ... 당신이 내 대답을 받아 들인 이후에 당신이 그것을 알아 냈다고 가정합니다. 그렇지 않은 경우 알려 주시면 자세히 안내해 드리겠습니다. – aioobe
시간을내어 고쳐 주셨습니다. – Kiril