本次结对的同学为 :
201306114314 李泳江
201306114309 谢洪跃
package ComputeProject; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class MainFrame extends JFrame implements ActionListener{ private JLabel time,jbl0,jbl1,jbl2,jbl3,jbl4,jbl5,jbl6,jbl7,jbl8,jbl9,jbl10, jbl11,jbl12,jbl13,jbl14,jbl15,jbl16,jbl17,jbl18,jbl19,jbl20; private JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6,jtf7,jtf8,jtf9,jtf10, jtf11,jtf12,jtf13,jtf14,jtf15,jtf16,jtf17,jtf18,jtf19,jtf20; private JButton jb1,jb2,jb3; private JPanel jp1,jp2; private ArrayList questions,answers,results; private static int right=0,wrong=0; private int score=0; private String s=null; class BgPanel extends JPanel { public void paintComponent(Graphics g) { Graphics2D gd = (Graphics2D)g; super.paintComponent(g); Image img = Toolkit.getDefaultToolkit().getImage("Images/Main.jpg"); gd.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this); } } public MainFrame() { this.InitQuestion(); jp1 = new JPanel(); jp1.setLayout(null); jp1.setBounds(0,100,800,400); jp1.setOpaque(false); jbl1 = new JLabel("第一题:"); jbl1.setBounds(new Rectangle(120,0,50,30)); jbl1.setForeground(Color.yellow); jbl2 = new JLabel("第二题:"); jbl2.setBounds(new Rectangle(120,40,50,30)); jbl2.setForeground(Color.yellow); jbl3 = new JLabel("第三题:"); jbl3.setBounds(new Rectangle(120,80,50,30)); jbl3.setForeground(Color.yellow); jbl4 = new JLabel("第四题:"); jbl4.setBounds(new Rectangle(120,120,50,30)); jbl4.setForeground(Color.yellow); jbl5 = new JLabel("第五题:"); jbl5.setBounds(new Rectangle(120,160,50,30)); jbl5.setForeground(Color.yellow); jbl6 = new JLabel("第六题:"); jbl6.setBounds(new Rectangle(120,200,50,30)); jbl6.setForeground(Color.yellow); jbl7 = new JLabel("第七题:"); jbl7.setBounds(new Rectangle(120,240,50,30)); jbl7.setForeground(Color.yellow); jbl8 = new JLabel("第八题:"); jbl8.setBounds(new Rectangle(120,280,50,30)); jbl8.setForeground(Color.yellow); jbl9 = new JLabel("第九题:"); jbl9.setBounds(new Rectangle(120,320,50,30)); jbl9.setForeground(Color.yellow); jbl10 = new JLabel("第十题:"); jbl10.setBounds(new Rectangle(120,360,50,30)); jbl10.setForeground(Color.yellow); jbl11 = new JLabel(" = "); jbl11.setBounds(new Rectangle(440,0,50,30)); jbl11.setForeground(Color.yellow); jbl12 = new JLabel(" = "); jbl12.setBounds(new Rectangle(440,40,50,30)); jbl12.setForeground(Color.yellow); jbl13 = new JLabel(" = "); jbl13.setBounds(new Rectangle(440,80,50,30)); jbl13.setForeground(Color.yellow); jbl14 = new JLabel(" = "); jbl14.setBounds(new Rectangle(440,120,50,30)); jbl14.setForeground(Color.yellow); jbl15 = new JLabel(" = "); jbl15.setBounds(new Rectangle(440,160,50,30)); jbl15.setForeground(Color.yellow); jbl16 = new JLabel(" = "); jbl16.setBounds(new Rectangle(440,200,50,30)); jbl16.setForeground(Color.yellow); jbl17 = new JLabel(" = "); jbl17.setBounds(new Rectangle(440,240,50,30)); jbl17.setForeground(Color.yellow); jbl18 = new JLabel(" = "); jbl18.setBounds(new Rectangle(440,280,50,30)); jbl18.setForeground(Color.yellow); jbl19 = new JLabel(" = "); jbl19.setBounds(new Rectangle(440,320,50,30)); jbl19.setForeground(Color.yellow); jbl20 = new JLabel(" = "); jbl20.setBounds(new Rectangle(440,360,50,30)); jbl20.setForeground(Color.yellow); jtf1 = new JTextField(); jtf1.setHorizontalAlignment(JTextField.CENTER); jtf1.setText((String)questions.get(0)); jtf1.setEditable(false); jtf1.setBounds(new Rectangle(200,0,200,30)); jtf2 = new JTextField(); jtf2.setHorizontalAlignment(JTextField.CENTER); jtf2.setText((String)questions.get(1)); jtf2.setEditable(false); jtf2.setBounds(new Rectangle(200,40,200,30)); jtf3 = new JTextField(); jtf3.setHorizontalAlignment(JTextField.CENTER); jtf3.setText((String)questions.get(2)); jtf3.setEditable(false); jtf3.setBounds(new Rectangle(200,80,200,30)); jtf4= new JTextField(); jtf4.setHorizontalAlignment(JTextField.CENTER); jtf4.setText((String)questions.get(3)); jtf4.setEditable(false); jtf4.setBounds(new Rectangle(200,120,200,30)); jtf5 = new JTextField(); jtf5.setHorizontalAlignment(JTextField.CENTER); jtf5.setText((String)questions.get(4)); jtf5.setEditable(false); jtf5.setBounds(new Rectangle(200,160,200,30)); jtf6 = new JTextField(); jtf6.setHorizontalAlignment(JTextField.CENTER); jtf6.setText((String)questions.get(5)); jtf6.setEditable(false); jtf6.setBounds(new Rectangle(200,200,200,30)); jtf7 = new JTextField(); jtf7.setHorizontalAlignment(JTextField.CENTER); jtf7.setText((String)questions.get(6)); jtf7.setEditable(false); jtf7.setBounds(new Rectangle(200,240,200,30)); jtf8 = new JTextField(); jtf8.setHorizontalAlignment(JTextField.CENTER); jtf8.setText((String)questions.get(7)); jtf8.setEditable(false); jtf8.setBounds(new Rectangle(200,280,200,30)); jtf9 = new JTextField(); jtf9.setHorizontalAlignment(JTextField.CENTER); jtf9.setText((String)questions.get(8)); jtf9.setEditable(false); jtf9.setBounds(new Rectangle(200,320,200,30)); jtf10 = new JTextField(); jtf10.setHorizontalAlignment(JTextField.CENTER); jtf10.setText((String)questions.get(9)); jtf10.setEditable(false); jtf10.setBounds(new Rectangle(200,360,200,30)); jtf11 = new JTextField(); jtf11.setBounds(new Rectangle(490,0,150,30)); jtf12 = new JTextField(); jtf12.setBounds(new Rectangle(490,40,150,30)); jtf13 = new JTextField(); jtf13.setBounds(new Rectangle(490,80,150,30)); jtf14 = new JTextField(); jtf14.setBounds(new Rectangle(490,120,150,30)); jtf15 = new JTextField(); jtf15.setBounds(new Rectangle(490,160,150,30)); jtf16 = new JTextField(); jtf16.setBounds(new Rectangle(490,200,150,30)); jtf17 = new JTextField(); jtf17.setBounds(new Rectangle(490,240,150,30)); jtf18 = new JTextField(); jtf18.setBounds(new Rectangle(490,280,150,30)); jtf19 = new JTextField(); jtf19.setBounds(new Rectangle(490,320,150,30)); jtf20 = new JTextField(); jtf20.setBounds(new Rectangle(490,360,150,30)); jp1.add(jbl1); jp1.add(jbl2); jp1.add(jbl3); jp1.add(jbl4); jp1.add(jbl5); jp1.add(jbl6); jp1.add(jbl7); jp1.add(jbl8); jp1.add(jbl9); jp1.add(jbl10); jp1.add(jbl11); jp1.add(jbl12); jp1.add(jbl13); jp1.add(jbl14); jp1.add(jbl15); jp1.add(jbl16); jp1.add(jbl17); jp1.add(jbl18); jp1.add(jbl19); jp1.add(jbl20); jp1.add(jtf1); jp1.add(jtf2); jp1.add(jtf3); jp1.add(jtf4); jp1.add(jtf5); jp1.add(jtf6); jp1.add(jtf7); jp1.add(jtf8); jp1.add(jtf9); jp1.add(jtf10); jp1.add(jtf11); jp1.add(jtf12); jp1.add(jtf13); jp1.add(jtf14); jp1.add(jtf15); jp1.add(jtf16); jp1.add(jtf17); jp1.add(jtf18); jp1.add(jtf19); jp1.add(jtf20); jp2 = new JPanel(); jp2.setLayout(null); jp2.setBounds(0,500,800,100); jp2.setOpaque(false); jb1 = new JButton("开始"); jb1.setBounds(new Rectangle(450,10,70,30)); jb1.addActionListener(this); jb2 = new JButton("提交"); jb2.setBounds(new Rectangle(530,10,70,30)); jb2.addActionListener(this); jb3 = new JButton("退出"); jb3.setBounds(new Rectangle(610,10,70,30)); jb3.addActionListener(this); jbl0 = new JLabel("时间: "); jbl0.setFont(new Font("微软雅黑",Font.BOLD,15)); jbl0.setForeground(Color.red); jbl0.setBounds(new Rectangle(100,10,50,30)); time = new JLabel(); time.setForeground(Color.red); time.setBounds(new Rectangle(160,10,100,30)); jp2.add(jbl0); jp2.add(jb1); jp2.add(jb2); jp2.add(jb3); jp2.add(time); this.setContentPane(new BgPanel()); this.setVisible(true); this.setLayout(null); this.setTitle("四则运算测试系统"); this.setSize(800,600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.add(jp1); this.add(jp2); } public int timer=0; public boolean running=true; public void startTimer() { new java.lang.Thread(new Runnable(){ public void run() { while(running) { try{ Thread.sleep(1000);//睡一秒 } catch(Exception e){} timer++; time.setText("已使用: " + String.valueOf(timer) + " 秒"); } } }).start(); } private void InitQuestion() { questions = new ArrayList(10); answers = new ArrayList(10); String[] operations = {"+","-","*","/"}; Random random = new Random(); String operation = null; int num1 = 0; int num2 = 0; for(int i = 0; i < 10; i++) { //自动生成运算符 operation = operations[(int)(random.nextDouble()*10*2/5)]; //自动生成2个100以内的数 num1 = (int)(random.nextDouble()*100); num2 = (int)(random.nextDouble()*100); if(operation.equals("+")) { answers.add(i, s.valueOf(num1+num2)); questions.add(i, num1 + " + " + num2 ); } else if(operation.equals("-")) { answers.add(i, s.valueOf(num1-num2)); questions.add(i, num1 + " - " +num2); } else if(operation.equals("*")) { answers.add(i, s.valueOf(num1*num2)); questions.add(i, num1 + " * " +num2); } else if(operation.equals("/")) { //防止除数为0 while(num2 == 0) { num2 = (int)(random.nextDouble()*100); } answers.add(i, s.valueOf(num1/num2)); questions.add(i, num1 + " / " +num2); } } } public void getResult() { results = new ArrayList(10); results.add(0,jtf11.getText()); results.add(1,jtf12.getText()); results.add(2,jtf13.getText()); results.add(3,jtf14.getText()); results.add(4,jtf15.getText()); results.add(5,jtf16.getText()); results.add(6,jtf17.getText()); results.add(7,jtf18.getText()); results.add(8,jtf19.getText()); results.add(9,jtf20.getText()); } public static void main(String []args) { Component mf = null; MainFrame mf1 = new MainFrame(); JOptionPane.showMessageDialog(mf, "请点击" + "[" + "开始" + "]" + "按钮开始测试!"); } public void actionPerformed(ActionEvent e) { if(e.getSource()==jb1) { this.startTimer(); } else if(e.getSource()==jb2) { this.getResult(); running=false; for(int i=0;i<10;i++) { if(results.get(i).equals(answers.get(i))) { right++; } else { wrong++; } } score = right * 10; ResultFrame rf = new ResultFrame(right,wrong,timer,score); this.dispose(); } else if(e.getSource()==jb3) { this.dispose(); JOptionPane.showMessageDialog(this, "谢谢使用!"); System.exit(0); } } }
package ComputeProject; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.event.AncestorListener; public class ResultFrame extends JFrame implements ActionListener{ private JLabel jbl1,jbl2,jbl3,jbl4; private JButton jb1,jb2; private JPanel jp1,jp2; private int compare; class BgPanel extends JPanel { private int compare; public void paintComponent(Graphics g) { Graphics2D gd = (Graphics2D)g; super.paintComponent(g); Image img = Toolkit.getDefaultToolkit().getImage("Images/Result.jpg"); gd.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this); } public void ResultFrame(int right,int wrong,int time,int score) { this.compare = score; jp1 = new JPanel(); jp1.setBounds(0,50,400,150); jp1.setOpaque(false); jp1.setLayout(null); jbl1 = new JLabel("答 对: " + right + " 道 题"); jbl1.setForeground(Color.yellow); jbl1.setBounds(new Rectangle(80,0,200,30)); jbl2 = new JLabel("答 错: " + wrong + " 道 题"); jbl2.setForeground(Color.yellow); jbl2.setBounds(new Rectangle(80,40,200,30)); jbl3 = new JLabel("用 时: " + time + " 秒"); jbl3.setForeground(Color.yellow); jbl3.setBounds(new Rectangle(80,80,200,30)); jbl4 = new JLabel("得 分: " + score); jbl4.setForeground(Color.yellow); jbl4.setBounds(new Rectangle(80,120,200,30)); jp1.add(jbl1); jp1.add(jbl2); jp1.add(jbl3); jp1.add(jbl4); jp2 = new JPanel(); jp2.setLayout(null); jp2.setOpaque(false); jp2.setBounds(0,200,400,100); jb1 = new JButton("查看评价"); jb1.addAncestorListener((AncestorListener) this); jb1.setBounds(new Rectangle(60,20,100,30)); jb2 = new JButton("退出测试"); jb2.addActionListener((ActionListener) this); jb2.setBounds(new Rectangle(200,20,100,30)); jp2.add(jb1); jp2.add(jb2); this.setContentPane(new BgPanel()); this.setLayout(null); this.setTitle("测试结果"); this.setResizable(false); this.setSize(400,300); this.add(jp1); this.add(jp2); this.setVisible(true); } private void setResizable(boolean b) { // TODO 自动生成的方法存根 } private void setTitle(String string) { // TODO 自动生成的方法存根 } private void setContentPane(BgPanel bgPanel) { // TODO 自动生成的方法存根 } public void actionPerformed(ActionEvent e) { if(e.getSource()==jb1) { if(compare >= 90) JOptionPane.showMessageDialog(this, "恭喜你,获得 A级水平!"); else if(compare >= 80) JOptionPane.showMessageDialog(this, "恭喜你,获得B级水平!"); else if(compare >= 70) JOptionPane.showMessageDialog(this, "恭喜你,获得C级水平!"); else if(compare >= 60) JOptionPane.showMessageDialog(this, "好险啊,你才刚刚及格!"); else JOptionPane.showMessageDialog(this, "很遗憾,你没有及格!"); } else if(e.getSource()==jb2) { this.dispose(); System.exit(0); } } private void dispose() { // TODO 自动生成的方法存根 } } public ResultFrame(int right, int wrong, int timer, int score) { // TODO 自动生成的构造函数存根 } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 } }
做这个程序的时候我觉得有难度,界面我和我的伙伴是做出来了,可是有一些按钮的功能实现不了,我和他的Java都不太好,我们讨论了很久也还是不怎么能实现那个提交的功能,这还是个半成品啊,有点失望,做了那么久还是只能做个半成品出来,觉得自己学得还是不够。做这个实验对自己的收获还是很大的,因为Java已经有一段时间没接触了,现在能好好地练习一下,不仅复习了以前的知识,还加固了自己的学习,我们会继续改正,把最好的程序做出来。
时间: 2024-11-03 10:01:48