如何在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();
}
});
}

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

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

  1. 我让addition1成为班级成员
  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);
}
}

也许您应该在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;
}

最新更新