如何在 JLabel 中显示和计算随机数?

How to display and calculate random numbers in a JLabel?

我正在尝试创建一个程序,我可以在其中计算 2 个随机生成的数字并将它们显示在 JLabel 中。该程序成功地在 JLabel 中显示随机数,但它只计算已生成的第一组数字。

我希望它能够计算所有随机生成的数字集。

Random dice = new Random();
boolean solution;
JLabel num1,num2,checker;
int calculation;

public void repeatLoop() {
    switch(calculation) {
    case 1:
        int addition1 = Integer.valueOf(num1.getText()) + Integer.valueOf(num2.getText());
        break;
    case 2:
        num1.setText("");
        num2.setText("");
        break;
    case 3:
        num1.setText(String.valueOf(1 + dice.nextInt(6)));
        num2.setText(String.valueOf(1 + dice.nextInt(6)));
        break;
    }
}

public JLabel checkerLabel() {
    JLabel checker = new JLabel("");
    checker.setFont(new Font("Tahoma", Font.BOLD, 13));
    checker.setForeground(Color.GREEN);
    checker.setHorizontalAlignment(SwingConstants.CENTER);
    checker.setBounds(163, 21, 109, 23);
    contentPane.add(checker);
    return checker;
}

public addFrame() {
    JLabel num1 = new JLabel("3");
    num1.setHorizontalAlignment(SwingConstants.CENTER);
    num1.setFont(new Font("Tahoma", Font.PLAIN, 20));
    num1.setBounds(122, 101, 63, 32);
    contentPane.add(num1);

    JLabel num2 = new JLabel("4");
    num2.setHorizontalAlignment(SwingConstants.CENTER);
    num2.setFont(new Font("Tahoma", Font.PLAIN, 20));
    num2.setBounds(268, 101, 63, 32);
    contentPane.add(num2);

    this.num1 = num1;
    this.num2 = num2;
    calculation = 3;
    int addition1 = Integer.valueOf(num1.getText()) + Integer.valueOf(num2.getText());
    JLabel addOperation = new JLabel("+");
    addOperation.setHorizontalAlignment(SwingConstants.CENTER);
    addOperation.setFont(new Font("Tahoma", Font.PLAIN, 20));
    addOperation.setBounds(195, 101, 63, 32);
    contentPane.add(addOperation);

    this.checker = checkerLabel();

    textField1 = new JTextField();
    textField1.setHorizontalAlignment(SwingConstants.CENTER);
    textField1.setBounds(163, 155, 120, 32);
    contentPane.add(textField1);
    textField1.setColumns(10);

    JButton checkBtn1 = new JButton("Check");
    checkBtn1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            final int addition2 = addition1;
            int ansUser = Integer.parseInt(textField1.getText());
            if(addition2 == ansUser) {
                solution = true;
            } else {
                solution = false;
            }
            if(solution == true) {
                checker.setText("CORRECT");
                checker.setForeground(Color.GREEN);
            } else {
                checker.setText("WRONG");
                checker.setForeground(Color.RED);
            }
            textField1.setText("");
            calculation = 1;
            calculation = 2;
            repeatLoop();
        }
    });
}

也许您应该在 switch(calculation) 结构中的每个案例之后添加 break

尝试将switch语句修改为:

switch(calculation) {
   case 1:
        int addition1 = Integer.valueOf(num1.getText()) + Integer.valueOf(num2.getText());
        break;
   case 2:
        num1.setText("");
        num2.setText("");
        break;
   case 3:
        num1.setText(String.valueOf(1 + dice.nextInt(6)));
        num2.setText(String.valueOf(1 + dice.nextInt(6)));
        break;
}

您的代码中存在一些逻辑错误。我相信我已经在下面的代码中修复了它们。

我建议您 运行 下面的代码以确保它按照您想要的方式运行。如果是,则将其与您的代码进行比较以查看差异。没有很多。这是一个摘要:

  1. 我让 addition1 成为了 class 会员。
  2. 我在方法 repeatLoopswitch 语句中添加了一个 break 语句。
  3. 我更改了方法的最后几行actionPerformed

最后,我建议你运行你的原始代码用调试器看看为什么你的代码有逻辑问题。

这是代码。

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class addFrame extends JFrame {

    private JPanel contentPane;
    private JTextField textField1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    addFrame frame = new addFrame();
                    frame.setVisible(true);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    Random dice = new Random();
    boolean solution;
    JLabel num1, num2, checker;
    int calculation;
    int addition1;

    public void repeatLoop() {
        switch (calculation) {
            case 1:
                addition1 = Integer.valueOf(num1.getText()) + Integer.valueOf(num2.getText());
                break;
            case 2:
                num1.setText("");
                num2.setText("");
            case 3:
                num1.setText(String.valueOf(1 + dice.nextInt(6)));
                num2.setText(String.valueOf(1 + dice.nextInt(6)));
        }
    }

    public JLabel checkerLabel() {
        JLabel checker = new JLabel("");
        checker.setFont(new Font("Tahoma", Font.BOLD, 13));
        checker.setForeground(Color.GREEN);
        checker.setHorizontalAlignment(SwingConstants.CENTER);
        checker.setBounds(163, 21, 109, 23);
        contentPane.add(checker);
        return checker;
    }

    public addFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel title3 = new JLabel("ADDITION");
        title3.setForeground(Color.RED);
        title3.setFont(new Font("Verdana", Font.PLAIN, 30));
        title3.setHorizontalAlignment(SwingConstants.CENTER);
        title3.setBounds(108, 47, 224, 32);
        contentPane.add(title3);

        JLabel num1 = new JLabel("3");
        num1.setHorizontalAlignment(SwingConstants.CENTER);
        num1.setFont(new Font("Tahoma", Font.PLAIN, 20));
        num1.setBounds(122, 101, 63, 32);
        contentPane.add(num1);

        JLabel num2 = new JLabel("4");
        num2.setHorizontalAlignment(SwingConstants.CENTER);
        num2.setFont(new Font("Tahoma", Font.PLAIN, 20));
        num2.setBounds(268, 101, 63, 32);
        contentPane.add(num2);

        this.num1 = num1;
        this.num2 = num2;
        calculation = 3;
        addition1 = Integer.valueOf(num1.getText()) + Integer.valueOf(num2.getText());
        JLabel addOperation = new JLabel("+");
        addOperation.setHorizontalAlignment(SwingConstants.CENTER);
        addOperation.setFont(new Font("Tahoma", Font.PLAIN, 20));
        addOperation.setBounds(195, 101, 63, 32);
        contentPane.add(addOperation);

        this.checker = checkerLabel();

        textField1 = new JTextField();
        textField1.setHorizontalAlignment(SwingConstants.CENTER);
        textField1.setBounds(163, 155, 120, 32);
        contentPane.add(textField1);
        textField1.setColumns(10);

        JButton checkBtn1 = new JButton("Check");
        checkBtn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                final int addition2 = addition1;
                int ansUser = Integer.parseInt(textField1.getText());
                if (addition2 == ansUser) {
                    solution = true;
                }
                else {
                    solution = false;
                }
                if (solution == true) {
                    checker.setText("CORRECT");
                    checker.setForeground(Color.GREEN);
                }
                else {
                    checker.setText("WRONG");
                    checker.setForeground(Color.RED);
                }
                textField1.setText("");
                calculation = 2;
                repeatLoop();
                calculation = 1;
                repeatLoop();
            }
        });
        checkBtn1.setBounds(183, 211, 89, 23);
        contentPane.add(checkBtn1);
        setLocationRelativeTo(null);
    }
}