实验二
package 四则运算; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.Timer; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.JPanel; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.GridLayout; import javax.swing.JTextField; import javax.swing.JLabel; public class jiemian extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = -2158244201317433166L; JTextField textField; JTextField textField_1; JTextField textField_2; JTextField textField_3; JTextField textField_4; JTextField textField_5; JTextField textField_6; JTextField textField_7; JTextField textField_8; JTextField textField_9; JTextField textField_10; JTextField textField_11; JTextField textField_12; JTextField t1; int a=0; JButton b1; JButton b2; JButton button; int[]daan=new int[10]; private Timer tmr; JPanel []P = {new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), new JPanel(), }; JTextField []T={new JTextField(20), new JTextField(20), new JTextField(20), new JTextField(20), new JTextField(20), new JTextField(20), new JTextField(20), new JTextField(20), new JTextField(20), new JTextField(20), new JTextField(10), new JTextField(10), new JTextField(10), new JTextField(10), new JTextField(10), new JTextField(10), new JTextField(10), new JTextField(10), new JTextField(10), new JTextField(10)}; public jiemian() { JPanel panel = new JPanel(); getContentPane().add(panel, BorderLayout.NORTH); b1 = new JButton("\u5F00\u59CB"); b1.addActionListener(this); panel.setLayout(new GridLayout(0, 3, 0, 0)); panel.add(b1); b2 = new JButton("\u4EA4\u5377"); b2.addActionListener(this); panel.add(b2); button = new JButton("\u6362\u80A4"); button.addActionListener(this); panel.add(button); JPanel panel_1 = new JPanel(); getContentPane().add(panel_1, BorderLayout.SOUTH); panel_1.setLayout(new GridLayout(0, 2, 0, 0)); panel_1.add(P[20]); JLabel label = new JLabel("\u7B54\u5BF9\u4E2A\u6570"); P[20].add(label); textField_11 = new JTextField(); P[20].add(textField_11); textField_11.setColumns(10); panel_1.add(P[21]); JLabel label_1 = new JLabel("\u7B54\u9519\u4E2A\u6570"); P[21].add(label_1); textField_10 = new JTextField(); P[21].add(textField_10); textField_10.setColumns(10); panel_1.add(P[22]); JLabel label_2 = new JLabel("\u603B\u5206"); P[22].add(label_2); textField_12 = new JTextField(); P[22].add(textField_12); textField_12.setColumns(10); panel_1.add(P[23]); JLabel label_3 = new JLabel("\u7528\u65F6"); P[23].add(label_3); t1 = new JTextField("0"); P[23].add(t1); t1.setColumns(10); JPanel panel_2 = new JPanel(); getContentPane().add(panel_2, BorderLayout.CENTER); panel_2.setLayout(new GridLayout(10,3)); JPanel panel_3 = new JPanel(); panel_2.add(panel_3); panel_3.setLayout(new GridLayout(0, 2, 0, 0)); panel_3.add(P[0]); P[0].add(T[0]); panel_3.add(P[1]); P[1].add(T[10]); JPanel panel_4 = new JPanel(); panel_2.add(panel_4); panel_4.setLayout(new GridLayout(0, 2, 0, 0)); panel_4.add(P[2]); P[2].add(T[1]); panel_4.add(P[3]); P[3].add(T[11]); JPanel panel_5 = new JPanel(); panel_2.add(panel_5); panel_5.setLayout(new GridLayout(0, 2, 0, 0)); panel_5.add(P[4]); P[4].add(T[2]); panel_5.add(P[5]); P[5].add(T[12]); JPanel panel_6 = new JPanel(); panel_2.add(panel_6); panel_6.setLayout(new GridLayout(0, 2, 0, 0)); panel_6.add(P[6]); P[6].add(T[3]); panel_6.add(P[7]); P[7].add(T[13]); JPanel panel_7 = new JPanel(); panel_2.add(panel_7); panel_7.setLayout(new GridLayout(0, 2, 0, 0)); panel_7.add(P[8]); P[8].add(T[4]); panel_7.add(P[9]); P[9].add(T[14]); JPanel panel_8 = new JPanel(); panel_2.add(panel_8); panel_8.setLayout(new GridLayout(0, 2, 0, 0)); panel_8.add(P[10]); P[10].add(T[5]); panel_8.add(P[11]); P[11].add(T[15]); JPanel panel_9 = new JPanel(); panel_2.add(panel_9); panel_9.setLayout(new GridLayout(0, 2, 0, 0)); panel_9.add(P[12]); P[12].add(T[6]); panel_9.add(P[13]); P[13].add(T[16]); JPanel panel_10 = new JPanel(); panel_2.add(panel_10); panel_10.setLayout(new GridLayout(0, 2, 0, 0)); panel_10.add(P[14]); P[14].add(T[7]); panel_10.add(P[15]); P[15].add(T[17]); JPanel panel_11 = new JPanel(); panel_2.add(panel_11); panel_11.setLayout(new GridLayout(0, 2, 0, 0)); panel_11.add(P[16]); P[16].add(T[8]); panel_11.add(P[17]); P[17].add(T[18]); JPanel panel_12 = new JPanel(); panel_2.add(panel_12); panel_12.setLayout(new GridLayout(0, 2, 0, 0)); panel_12.add(P[18]); P[18].add(T[9]); panel_12.add(P[19]); P[19].add(T[19]); Timer tmr = new Timer(1000, this); this.tmr = tmr; setSize(500,500); setLocationRelativeTo(null); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { if(a == 0) { for(int i=0;i<24;i++) { P[i].setBackground(Color.GRAY); } a++; } else if (a == 1) { for(int i=0;i<24;i++) { P[i].setBackground(Color.orange); } a++; } else if (a == 2) { for(int i=0;i<24;i++) { P[i].setBackground(Color.pink); } a=0; } } if (e.getSource() == b1) { int i=0; do { int a,d,d1,d2; d1=(int)(1+Math.random()*(100-1+1)); d2=(int)(1+Math.random()*(100-1+1)); a=(int)(Math.random()*5); d=(int)(Math.random()*12+1); if(a==0) { T[i].setText(d1+"+"+d2+"="); daan[i]=d1+d2; } if(a==1) { T[i].setText(d1+"-"+d2+"="); daan[i]=d1-d2; } if(a==2) { T[i].setText(d1+"×"+d2+"="); daan[i]=d1*d2; } if(a==3) { if(d2==0) d2=d2+1; T[i].setText(d1+"÷"+d2+"="); daan[i]=d1/d2; } if(a==4) { T[i].setText(d+"!"); daan[i]=jiecheng(d); } i++; }while(i<10); t1.setText("0"); tmr.start(); } else { int t; t = Integer.parseInt(t1.getText()); t++; t1.setText("" + t); if (t >= 120) { JFrame f=new JFrame(); JOptionPane.showMessageDialog(f,"时间到,请交卷"); tmr.stop(); } } if (e.getSource() == b2) { int n=0; for(int k=0;k<20;k++) { try { if(T[k+10].getText().length()==0) { continue; } if(daan[k]==Integer.parseInt(T[k+10].getText())) { n++; } } catch(ArrayIndexOutOfBoundsException x) { } } textField_11.setText(""+n); textField_10.setText(""+(10-n)); textField_12.setText(""+(10*n)); JFrame f=new JFrame(); JOptionPane.showMessageDialog(f,""+daan[0]+"\n"+daan[1]+"\n"+daan[2]+"\n"+daan[3]+"\n"+daan[4]+"\n"+daan[5]+"\n"+daan[6]+"\n"+daan[7]+"\n"+daan[8]+"\n"+daan[9]); tmr.stop(); } } private int jiecheng(int x) { if(x==0 || x==1) { return 1; } else { return x*jiecheng(x-1); } } public static void main(String[] args) { new jiemian(); } }
心得体会:
吴旭涛
通过四则运算的编码,编程Java界面的时,通过jdk的插件
,对按钮、输入框都有了一定的掌握,对于结对合作的方面,
加深了团队合作的重要性,对计算机编程开发、执行、
应用软件等方面有重大的意义
王宏财:
在编码四则运算过程中,
初步了解了Java界面和C语言相结合的特点,
对于随机生成四则运算方面遇到不晓得问题
,统计运行时间、错题、对题、对面板换肤等方面有点不足,
还有待进步。通过这次结对合作,
充分体会到团队合作的重要性,
以后会好好对待每一个合作机会
优点:
1.按开始按键就开始计时,按交卷键就暂停即总时间
2.有计算答对的个数,答错的个数和总分
3.能换多种皮肤
缺点:
1.计时器到了120秒不会自动交卷
2.再按开始,答题区不会清空
3.没写哪道题对,哪道题错
合作:王宏财 http://www.cnblogs.com/wanghongcai/