gpt4 book ai didi

java - 将值传递给 JLabel

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:10 24 4
gpt4 key购买 nike

大家好,我是堆栈,所以如果有人能给我帮助,那就太好了。因此,当我在 jtextfield 中输入某个值时,如果该值与 x * y 中的一个值相同,则它应该正确地增加,如果它们不相同,则应该只增加总数。但目前它总是在增加总数。我认为我使用的逻辑是正确的,但我遗漏了一些东西。我正在使用 eclipse,程序正在编译和运行。我想问题出在 actionPerformed 方法的 PanelQuizCountdown 类中。这是代码。

/**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");


}

}
}

最佳答案

但是您在获得输入结果之前正在更新预期结果:

生成新的随机因子:

        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++;
}

旁注:

if(result == check)
{
correct++;
total++;
}
else
{
total++;
}

可以表示为

total++;
if (result == check)
correct++;

关于java - 将值传递给 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9654359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com