之前做了个流程版的,这里放最后的总结版
先讲功能实现:
1.整体程序是个100以内的四则运算出题器;
2.做了个选择菜单,你可以自由选择需要做加减乘除任意一项的四则运算练习;
3.增加了内层中的返回功能,使得运行过程中不必退出;
4.出题器中提供出题,检验答案和揭示答案功能。
下面是程序实现截图以及代码实现:
菜单面板:
package Live; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class SizeCaidan extends JFrame{ JFrame f = new JFrame("运算选择"); JPanel p = new JPanel(); public SizeCaidan() { JButton b1 = new JButton("加法练习"); JButton b2 = new JButton("减法练习"); JButton b3 = new JButton("乘法练习"); JButton b4 = new JButton("除法练习"); JButton ex = new JButton("退出"); b1.addActionListener(new Monitor2()); b2.addActionListener(new Monitor2()); b3.addActionListener(new Monitor2()); b4.addActionListener(new Monitor2()); ex.addActionListener(new Monitor2()); f.addWindowListener(new Wind()); f.add(p); p.setLayout(new GridLayout(1, 2)); Icon icon=new ImageIcon("src/Live/gdjssfdx.jpg");// JLabel label=new JLabel(icon); label.setBounds(0,0,400,350); f.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); JPanel jp = (JPanel)f.getContentPane(); jp.setOpaque(false);// JLabel l = new JLabel(); JPanel p2 = new JPanel(); p.add(l); p.add(p2); p2.setLayout(new GridLayout(5, 1)); p2.add(b1); p2.add(b2); p2.add(b3); p2.add(b4); p2.add(ex); p.setOpaque(false); f.setBounds(200, 200, 400, 400); f.setVisible(true); } public static void main(String[] args) { new SizeCaidan(); } private class Wind extends WindowAdapter { public void windowClosing(WindowEvent e) { f.setVisible(false); System.exit(0); } } private class Monitor2 implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("退出")) { //new SizeCaidan(); System.exit(0); } if (e.getActionCommand().equals("加法练习")) { new NumJframe(); f.setVisible(false); } if (e.getActionCommand().equals("减法练习")) { new NumJframe2(); f.setVisible(false); } if (e.getActionCommand().equals("乘法练习")) { new NumJframe3(); f.setVisible(false); } if (e.getActionCommand().equals("除法练习")) { new NumJframe4(); f.setVisible(false); } } } }
加法练习面板:
package Live; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; import Live.JiafaSum; public class Jia_1 { public static void main(String[] args) { new NumJframe(); } } class JiafaSum {// 定义全局变量(其实就是类变量,因为在JAVA中不存在全局变量的声明,但是类默认是static的, // 所以采用定义类变量的方法) public static int sum = 0; } class NumJframe extends JFrame { public NumJframe() { setLayout(null); Container con = getContentPane();// 创建一个内容面板容器 bank1();// 获得题目 bank2();// 确认答案 bank3();//返回 bank4();//揭示答案 setTitle("100以內的加法测试"); setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 setVisible(true);// 设置是否展示窗口 con.setBackground(Color.yellow); } void bank1() {// 获得题目按钮区 JTextField jt = new JTextField();// 文本框1 jt.setBounds(120, 40, 200, 30); JButton jb1 = new JButton();// 按钮1 jb1.setBounds(20, 40, 100, 30); jb1.setText("获得题目"); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int jiashu1 = (int) (Math.random() * 100 + 1); int jiashu2 = (int) (Math.random() * 100 + 1); String js1 = "" + jiashu1; String js2 = "" + jiashu2; JiafaSum.sum = jiashu1 + jiashu2; jt.setText(js1 + "+" + js2 + "=" + ""); } }); add(jt); add(jb1); } void bank2() {// 确认答案按钮区 JTextField jt2 = new JTextField();// 文本框2 jt2.setBounds(120, 75, 200, 30); JButton jb2 = new JButton();// 按钮2 jb2.setBounds(20, 75, 100, 30); jb2.setText("确认答案"); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int sum = JiafaSum.sum; String inputSum = jt2.getText(); //System.out.println("sum="+sum); //System.out.println("inputSum="+inputSum); if(inputSum == null) { System.out.println("你的输入有误,请重新输入一个整数:"); } int inputsum = Integer.parseInt(inputSum); if (inputsum == sum) { JOptionPane.showMessageDialog(null,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE); } } }); add(jt2); add(jb2); } void bank3() {//返回区 JButton jb3 = new JButton();// jb3.setBounds(0, 0, 100, 25); jb3.setText("返回"); jb3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); new SizeCaidan(); } }); add(jb3); } void bank4() {//输出答案区 JTextField jt4 = new JTextField();// 文本框1 jt4.setBounds(120, 110, 200, 30); JButton jb4 = new JButton();// 按钮1 jb4.setBounds(20, 110, 100, 30); jb4.setText("揭示答案"); jb4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jt4.setText(JiafaSum.sum+""); } }); add(jt4); add(jb4); } }
以加法为例,其他三则运算也类似,以下直接贴出代码
(PS:除法中因为变量提取为int型,所以除法结果也只保留整数部分)
package Live; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; public class Jian_1 { public static void main(String[] args) { new NumJframe2(); } } class JianfaSum {// 定义全局变量(其实就是类变量,因为在JAVA中不存在全局变量的声明,但是类默认是static的, // 所以采用定义类变量的方法) public static int sum2 = 0; } class NumJframe2 extends JFrame { public NumJframe2() { setLayout(null); Container con = getContentPane();// 创建一个内容面板容器 bank1();// 获得题目 bank2();// 确认答案 bank3(); bank4(); setTitle("100以內的减法测试"); setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 setVisible(true);// 设置是否展示窗口 con.setBackground(Color.cyan); } void bank1() {// 获得题目按钮区 JTextField jt = new JTextField();// 文本框1 jt.setBounds(120, 40, 200, 30); JButton jb1 = new JButton();// 按钮1 jb1.setBounds(20, 40, 100, 30); jb1.setText("获得题目"); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int jianshu1 = (int) (Math.random() * 100 + 1); int jianshu2 = (int) (Math.random() * 100 + 1); String js1 = "" + jianshu1; String js2 = "" + jianshu2; JianfaSum.sum2 = jianshu1 - jianshu2; jt.setText(js1 + "-" + js2 + "=" + ""); } }); add(jt); add(jb1); } void bank2() {// 确认答案按钮区 JTextField jt2 = new JTextField();// 文本框2 jt2.setBounds(120, 75, 200, 30); JButton jb2 = new JButton();// 按钮1 jb2.setBounds(20, 75, 100, 30); jb2.setText("确认答案"); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int sum = JianfaSum.sum2; String inputSum = jt2.getText(); // System.out.println("sum="+sum); // System.out.println("inputSum="+inputSum); if (inputSum == null) { System.out.println("你的输入有误,请重新输入一个整数:"); } int inputsum = Integer.parseInt(inputSum); if (inputsum == sum) { JOptionPane.showMessageDialog(null, "答案正确", "消息对话框", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "答案错误", "消息对话框", JOptionPane.WARNING_MESSAGE); } } }); add(jt2); add(jb2); } void bank3() {//返回区 JButton jb3 = new JButton();// jb3.setBounds(0, 0, 100, 25); jb3.setText("返回"); jb3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); new SizeCaidan(); } }); add(jb3); } void bank4() {//输出答案区 JTextField jt4 = new JTextField();// 文本框1 jt4.setBounds(120, 110, 200, 30); JButton jb4 = new JButton();// 按钮1 jb4.setBounds(20, 110, 100, 30); jb4.setText("揭示答案"); jb4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jt4.setText(JianfaSum.sum2+""); } }); add(jt4); add(jb4); } }
package Live; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; public class Cheng { public static void main(String[] args) { new NumJframe3(); } } class ChengfaSum {// 定义全局变量(其实就是类变量,因为在JAVA中不存在全局变量的声明,但是类默认是static的, // 所以采用定义类变量的方法) public static int sum3 = 0; } class NumJframe3 extends JFrame { public NumJframe3() { setLayout(null); Container con = getContentPane();// 创建一个内容面板容器 bank1();// 获得题目 bank2();// 确认答案 bank3(); bank4(); setTitle("100以內的乘法测试"); setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 setVisible(true);// 设置是否展示窗口 con.setBackground(Color.darkGray); } void bank1() {// 获得题目按钮区 JTextField jt = new JTextField();// 文本框1 jt.setBounds(120, 40, 200, 30); JButton jb1 = new JButton();// 按钮1 jb1.setBounds(20, 40, 100, 30); jb1.setText("获得题目"); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int jiashu1 = (int) (Math.random() * 100 + 1); int jiashu2 = (int) (Math.random() * 100 + 1); String js1 = "" + jiashu1; String js2 = "" + jiashu2; ChengfaSum.sum3 = jiashu1 * jiashu2; jt.setText(js1 + "X" + js2 + "=" + ""); } }); add(jt); add(jb1); } void bank2() {// 确认答案按钮区 JTextField jt2 = new JTextField();// 文本框2 jt2.setBounds(120, 75, 200, 30); JButton jb2 = new JButton();// 按钮1 jb2.setBounds(20, 75, 100, 30); jb2.setText("确认答案"); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int sum = ChengfaSum.sum3; String inputSum = jt2.getText(); //System.out.println("sum="+sum); //System.out.println("inputSum="+inputSum); if(inputSum == null) { System.out.println("你的输入有误,请重新输入一个整数:"); } int inputsum = Integer.parseInt(inputSum); if (inputsum == sum) { JOptionPane.showMessageDialog(null,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE); } } }); add(jt2); add(jb2); } void bank3() {//返回区 JButton jb3 = new JButton();// jb3.setBounds(0, 0, 100, 25); jb3.setText("返回"); jb3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); new SizeCaidan(); } }); add(jb3); } void bank4() {//输出答案区 JTextField jt4 = new JTextField();// 文本框1 jt4.setBounds(120, 110, 200, 30); JButton jb4 = new JButton();// 按钮1 jb4.setBounds(20, 110, 100, 30); jb4.setText("揭示答案"); jb4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jt4.setText(ChengfaSum.sum3+""); } }); add(jt4); add(jb4); } }
package Live; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; public class Chu_1 { public static void main(String[] args) { new NumJframe4(); } } class ChufaSum {// 定义全局变量(其实就是类变量,因为在JAVA中不存在全局变量的声明,但是类默认是static的, // 所以采用定义类变量的方法) public static int sum4 = 0; } class NumJframe4 extends JFrame { public NumJframe4() { setLayout(null); Container con = getContentPane();// 创建一个内容面板容器 bank1();// 获得题目 bank2();// 确认答案 bank3(); bank4(); setTitle("100以內的除法测试"); setBounds(200, 200, 600, 300);// 设置出现位置和窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭方式 setVisible(true);// 设置是否展示窗口 con.setBackground(Color.orange); } void bank1() {// 获得题目按钮区 JTextField jt = new JTextField();// 文本框1 jt.setBounds(120, 40, 200, 30); JButton jb1 = new JButton();// 按钮1 jb1.setBounds(20, 40, 100, 30); jb1.setText("获得题目"); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int jiashu1 = (int) (Math.random() * 100 + 1); int jiashu2 = (int) (Math.random() * 100 + 1); String js1 = "" + jiashu1; String js2 = "" + jiashu2; ChufaSum.sum4 = jiashu1 / jiashu2; jt.setText(js1 + "÷" + js2 + "=" + ""); } }); add(jt); add(jb1); } void bank2() {// 确认答案按钮区 JTextField jt2 = new JTextField();// 文本框2 jt2.setBounds(120, 75, 200, 30); JButton jb2 = new JButton();// 按钮1 jb2.setBounds(20, 75, 100, 30); jb2.setText("确认答案"); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int sum = ChufaSum.sum4; String inputSum = jt2.getText(); //System.out.println("sum="+sum); //System.out.println("inputSum="+inputSum); if(inputSum == null) { System.out.println("你的输入有误,请重新输入一个整数:"); } int inputsum = Integer.parseInt(inputSum); if (inputsum == sum) { JOptionPane.showMessageDialog(null,"答案正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null,"答案错误","消息对话框",JOptionPane.WARNING_MESSAGE); } } }); add(jt2); add(jb2); } void bank3() {//返回区 JButton jb3 = new JButton();// jb3.setBounds(0, 0, 100, 25); jb3.setText("返回"); jb3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); new SizeCaidan(); } }); add(jb3); } void bank4() {//输出答案区 JTextField jt4 = new JTextField();// 文本框1 jt4.setBounds(120, 110, 200, 30); JButton jb4 = new JButton();// 按钮1 jb4.setBounds(20, 110, 100, 30); jb4.setText("揭示答案"); jb4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jt4.setText(ChufaSum.sum4+""); } }); add(jt4); add(jb4); } }
以上即为该四则运算的最终代码版。
牢骚时间:
(PS:你看10000遍和她的聊天记录,她不是你的,你看100遍书,知识就是你的,该醒醒了,我们的目标是建设祖国!)