初步代码:
1 package sy11; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import java.awt.event.*; 6 7 public class JiaFa { 8 public static void main(String[] args) { 9 new NumJframe(); 10 } 11 } 12 13 class JiafaSum {// 定义全局变量 14 public static int sum = 0; 15 } 16 17 class NumJframe extends JFrame { 18 NumJframe() { 19 setLayout(null); 20 Container con = getContentPane();// 创建一个内容面板容器 21 bank1();// 获得题目 22 bank2();// 确认答案 23 setTitle("100以內的加法测试"); 24 setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 25 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 26 setVisible(true);// 设置是否展示窗口 27 con.setBackground(Color.lightGray); 28 } 29 30 void bank1() {// 获得题目按钮区 31 JTextField jt = new JTextField();// 文本框1 32 jt.setBounds(120, 40, 200, 30); 33 JButton jb1 = new JButton();// 按钮1 34 jb1.setBounds(20, 40, 100, 30); 35 jb1.setText("获得题目"); 36 jb1.addActionListener(new ActionListener() { 37 38 public void actionPerformed(ActionEvent e) { 39 40 int jiashu1 = (int) (Math.random() * 100 + 1); 41 int jiashu2 = (int) (Math.random() * 100 + 1); 42 String js1 = "" + jiashu1; 43 String js2 = "" + jiashu2; 44 JiafaSum.sum = jiashu1 + jiashu2; 45 jt.setText(js1 + "+" + js2 + "=" + ""); 46 } 47 }); 48 add(jt); 49 add(jb1); 50 } 51 52 void bank2() {// 确认答案按钮区 53 JTextField jt2 = new JTextField();// 文本框2 54 jt2.setBounds(120, 75, 200, 30); 55 JButton jb2 = new JButton();// 按钮1 56 jb2.setBounds(20, 75, 100, 30); 57 jb2.setText("确认答案"); 58 jb2.addActionListener(new ActionListener() { 59 public void actionPerformed(ActionEvent e) { 60 int sum = JiafaSum.sum; 61 String inputSum = jt2.getText(); 62 int inputsum = Integer.parseInt(inputSum); 63 System.out.println("sum=" + sum); 64 System.out.println("inputSum=" + inputSum); 65 if (inputsum == sum) { 66 67 JOptionPane.showMessageDialog(null, "答案正确", "消息对话框", JOptionPane.INFORMATION_MESSAGE); 68 } else { 69 JOptionPane.showMessageDialog(null, "答案错误", "消息对话框", JOptionPane.WARNING_MESSAGE); 70 } 71 } 72 }); 73 add(jt2); 74 add(jb2); 75 } 76 }
运行结果:
1 package sy11; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import java.awt.event.*; 6 7 public class JianFa { 8 public static void main(String[] args) { 9 new jianfaJframe(); 10 } 11 } 12 13 class jianfaDiff {// 定义全局变量 14 public static int diff = 0; 15 } 16 17 class jianfaJframe extends JFrame { 18 jianfaJframe() { 19 setLayout(null); 20 Container con = getContentPane();// 创建一个内容面板容器 21 bank1();// 获得题目 22 bank2();// 确认答案 23 setTitle("100以內的减法测试"); 24 setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 25 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 26 setVisible(true);// 设置是否展示窗口 27 con.setBackground(Color.lightGray); 28 } 29 30 void bank1() {// 获得题目按钮区 31 JTextField jt = new JTextField();// 文本框1 32 jt.setBounds(120, 40, 200, 30); 33 JButton jb1 = new JButton();// 按钮1 34 jb1.setBounds(20, 40, 100, 30); 35 jb1.setText("获得题目"); 36 jb1.addActionListener(new ActionListener() { 37 38 public void actionPerformed(ActionEvent e) { 39 40 int beijianshu = (int) (Math.random() * 100 + 1); 41 int jianshu = (int) (Math.random() * beijianshu + 1); 42 String bjs = "" + beijianshu; 43 String js = "" + jianshu; 44 jianfaDiff.diff = beijianshu - jianshu; 45 jt.setText(bjs + "-" + js + "=" + ""); 46 } 47 }); 48 add(jt); 49 add(jb1); 50 } 51 52 void bank2() {// 确认答案按钮区 53 JTextField jt2 = new JTextField();// 文本框2 54 jt2.setBounds(120, 75, 200, 30); 55 JButton jb2 = new JButton();// 按钮1 56 jb2.setBounds(20, 75, 100, 30); 57 jb2.setText("确认答案"); 58 jb2.addActionListener(new ActionListener() { 59 public void actionPerformed(ActionEvent e) { 60 int diff = jianfaDiff.diff; 61 String inputDiff = jt2.getText(); 62 int inputdiff = Integer.parseInt(inputDiff); 63 System.out.println("diff=" + diff); 64 System.out.println("inputSum=" + inputDiff); 65 if (inputdiff == diff) { 66 67 JOptionPane.showMessageDialog(null, "答案正确", "消息对话框", JOptionPane.INFORMATION_MESSAGE); 68 } 69 else { 70 JOptionPane.showMessageDialog(null, "答案错误", "消息对话框", JOptionPane.WARNING_MESSAGE); 71 } 72 } 73 }); 74 add(jt2); 75 add(jb2); 76 } 77 }
结果:
1 package sy11; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import java.awt.event.*; 6 7 public class ChengFa { 8 public static void main(String[] args) { 9 new MulJframe(); 10 } 11 } 12 13 class ChengfaMul {// 定义全局变量 14 public static int mul = 0; 15 } 16 17 class MulJframe extends JFrame { 18 MulJframe() { 19 setLayout(null); 20 Container con = getContentPane();// 创建一个内容面板容器 21 bank1();// 获得题目 22 bank2();// 确认答案 23 setTitle("100以內的乘法测试"); 24 setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 25 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 26 setVisible(true);// 设置是否展示窗口 27 con.setBackground(Color.lightGray); 28 } 29 30 void bank1() {// 获得题目按钮区 31 JTextField jt = new JTextField();// 文本框1 32 jt.setBounds(120, 40, 200, 30); 33 JButton jb1 = new JButton();// 按钮1 34 jb1.setBounds(20, 40, 100, 30); 35 jb1.setText("获得题目"); 36 jb1.addActionListener(new ActionListener() { 37 38 public void actionPerformed(ActionEvent e) { 39 40 int chengshu1 = (int) (Math.random() * 100 + 1); 41 int chengshu2 = (int) (Math.random() * 100 + 1); 42 String cs1 = "" + chengshu1; 43 String cs2 = "" + chengshu2; 44 ChengfaMul.mul = chengshu1 * chengshu2; 45 jt.setText(cs1 + "X" + cs2 + "=" + ""); 46 } 47 }); 48 add(jt); 49 add(jb1); 50 } 51 52 void bank2() {// 确认答案按钮区 53 JTextField jt2 = new JTextField();// 文本框2 54 jt2.setBounds(120, 75, 200, 30); 55 JButton jb2 = new JButton();// 按钮1 56 jb2.setBounds(20, 75, 100, 30); 57 jb2.setText("确认答案"); 58 jb2.addActionListener(new ActionListener() { 59 public void actionPerformed(ActionEvent e) { 60 int mul = ChengfaMul.mul; 61 String inputMul = jt2.getText(); 62 int inputmul = Integer.parseInt(inputMul); 63 System.out.println("mul=" + mul); 64 System.out.println("inputMul=" + inputMul); 65 if (inputmul == mul) { 66 67 JOptionPane.showMessageDialog(null, "答案正确", "消息对话框", JOptionPane.INFORMATION_MESSAGE); 68 } 69 else { 70 JOptionPane.showMessageDialog(null, "答案错误", "消息对话框", JOptionPane.WARNING_MESSAGE); 71 } 72 } 73 }); 74 add(jt2); 75 add(jb2); 76 } 77 }
结果:
1 package sy11; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import java.awt.event.*; 6 7 public class ChuFa { 8 public static void main(String[] args) { 9 new ChuFaJframe(); 10 } 11 } 12 13 class ChufaDiv {// 定义全局变量 14 public static int division = 0; 15 } 16 17 class ChuFaJframe extends JFrame { 18 ChuFaJframe() { 19 setLayout(null); 20 Container con = getContentPane();// 创建一个内容面板容器 21 bank1();// 获得题目 22 bank2();// 确认答案 23 setTitle("100以內的除法测试"); 24 setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 25 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 26 setVisible(true);// 设置是否展示窗口 27 con.setBackground(Color.lightGray); 28 } 29 30 void bank1() {// 获得题目按钮区 31 JTextField jt = new JTextField();// 文本框1 32 jt.setBounds(120, 40, 200, 30); 33 JButton jb1 = new JButton();// 按钮1 34 jb1.setBounds(20, 40, 100, 30); 35 jb1.setText("获得题目"); 36 jb1.addActionListener(new ActionListener() { 37 38 public void actionPerformed(ActionEvent e) { 39 40 int beichushu = (int) (Math.random() * 100 + 1); 41 int chushu = (int) (Math.random() * beichushu + 1); 42 String js1 = "" + beichushu; 43 String js2 = "" + chushu; 44 ChufaDiv.division = beichushu / chushu; 45 jt.setText(js1 + "➗" + js2 + "=" + ""); 46 } 47 }); 48 add(jt); 49 add(jb1); 50 } 51 52 void bank2() {// 确认答案按钮区 53 JTextField jt2 = new JTextField();// 文本框2 54 jt2.setBounds(120, 75, 200, 30); 55 JButton jb2 = new JButton();// 按钮1 56 jb2.setBounds(20, 75, 100, 30); 57 jb2.setText("确认答案"); 58 jb2.addActionListener(new ActionListener() { 59 public void actionPerformed(ActionEvent e) { 60 int div = ChufaDiv.division; 61 String inputDiv = jt2.getText(); 62 int inputdiv = Integer.parseInt(inputDiv); 63 System.out.println("division=" + div); 64 System.out.println("inputDiv=" + inputDiv); 65 if (inputdiv == div) { 66 67 JOptionPane.showMessageDialog(null, "答案正确", "消息对话框", JOptionPane.INFORMATION_MESSAGE); 68 } 69 else { 70 JOptionPane.showMessageDialog(null, "答案错误", "消息对话框", JOptionPane.WARNING_MESSAGE); 71 } 72 } 73 }); 74 add(jt2); 75 add(jb2); 76 } 77 }
结果:
后续会更新完善四则运算功能~~