gpt4 book ai didi

java - 无法更新 Swing 中的 JLabel

转载 作者:行者123 更新时间:2023-12-02 07:07:23 24 4
gpt4 key购买 nike

大家好,我对编程比较陌生(只是为了把它扔出去),我正在使用 Swing 做一个简单的数学测验。它旨在仅询问加法、减法、乘法和除法问题。我想做到这一点,以便当用户单击“检查答案”按钮时,ActionListener 更新标签(包含问题)以及问题本身。我设法更新问题本身,但 JLabel 根本没有更新。我尝试通过删除标签来更新标签,然后使用问题构造函数构建新的问题和标签,然后添加更新的标签和问题。我发现我无法删除然后将其添加回来。我只能添加一个全新的标签或者完全删除它。感谢您的帮助,希望不会太困惑!

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class MathQuiz extends JFrame
{

private int num1, num2, hardNum1, hardNum2, WIDTH = 400, HEIGHT = 150;
private double answer, input;
private int questionNum = 1, operatorSelector;
private String userInput;
private JLabel questionLabel;
private JPanel question;
private JTextField answerBox;
private JButton enter;
private String addition = "+", subtraction = "-", multiplication = "*", division = "/";
private int hard = 1, easy = 0, easiness = 0;
private int easinessSelector[] = {hard, easy};
private int[][] selector = new int[2][2];
private int easyNum = 0, hardNum = 1, number;

String[] operator = {addition, subtraction, multiplication, division};


/**Constructor
*
*/
public MathQuiz()
{
setTitle("Math quiz");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
answerBox = new JTextField(6);
add(answerBox, BorderLayout.NORTH);
questionPanelBuild();
questionConstructor();
enter = new JButton("Check answer");
enter.addActionListener(new ButtonListener());
add(enter, BorderLayout.SOUTH);
setResizable(false);
setSize(WIDTH, HEIGHT);
setVisible(true);

}

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
userInput = answerBox.getText();
input = Double.parseDouble(userInput);

if(input == answer)
{
questionNum +=1;
question.remove(questionLabel);
question.remove(question);
questionConstructor();
}
else if(input != answer)
{
JOptionPane.showMessageDialog(null, "Sorry, that's incorrect.");
}
}
}

private void questionPanelBuild()
{
question = new JPanel();
}

private void questionConstructor()
{
Random rand1 = new Random();
num1 = rand1.nextInt(101);
Random rand2 = new Random();
num2 = rand2.nextInt(101);
Random rand3 = new Random();
operatorSelector = rand3.nextInt(4);
Random rand4 = new Random();
hardNum1 = rand4.nextInt(21);
Random rand5 = new Random();
hardNum2 = rand5.nextInt(21);

if(operatorSelector == 0)
answer = num1 + num2;

if(operatorSelector == 1)
answer = num1 - num2;

if(operatorSelector == 2)
answer = hardNum1 * hardNum2;

if(operatorSelector == 3)
answer = hardNum1 / hardNum2;

selector[0][0] = num1;
selector[1][0] = num2;
selector[0][1] = hardNum1;
selector[1][1] = hardNum2;

if(operatorSelector == 0 || operatorSelector == 1)
easiness = easinessSelector[hardNum];
if(operatorSelector == 2 || operatorSelector == 3)
easiness = easinessSelector[easyNum];

questionLabel = new JLabel("What is: " + selector[0][easiness] + " " + operator[operatorSelector] + " " + selector[1][easiness] + "?");
questionLabel.setBorder(BorderFactory.createTitledBorder("Question " + questionNum));
questionLabel.setFont(new Font("Arial", Font.BOLD, 18));
question.add(questionLabel);
add(question, BorderLayout.CENTER);

}
public static void main(String[] args)
{
new MathQuiz();
}
}

最佳答案

添加问题JPanel后,您需要重新验证重新绘制

add(question, BorderLayout.CENTER);
revalidate();
repaint();

您可以简单地更新questionLabel:

questionLabel.setText(...);

关于java - 无法更新 Swing 中的 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15928576/

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