源代码:
package zuoye2; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; class jiemian { public jiemian() { JFrame jf=new JFrame("四则运算"); jf.setBounds(200, 100, 500, 500); jf.setLayout(null); Container c=jf.getContentPane(); JCheckBox j1=new JCheckBox("有乘除法"); JCheckBox j2=new JCheckBox("有括号"); JCheckBox j3=new JCheckBox("有负数"); JCheckBox j4=new JCheckBox("有余数"); JLabel l1=new JLabel("请输入数值范围"); JLabel l4=new JLabel("请输入参数个数(1—10)"); JLabel l2=new JLabel("—"); JLabel l3=new JLabel("请输入题目数目(1-30)"); JTextField t1=new JTextField("2"); JTextField t2=new JTextField("20"); JTextField t3=new JTextField("30"); JTextField t4=new JTextField("30"); j1.setBounds(100, 100, 80,20); j2.setBounds(100, 130, 80,20); l4.setBounds(100, 160, 180,20); j3.setBounds(100, 220, 80,20); j4.setBounds(100, 250, 80,20); t1.setBounds(100, 190, 50,20); l1.setBounds(100, 280, 150,20); t2.setBounds(100, 310, 50,20); t3.setBounds(180, 310, 50,20); l2.setBounds(160, 310, 30,20); l3.setBounds(100, 340, 150,20); t4.setBounds(100, 370, 50,20); c.add(j1); c.add(j2); c.add(j3); c.add(l4); c.add(j4); c.add(t1); c.add(l1); c.add(t2); c.add(t3); c.add(l2); c.add(l3); c.add(t4); JButton b1=new JButton("确定"); JButton b2=new JButton("退出"); b1.setBounds(350, 150,70, 50); b2.setBounds(350, 300,70, 50); c.add(b1); c.add(b2); b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jf.dispose(); String a1,a2,a3,a4,a5; int c1,c2,c3,c4,c5; boolean f1=false,f2=false,f3=false,f4=false,f5=false; a1=t1.getText().toString(); a2=t2.getText().toString(); a3=t3.getText().toString(); a4=t4.getText().toString(); if(a2.equals("")||a3.equals("")) { JOptionPane.showMessageDialog(null, "范围不能为空", "范围不能为空", JOptionPane.ERROR_MESSAGE); System.exit(0); } else if(a4.equals("")) { JOptionPane.showMessageDialog(null, "数目不能为空", "数目不能为空", JOptionPane.ERROR_MESSAGE); System.exit(0); } else if (a1.equals("")) { JOptionPane.showMessageDialog(null, "参数个数不能为空", "参数个数不能为空", JOptionPane.ERROR_MESSAGE); System.exit(0); } else if(Integer.parseInt(a1)>10||Integer.parseInt(a1)<=0) { JOptionPane.showMessageDialog(null, "参数个数超出", "参数个数超出", JOptionPane.ERROR_MESSAGE); System.exit(0); } else if(Integer.parseInt(a4)<=0||Integer.parseInt(a4)>30) { JOptionPane.showMessageDialog(null, "题目数目不正确", "题目数目不正确", JOptionPane.ERROR_MESSAGE); System.exit(0); } else if(Integer.parseInt(a3)<=Integer.parseInt(a2)) { JOptionPane.showMessageDialog(null, "范围不正确", "范围不正确", JOptionPane.ERROR_MESSAGE); System.exit(0); } if(j1.isSelected()) { f1=true; } if(j2.isSelected()) { f2=true; } if(j3.isSelected()) { f3=true; } if(j4.isSelected()) { f4=true; } c1=Integer.parseInt(a1); c2=Integer.parseInt(a2); c3=Integer.parseInt(a3); c4=Integer.parseInt(a4); yunsuan a=new yunsuan(c1,c2,c3,c4,f1,f2,f3,f4); } }); b2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jf.dispose(); } }); jf.setVisible(true); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } } package zuoye2; public class m extends jiemian { public static void main(String[] args) { // TODO Auto-generated method stub jiemian a=new jiemian(); } } package zuoye2; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.WindowConstants; public class yunsuan implements Runnable { JFrame jf=new JFrame("四则运算"); Container c=jf.getContentPane(); int i1,i2,i3,i4,anwser; boolean j1,j2,j3,j4; JTextField j5; Thread t; protected void ks() { t=new Thread(this); } public yunsuan(int c1, int c2, int c3, int c4, boolean f1, boolean f2, boolean f3, boolean f4) { i1=c1; i2=c2; i3=c3; i4=c4; j1=f1; j2=f2; j3=f3; j4=f4; JButton bu1=new JButton("提交"); JButton bu2=new JButton("退出"); bu1.setBounds(20, 100, 60, 20); bu2.setBounds(100, 100, 60, 20); c.add(bu1); c.add(bu2); ks(); t.start(); bu1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String qs; qs=j5.getText().toString(); if(qs.equals("")) { JOptionPane.showMessageDialog(null, "答案为空", "答案为空", JOptionPane.ERROR_MESSAGE); } else if(anwser==Integer.parseInt(qs)) { t.stop(); ks(); c.removeAll(); bu1.setBounds(20, 100, 60, 20); bu2.setBounds(100, 100, 60, 20); c.add(bu1); c.add(bu2); t.start(); c.repaint(); jf.setVisible(true); } else { JOptionPane.showMessageDialog(null, "答案错误", "答案错误", JOptionPane.ERROR_MESSAGE); } } }); bu2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jf.dispose(); } }); jf.setBounds(250, 200, c1*150+100, 200); jf.setLayout(null); jf.setVisible(true); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } @Override public void run() { int c1,c2,c3,c4; boolean f1,f2,f3,f4; //Container c=jf.getContentPane(); c1=i1; c2=i2; c3=i3; c4=i4; f1=j1; f2=j2; f3=j3; f4=j4; int a,b,d = 0,e; String a1,b1,d1; JLabel j1,j2,j3,j4,j7; j1=new JLabel(); j2=new JLabel(); j3=new JLabel(); j4=new JLabel(); j7=new JLabel(); JTextField j6; j5=new JTextField(); j6=new JTextField(); Random r1[]=new Random[2]; Random r2=new Random(); int i,j=0,k=0,l; j5=new JTextField(); j6=new JTextField(); for(i=0;i<2;i++) { r1[i]=new Random(); } j5=new JTextField(); if(f1==true) { a=c3-c2; b=c3-c2; a=r1[0].nextInt(a); b=r1[1].nextInt(b); a=a+c2; b=b+c2; e=r2.nextInt(4); } else { a=c3-c2; b=c3-c2; a=r1[0].nextInt(a); b=r1[1].nextInt(b); a=a+c2; b=b+c2; e=r2.nextInt(2); } e=e+1; if(f3!=false) { if(e==1) { a=r1[0].nextInt(c3-c2)+c2; b=r1[1].nextInt(c3-c2)+c2; d=a+b; a1=String.valueOf(a); b1=String.valueOf(b); d1=String.valueOf(d); j1=new JLabel(a1); j2=new JLabel("+"); j3=new JLabel(b1); j4=new JLabel("="); } if(e==2) { a=r1[0].nextInt(c3-c2)+c2; b=r1[1].nextInt(c3-c2)+c2; d=a-b; a1=String.valueOf(a); b1=String.valueOf(b); d1=String.valueOf(d); j1=new JLabel(a1); j2=new JLabel("-"); j3=new JLabel(b1); j4=new JLabel("="); } } else { if(e==1) { a=r1[0].nextInt(c3-c2)+c2; b=r1[1].nextInt(c3-c2)+c2; d=a+b; a1=String.valueOf(a); b1=String.valueOf(b); d1=String.valueOf(d); j1=new JLabel(a1); j2=new JLabel("+"); j3=new JLabel(b1); j4=new JLabel("="); } if(e==2) { int x; a=r1[0].nextInt(c3-c2)+c2; b=r1[1].nextInt(c3-c2)+c2; if(b>a) { x=b; b=a; a=x; } d=a-b; a1=String.valueOf(a); b1=String.valueOf(b); d1=String.valueOf(d); j1=new JLabel(a1); j2=new JLabel("-"); j3=new JLabel(b1); j4=new JLabel("="); } } if(e==3) { a=r1[0].nextInt(c3-c2)+c2; b=r2.nextInt(c3-c2)+c2; d=a*b; a1=String.valueOf(a); b1=String.valueOf(b); d1=String.valueOf(d); j1=new JLabel(a1); j2=new JLabel("X"); j3=new JLabel(b1); j4=new JLabel("="); } if(f4==false) { if(e==4) { d=r1[0].nextInt(c3-c2)+c2; b=r1[1].nextInt(c3-c2)+c2; a=b*d; while(d==0||b==0) { r1[0]=new Random(); r1[1]=new Random(); d=r1[0].nextInt(20); b=r1[1].nextInt(20); a=b*d; } a1=String.valueOf(a); b1=String.valueOf(b); d1=String.valueOf(d); j1=new JLabel(a1); j2=new JLabel("/"); j3=new JLabel(b1); j4=new JLabel("="); } } else { if(e==4) { j7=new JLabel("余"); a=r1[0].nextInt(c3-c2)+c2; b=r1[1].nextInt(c3-c2)+c2; while(a==0||b==0) { r1[0]=new Random(); r1[1]=new Random(); d=r1[0].nextInt(20); b=r1[1].nextInt(20); a=b*d; } a1=String.valueOf(a); b1=String.valueOf(b); d1=String.valueOf(d); j1=new JLabel(a1); j2=new JLabel("/"); j3=new JLabel(b1); j4=new JLabel("="); } } if(e==4&&f4==true) { j1.setBounds(50+j*42, 50+(k/2)*32, 40, 30); j++; j2.setBounds(50+j*42, 50+(k/2)*32, 40, 30); j++; j3.setBounds(50+j*42, 50+(k/2)*32, 40, 30); j++; j4.setBounds(50+j*42, 50+(k/2)*32, 40, 30); j++; j5.setBounds(50+j*42, 50+(k/2)*32, 40, 30); j++; j7.setBounds(65+j*42,50+(k/2)*32, 40, 30); j++; j6.setBounds(50+j*42,50+(k/2)*32, 40, 30); j++; j++; j++; k++; if(k!=0&&k%2==0) { j=0; } c.add(j1); c.add(j2); c.add(j3); c.add(j4); c.add(j5); c.add(j6); c.add(j7); } else { j1.setBounds(50+j*42, 50+(k/2)*32, 40, 30); j++; j2.setBounds(50+j*42, 50+(k/2)*32, 40, 30); j++; j3.setBounds(50+j*42, 50+(k/2)*32, 40, 30); j++; j4.setBounds(50+j*42, 50+(k/2)*32, 40, 30); j++; j5.setBounds(50+j*42, 50+(k/2)*32, 40, 30); j++; j++; j++; j++; j++; k++; if(k!=0&&k%2==0) { j=0; } c.add(j1); c.add(j2); c.add(j3); c.add(j4); c.add(j5); } anwser = d; c.repaint(); jf.setVisible(true); } // TODO Auto-generated method stub }
设计思路:
1.建立一个frame面板,通过添加复选框来选择是否有乘除法,是否有余数,是否有括号以及是否有负数
2.通过添加文本框来控制计算范围和题目数目。
3.当点击确定按钮,当前窗口关闭,数据提交,并新建一个frame窗口。
4.通过random产生随机数来控制计算题目以及加减乘除(0-3分别代表加减乘除,若没有选择有乘除,则产生0或1,从而将乘除运算排除掉)
5.根据是否选择有余数来控制除法。若没有余数,则将生成的两个随机数之积作为被除数,第二个随机数作为除数。
6.若选择有负数,加减不做处理,否则比较减数与被减数大小。若减数大于被减数,则将二者交换。
7.通过建立JLabel数组来向窗口输出题目,题目坐标按照题目数累加
8.通过建立文本框来填入答案
9.括号和检验重复没做。
10.通过线程来控制题目更换
截图:
原文:http://www.cnblogs.com/love-xi/p/5329560.html