无法在Swing中更新JLabel

时间:2013-04-10 14:23:48

标签: java swing jpanel jlabel

大家好,我对编程比较陌生(只是把它扔出去)而且我正在使用Swing做一个简单的数学测验。它旨在仅询问加法,减法,乘法和除法问题。我想这样做,以便当用户单击“检查答案”按钮时,ActionListener更新Label(包含问题)和问题本身。我设法更新了问题本身,但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();
    }
}

1 个答案:

答案 0 :(得分:2)

您需要revalidate&添加问题repaint

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

您只需更新questionLabel

questionLabel.setText(...);