随机生成两个10以内的随机数并得到这两个随机数的运算结果(这里指的运算是指加减运算),在输入框输入一个数 和 前面的两个随机数的运算结果进行判断是否相等,如果相等,在java图形用户界面 实现的窗口显示正确
import javax.swing.*;
public class MainWindow extends JFrame {
private MyPanel contentPane = new MyPanel();
public static void main(String[] args) {
new MainWindow();
}
public MainWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 300);
setVisible(true);
add(contentPane);
}
}
/**
* 用于生成随机数 和 生成两个随机数的和 和 输入的数比较
*/
public class ManagerNumber {
Number number = new Number();
static int result ;
public ManagerNumber() {
generateResult();
}
public int generateResult() {
number.setN1( (int) ((Math.random() * 10) + 1) );//1~10的随机整数
number.setN2((int) ((Math.random() * 10) + 1));
int temp = (int) ((Math.random() * 2));
//随机生成0/1 ,如果0 ,则是+ ,如果 1 则是 -
if (temp == 0) {
number.setSign("+");
result = number.getN1() + number.getN2();
}
if (temp == 1) {
number.setSign("-");
result = number.getN1() - number.getN2();
}
return result;
}
public Number getNumber(){
return number;
}
//判断 inputNumber和targetNumber是否相等
public static boolean IsEqual(String inputNumber){
return inputNumber.equals(result+"");
}
//获取两个随机数的运算结果
public static int getResult(){
return result;
}
}
public class Number {
private int n1;//随机数
private int n2;//随机数
private String sign;// + / - 符号
public Number() {
}
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
}
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MyPanel extends JPanel {
private JTextField txfInput1;
private ManagerNumber managerNumber = new ManagerNumber();
private Number number = managerNumber.getNumber();
private String info = " ";
private JLabel lblMessage1;
private JLabel lblMessage2;
private JLabel lblMessage3;
private JLabel lblMessage5;
public MyPanel() {
setBorder(new EmptyBorder(5, 5, 5, 5));
// setBounds(5,5,100,200);
setLayout(null);
//输入框的内容,用于和计算结果匹配
inputBox();
//初始显示生成随机数以及运算符号
genN1AndN2Sign(number.getN1(), number.getN2(), number.getSign());
JLabel lblMessage4 = new JLabel("=");
lblMessage4.setBounds(160, 40, 100, 20);
add(lblMessage4);
// lblMessage5 用于显示提示信息(如果正确,给出 表扬 ,错误,给出提示信息 和 正确答案)
genLabel();
JButton btnCalculate = new JButton("计算");
//给计算按钮添加一个监听器
btnCalculate.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//获取文本输入框的输入的内容
String text = txfInput1.getText();
//判断输入的是否和两个随机数运算的结果是否相等
boolean b = ManagerNumber.IsEqual(text);
//根据b来设置提示信息
if (b) {
info = "正确";
genLabel();
}
if (!b) {
info = "错误,正确答案是" + ManagerNumber.getResult();
genLabel();
}
//重绘
repaint();
}
});
btnCalculate.setBounds(100, 80, 80, 20);
add(btnCalculate);
JButton btnNext = new JButton("下一题");
//给下一题按钮添加监听器
btnNext.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//当鼠标点击‘下一题按钮’
//生成两个随机数和运算符号,并返回运算结果
int i = managerNumber.generateResult();
Number number = managerNumber.getNumber();
//将随机数置于面板随机数对应的JLabel
genN1AndN2Sign(number.getN1(), number.getN2(), number.getSign());
//置空文本输入框
inputBox();
//将info置为“”
info = "";
genLabel();
//重绘
repaint();
}
});
btnNext.setBounds(100, 120, 80, 20);
add(btnNext);
}
//生成提示信息标签
public void genLabel() {
if (!(lblMessage5 == null)) {
remove(lblMessage5);
}
lblMessage5 = new JLabel(info);
lblMessage5.setFont(new Font("微软雅黑",Font.BOLD,15));
lblMessage5.setBounds(230, 100, 200, 20);
add(lblMessage5);
}
//方法:实现将随机数以及运算符号 置于 面板中随机数和运算符号对应的JLabel
public void genN1AndN2Sign(int n1, int n2, String sign) {
//先清空上一次JLabel的内容
if (!(lblMessage1 == null && lblMessage2 == null && lblMessage3 == null)) {
remove(lblMessage1);
remove(lblMessage2);
remove(lblMessage3);
}
lblMessage1 = new JLabel(n1 + "");
lblMessage1.setFont(new Font("微软雅黑",Font.BOLD,15));
lblMessage1.setBounds(100, 40, 100, 20);
add(lblMessage1);
lblMessage2 = new JLabel(sign);
lblMessage2.setFont(new Font("微软雅黑",Font.BOLD,20));
lblMessage2.setBounds(120, 40, 100, 20);
add(lblMessage2);
lblMessage3 = new JLabel(n2 + "");
lblMessage3.setFont(new Font("微软雅黑",Font.BOLD,15));
lblMessage3.setBounds(140, 40, 100, 20);
add(lblMessage3);
}
//方法:置空文本输入框并绘制新文本输入框
public void inputBox() {
if (!(txfInput1 == null)) {
remove(txfInput1);
}
txfInput1 = new JTextField();
txfInput1.setBounds(180, 40, 70, 20);
txfInput1.setFont(new Font("微软雅黑",Font.BOLD,15));
add(txfInput1);
txfInput1.setColumns(10);
}
}
|