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();
}
}
相當不錯的例子使用CardLayout – mKorbel 2013-04-10 15:20:09
THANK YOU!我不能相信我錯過了「setText」。完全忘記了這種方法 – user2052855 2013-04-10 15:22:27