java图形界面之电子算盘
- 格式:doc
- 大小:359.00 KB
- 文档页数:21
Java图形用户界面院系:计算机系姓名:后人好乘凉一实验目的●使学生综合运用图形用户界面设计的概念;●使学生熟悉使用中间面板,组成层次复杂的GUI界面;●使学生掌握使用事件驱动编程;二实验环境及实验准备●所需硬件环境为微机;●所需软件环境为JDK、Eclipse;●掌握Java下界面容器与基本组件的基本知识与应用;●掌握Java下事件处理模型;●掌握Java下图形绘制的方法;三实验内容(一)、设计实现电子算盘,并完成测试;[题目描述及要求] 给小朋友设计一个电子算盘。
要求绘制电子算盘界面,设计并实现打珠算过程(界面参考如下图示)。
电子算盘参考界面示意程序源代码:package calculator;import java.awt.Color; //导入import java.awt.GridLayout;import java.awt.Container;import javax.swing.JLabel;import javax.swing.JButton;import javax.swing.ImageIcon;import javax.swing.JPanel;import javax.swing.JFrame;import javax.swing.JTextField;import java.awt.event.*;public class Calculator extends JFrame{public int totalNumber;public int t = 0;public boolean[] isAddFive = new boolean[5];public int number[] = new int[5];public ImageIcon picture = newImageIcon(System.getProperty("user.dir") + "/算珠.jpg" ); //添加图片public ImageIcon picture1 = newImageIcon(System.getProperty("user.dir") + "/算珠1.jpg" );Container con = getContentPane();JPanel j1 = new JPanel();JPanel j2 = new JPanel();JPanel j3 = new JPanel();JPanel time=new JPanel();JLabel q = new JLabel();JLabel nA = new JLabel();JLabel Time=new JLabel(new ImageIcon("./时钟.jpg")); //添加时钟标签的图片JTextField a = new JTextField();JButton Buttons_1[][] = new JButton[2][5];JButton Buttons_2[][] = new JButton[5][5];GridLayout gl_1 = new GridLayout(2,5);GridLayout gl_2 = new GridLayout(5,5);GridLayout gl_3 =new GridLayout();public Calculator(){super("电子算盘");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,600,400);setVisible(true);setLayout(null);AbacusPanel();}public void AbacusPanel() // 算盘面板{j1.setLayout(null);j1.setBounds(15,60,400,300);j1.setBackground(Color.blue);con.add(j1);JPanel j2 = new JPanel();j2.setLayout(gl_1);j2.setBounds(10, 10, 380, 80);j1.add(j2);JPanel j3 = new JPanel();j3.setLayout(gl_2);j3.setBounds(10, 100, 380, 190);j1.add(j3);time.setLayout(gl_3);time.setBounds(430,220,130,130); //时钟面板大小time.setBackground(Color.white); //时钟面板颜色time.add(Time);//添加时钟标签con.add(time);for(int i = 0;i < 2;i++){for(int j = 0;j < 5;j++){Buttons_1[i][j] = new JButton("");Buttons_1[i][j].addActionListener(new ClickIt());Buttons_1[i][j].setBorderPainted(false);Buttons_1[i][j].setContentAreaFilled(false);if(i != 1)Buttons_1[i][j].setIcon(picture1);j2.add(Buttons_1[i][j]);}}for(int i = 0;i < 5;i++){for(int j = 0;j < 5;j++){Buttons_2[i][j] = new JButton("");Buttons_2[i][j].addActionListener(new ClickIt());Buttons_2[i][j].setBorderPainted(false);Buttons_2[i][j].setContentAreaFilled(false);if(i != 0)Buttons_2[i][j].setIcon(picture);j3.add(Buttons_2[i][j]);}}}private void addTotalNumber() // 计算算盘的结果{for(int j = 0;j < 5;j++){if(isAddFive[j] == true && number[j] <5)number[j] = number[j] + 5;elseif(isAddFive[j] == false && number[j] >= 5)number[j] = number[j] - 5;}totalNumber = 0;for(int j = 0;j < 5;j++){totalNumber = totalNumber + number[j] *(int)Math.pow(10,(double)j);}System.out.println(totalNumber);}private void DrawCol(int j) // 绘制j列算珠{for(int i = 1; i <=4;i++){B uttons_2[i][j].setIcon(picture);}}class ClickIt implements ActionListener // 单击事件监听器 {public void actionPerformed(ActionEvent e){for(int i = 0;i < 2;i++)for(int j = 0;j < 5;j++)if(e.getSource() == Buttons_1[i][j]){if(i == 1){isAddFive[5-j-1] = false;Buttons_1[0][j].setIcon(picture1);Buttons_1[1][j].setIcon(null);}elseif(i == 0){isAddFive[5-j-1] = true;Buttons_1[1][j].setIcon(picture1);Buttons_1[0][j].setIcon(null); }}for(int i = 0;i <= 4;i++) // 点击后动作for(int j = 0;j < 5;j++)if(e.getSource() == Buttons_2[i][j]){number[5-j-1] = i;DrawCol(j);Buttons_2[0][j].setIcon(picture);Buttons_2[i][j].setIcon(null);}addTotalNumber();}}public static void main(String[] args) {// TODO Auto-generated method stubCalculator f = new Calculator();}}运行结果截图程序流程图开始添加框架添加面板j1、j2、j3实现算盘分块添加时钟Label,并添加图片按面板j2、j3上划分好的网格结构添加按钮及图片。
添加单击事件监听器及单击后动作输出电子算盘结束(二)、以(一)中的电子算盘为基础,设计并实现珠算测试器,并完成测试。
[题目描述及要求]给小朋友设计一个珠算测试器,要求能够完成珠算加减法的测试。
具体的要求功能如下:(1) 用户启动测试,输入用户名后系统随机生成特定数目的加减法测试题;(2) 要求测试使用表盘式或数字时秒表进行界面计时显示(参考如上图示);(3) 对于每道测试题目,要求用户使用电子算盘完成珠算过程,当按下确认键时,将珠算结果与正确答案比对,并在界面上显示总题数、已答题数和已做对题数;(4) 当测试完成,界面显示本次测试情况(包括用户名、测试题目及答题明细、对错情况、测试用时和测试成绩)程序源代码package testcal;import java.awt.Color; //导入import java.awt.Font;import java.awt.GridLayout;import java.awt.Container;import javax.swing.JLabel;import javax.swing.JButton;import javax.swing.ImageIcon;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JFrame;import javax.swing.JTextField;import java.awt.event.*;import java.util.Timer;import java.util.TimerTask;public class extends JFrame {public int totalNumber;public int answer;public boolean isAnswer = true;public int t = 0;public int answeredQuestion = 0;public int rightA_Number = 0;public int question_Number;public boolean[] isAddFive = new boolean[5];///4-5public int number[] = new int[5];///4-5public ImageIcon picture = newImageIcon(System.getProperty("user.dir") + "/算珠.jpg" ); //添加图片public ImageIcon picture1 = newImageIcon(System.getProperty("user.dir") + "/算珠1.jpg" );Container con = getContentPane();JPanel j1 = new JPanel();//添加面板JPanel j2 = new JPanel();JPanel j3 = new JPanel();JPanel time=new JPanel();JLabel q = new JLabel();JLabel nA = new JLabel();JLabel nB = new JLabel();JLabel n = new JLabel();JLabel b = new JLabel();JLabel s = new JLabel();JLabel sN = new JLabel();JLabel tM = new JLabel();JLabel Time=new JLabel(new ImageIcon("./时钟.jpg")); //添加时钟标签的图片JTextField userName = new JTextField(); //添加文本JTextField sNumber = new JTextField();JButton beginAnswer = new JButton("开始"); //添加按钮JButton oK = new JButton("确定");JButton nQ = new JButton("下一题");JTextField a = new JTextField();JButton Buttons_1[][] = new JButton[2][5]; //算盘上部JButton Buttons_2[][] = new JButton[5][5]; //算盘下部GridLayout gl_1 = new GridLayout(2,5); //网格布局GridLayout gl_2 = new GridLayout(5,5);GridLayout gl_3 =new GridLayout();public Testcal(){super("电子算盘");Init();//初始化setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗体关闭setBounds(100,100,600,500);//设置大小setVisible(true);//窗体可见setLayout(null);AbacusPanel(); //算盘面板DrawMnue();}Timer timer; // 计时器public void QuestionTimer(){timer = new Timer();timer.schedule(new RunTask(),1000,1000);}class RunTask extends TimerTask{public void run(){t = t + 1;String ss = "";if(t / 60 < 10)ss = ss + "0";ss = ss + Integer.toString(t / 60) + ":";if(t % 60 < 10)ss = ss + "0";ss = ss + Integer.toString(t % 60);tM.setText("计时: " + ss);}}public void AbacusPanel() // 算盘面板{j1.setLayout(null);j1.setBounds(15,160,400,300); //面板大小j1.setBackground(Color.blue); //面板颜色con.add(j1);//添加面板JPanel j2 = new JPanel(); //面板叠加间距成为边框j2.setLayout(gl_1);j2.setBounds(10, 10, 380, 80);j1.add(j2);JPanel j3 = new JPanel();j3.setLayout(gl_2);j3.setBounds(10, 100, 380, 190);j1.add(j3);time.setLayout(gl_3);time.setBounds(430,320,130,130); //时钟面板大小time.setBackground(Color.white); //时钟面板颜色time.add(Time);//添加时钟标签con.add(time);for(int i = 0;i < 2;i++){for(int j = 0;j < 5;j++){Buttons_1[i][j] = new JButton("");Buttons_1[i][j].addActionListener(new ClickIt());Buttons_1[i][j].setBorderPainted(false);Buttons_1[i][j].setContentAreaFilled(false);if(i != 1)Buttons_1[i][j].setIcon(picture1);j2.add(Buttons_1[i][j]);}}for(int i = 0;i < 5;i++){for(int j = 0;j < 5;j++){Buttons_2[i][j] = new JButton("");Buttons_2[i][j].addActionListener(new ClickIt());Buttons_2[i][j].setBorderPainted(false);Buttons_2[i][j].setContentAreaFilled(false);if(i != 0)Buttons_2[i][j].setIcon(picture);j3.add(Buttons_2[i][j]);}}}public void DrawMnue()//编辑选项{Font f1 = new Font("",Font.BOLD,30);Font f2 = new Font("",Font.BOLD,14);Font f3 = new Font("",Font.PLAIN,18);q.setBounds(20, 50, 280, 40);q.setFont(f1);con.add(q);a.setFont(f1);a.setBounds(270,50,80,40);a.setVisible(false);con.add(a);oK.setFont(f2);oK.setBounds(350, 50, 65, 40);oK.addActionListener(new ClickOk());oK.setVisible(false);con.add(oK);nA.setText("");nA.setBounds(20,100,300,40);nA.setFont(f1);con.add(nA);nQ.setFont(f2);nQ.setBounds(420, 50, 80, 40);nQ.addActionListener(new ClickNext());nQ.setVisible(false);con.add(nQ);nB.setText("已做对题数:" + rightA_Number); //已对题目nB.setFont(f3);nB.setForeground(Color.green);nB.setBounds(430, 150, 140, 40);con.add(nB);n.setText("已答题数:" + answeredQuestion); //已答题目n.setFont(f3);n.setForeground(Color.red);n.setBounds(430, 190, 140, 40);con.add(n);sN.setText("总题数:" + question_Number);//总题数sN.setFont(f3);sN.setBounds(430, 240, 140, 40);con.add(sN);b.setText("用户名:"); //用户名b.setFont(f3);b.setBounds(20, 0, 80, 40);con.add(b);userName.setText("");userName.setFont(f3);userName.setBounds(100, 10, 60, 30);userName.setHorizontalAlignment(0);con.add(userName);s.setText("设置题目总数:");//设置题目总数s.setFont(f3);s.setBounds(180,0,130,40);con.add(s);sNumber.setText("");sNumber.setFont(f3);sNumber.setBounds(300, 10, 60, 30);sNumber.setHorizontalAlignment(0);con.add(sNumber);beginAnswer.setFont(new Font("",Font.PLAIN,12));beginAnswer.setBounds(400, 5, 85,30);beginAnswer.addActionListener(new ClickLogin());con.add(beginAnswer);tM.setText("计时: 00:00"); //计时tM.setFont(f3);tM.setBounds(430, 280, 200, 40);con.add(tM);}private void Question() // 生成题目{int i1 = 1 + (int)(Math.random()*9999); //随机数范围9999-99999int i2 = 1 + (int)(Math.random()*9999);int number1 = Math.max(i1, i2);int number2 = Math.min(i1, i2);int m = (int)(Math.random()*2);if(m == 0){while(i1 + i2 > 10000) //随机数之和大于10000{i1 = 1 + (int)(Math.random()*99999);i2 = 1 + (int)(Math.random()*99999);}q.setText(Integer.toString(i1) + " + "+ Integer.toString(i2)+ " = ");answer = i1 + i2;}else{q.setText(Integer.toString(number1) + " - " +Integer.toString(number2) + " = ");answer = number1 - number2;}}private void addTotalNumber()// 计算算盘的结果{for(int j = 0;j < 5;j++){if(isAddFive[j] == true && number[j] < 5) //1-4number[j] = number[j] + 5;elseif(isAddFive[j] == false && number[j] >= 5) //5-9number[j] = number[j] - 5;}totalNumber = 0;for(int j = 0;j < 5;j++){totalNumber = totalNumber + number[j] *(int)Math.pow(10,(double)j);}System.out.println(totalNumber);}private void Init() // 初始化数据{totalNumber = 0;for(int j = 0;j < 5;j++) {number[j] = 0;isAddFive[j] = false;}}private void DrawCol(int j) // 绘制j列算珠{for(int i = 1; i <=4;i++) //1-5{B uttons_2[i][j].setIcon(picture);}}class ClickIt implements ActionListener //单击事件监听器{public void actionPerformed(ActionEvent e){for(int i = 0;i < 2;i++)for(int j = 0;j < 5;j++)if(e.getSource() == Buttons_1[i][j]){if(i == 1){isAddFive[5-j-1] = false;Buttons_1[0][j].setIcon(picture1);Buttons_1[1][j].setIcon(null);}elseif(i == 0){isAddFive[5-j-1] = true;Buttons_1[1][j].setIcon(picture1);Buttons_1[0][j].setIcon(null);}}for(int i = 0;i <= 4;i++) // 点击后动作for(int j = 0;j < 5;j++) //1-5if(e.getSource() == Buttons_2[i][j]){number[5-j-1] = i;DrawCol(j);Buttons_2[0][j].setIcon(picture);Buttons_2[i][j].setIcon(null);}addTotalNumber();}}class ClickOk implements ActionListener //答题点击确定后{public void actionPerformed(ActionEvent e){isAnswer = true;timer.cancel();a.setText(Integer.toString(totalNumber));if(totalNumber == answer){oK.setText("正确");oK.setForeground(Color.green);rightA_Number = rightA_Number + 1;}else{oK.setText("错误");oK.setForeground(Color.red);nA.setText("正确答案为: " + Integer.toString(answer));nA.setForeground(Color.green);}answeredQuestion = answeredQuestion + 1;nB.setText("已做对题数:" + rightA_Number);nB.setForeground(Color.green);n.setText("已答题数:" + answeredQuestion);n.setForeground(Color.red);}}class ClickNext implements ActionListener //点击下一题{public void actionPerformed(ActionEvent e){if(isAnswer == true){isAnswer = false;if(nQ.getText() == "完成"){timer.cancel();double x = ((double)rightA_Number / (double)question_Number) * 100;int option = JOptionPane.showConfirmDialog(null , "您的正确率为: " + x + "%\n用时" + t + "秒"+"\n是否重新开始?" , "提示",JOptionPane.YES_NO_OPTION );if(JOptionPane.YES_OPTION == option){userName.setText("");userName.setEditable(true);sNumber.setText("");sNumber.setEditable(true);beginAnswer.setEnabled(true);nQ.setVisible(false);oK.setVisible(false);a.setVisible(false);q.setVisible(false);oK.setText("确定");oK.setForeground(null);a.setText("");nA.setText("");nQ.setText("下一题");Init();rightA_Number = 0;answeredQuestion = 0;t = 0;n.setText("已答题数:"+ answeredQuestion); //显示已答题数等信息n.setForeground(Color.green);nB.setText("已做对题数:" + rightA_Number);nB.setForeground(Color.red);sN.setText("总题数:" + 0);sN.setForeground(Color.red);tM.setText("计时:" + 0);}}else{QuestionTimer();Question();oK.setText("确定");oK.setForeground(null);a.setText("");nA.setText("");for(int i = 0;i < 5;i++) // 初始化算盘{DrawCol(i);Buttons_1[1][i].setIcon(null);Buttons_2[0][i].setIcon(null);Buttons_1[0][i].setIcon(picture1);}Init();if(answeredQuestion == question_Number - 1)nQ.setText("完成");}}}}class ClickLogin implements ActionListener //用户名等信息{public void actionPerformed(ActionEvent e){boolean isInt = true;for(int i = 0;i < sNumber.getText().length();i++)if(!Character.isDigit(sNumber.getText().charAt(i)))isInt = false;if(userName.getText().equalsIgnoreCase(""))JOptionPane.showMessageDialog(null, "请输入用户名!");elseif(sNumber.getText().equalsIgnoreCase("") || isInt == false) JOptionPane.showMessageDialog(null, "请输入题目总数 !");else{isAnswer = false;Question();beginAnswer.setEnabled(false);q.setVisible(true);nQ.setVisible(true);oK.setVisible(true);a.setVisible(true);userName.setEditable(false);sNumber.setEditable(false);String xx = sNumber.getText();question_Number = Integer.parseInt(xx);sN.setText("总题数:" + question_Number); QuestionTimer();}}}public static void main(String[] args) {Testcal t = new Testcal();}}程序运行结果截图程序流程图四实验分析及问题思考1、时钟还没能实现,仅使用timer形成了电子钟。