2013-04-10 57 views
0

嗨,大家好,我对编程比较陌生(只是为了抛出它),我正在使用Swing进行简单的数学测验。它旨在仅询问加法,减法,乘法和除法问题。我想这样做是为了当用户单击“检查答案”按钮时,ActionListener更新标签(包含问题)和问题本身。我设法更新了问题本身,但JLabel根本没有得到更新。我试图通过删除它来更新标签,然后使用问题构造器来构建新的问题和标签,然后添加更新后的标签和问题。我发现我无法删除,然后将其添加回来。我只能添加一个全新的标签或完全删除它。感谢您的帮助,我希望它不会太杂乱!无法在Swing中更新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(); 
    } 
} 

回答

2

您需要revalidate & repaint增加的问题JPanel

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

你可以简单地更新questionLabel代替:

questionLabel.setText(...); 
+0

相当不错的例子使用CardLayout – mKorbel 2013-04-10 15:20:09

+0

THANK YOU!我不能相信我错过了“setText”。完全忘记了这种方法 – user2052855 2013-04-10 15:22:27