1、java图形界面之电子算盘Java图形用户界面院系: 计算机系 姓名:后人好乘凉一 实验目的 使学生综合运用图形用户界面设计的概念; 使学生熟悉使用中间面板,组成层次复杂的GUI界面; 使学生掌握使用事件驱动编程;二 实验环境及实验准备 所需硬件环境为微机; 所需软件环境为JDK、Eclipse; 掌握Java下界面容器与基本组件的基本知识与应用; 掌握Java下事件处理模型; 掌握Java下图形绘制的方法;三 实验内容(一)、设计实现电子算盘,并完成测试;题目描述及要求 给小朋友设计一个电子算盘。要求绘制电子算盘界面,设计并实现打珠算过程(界面参考如下图示)。 电子算盘参考界面示意程序源代码
2、: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
3、Calculator extends JFrame public int totalNumber; public int t = 0; public boolean isAddFive = new boolean5; public int number = new int5; public ImageIcon picture = new ImageIcon(System.getProperty(user.dir) + /算珠.jpg ); /添加图片 public ImageIcon picture1 = new ImageIcon(System.getProperty(user.dir) +
4、 /算珠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(); JButto
5、n Buttons_1 = new JButton25; JButton Buttons_2 = new JButton55; 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(tru
6、e); 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.s
7、etBounds(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_1ij = new JButton(); Buttons_1ij.addActionListener(new ClickIt()
8、; Buttons_1ij.setBorderPainted(false); Buttons_1ij.setContentAreaFilled(false); if(i != 1) Buttons_1ij.setIcon(picture1); j2.add(Buttons_1ij); for(int i = 0;i 5;i+) for(int j = 0;j 5;j+) Buttons_2ij = new JButton(); Buttons_2ij.addActionListener(new ClickIt(); Buttons_2ij.setBorderPainted(false); Bu
9、ttons_2ij.setContentAreaFilled(false); if(i != 0) Buttons_2ij.setIcon(picture); j3.add(Buttons_2ij); private void addTotalNumber() / 计算算盘的结果 for(int j = 0;j 5;j+) if(isAddFivej = true & numberj = 5) numberj = numberj - 5; totalNumber = 0; for(int j = 0;j 5;j+) totalNumber = totalNumber + numberj * (
10、int)Math.pow(10,(double)j); System.out.println(totalNumber); private void DrawCol(int j) / 绘制j列算珠 for(int i = 1; i =4;i+) Buttons_2ij.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.getSou
11、rce() = Buttons_1ij) if(i = 1) isAddFive5-j-1 = false; Buttons_10j.setIcon(picture1); Buttons_11j.setIcon(null); else if(i = 0) isAddFive5-j-1 = true; Buttons_11j.setIcon(picture1); Buttons_10j.setIcon(null); for(int i = 0;i = 4;i+) / 点击后动作 for(int j = 0;j 5;j+) if(e.getSource() = Buttons_2ij) numbe
12、r5-j-1 = i; DrawCol(j); Buttons_20j.setIcon(picture); Buttons_2ij.setIcon(null); addTotalNumber(); public static void main(String args) / TODO Auto-generated method stub Calculator f = new Calculator(); 运行结果截图开 始添加框架输出电子算盘结 束添加时钟Label,并添加图片添加单击事件监听器及单击后动作添加面板j1、j2、j3实现算盘分块按面板j2、j3上划分好的网格结构添加按钮及图片。程序
13、流程图(二)、以(一)中的电子算盘为基础,设计并实现珠算测试器,并完成测试。题目描述及要求给小朋友设计一个珠算测试器,要求能够完成珠算加减法的测试。具体的要求功能如下:(1) 用户启动测试,输入用户名后系统随机生成特定数目的加减法测试题;(2) 要求测试使用表盘式或数字时秒表进行界面计时显示(参考如上图示);(3) 对于每道测试题目,要求用户使用电子算盘完成珠算过程,当按下确认键时,将珠 算结果与正确答案比对,并在界面上显示总题数、已答题数和已做对题数;(4) 当测试完成,界面显示本次测试情况(包括用户名、测试题目及答题明细、对错情 况、测试用时和测试成绩)程序源代码package testc
14、al;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;im
15、port java.awt.event.*;import java.util.Timer;import java.util.TimerTask;public class Testcal 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 b
16、oolean isAddFive = new boolean5;/4-5 public int number = new int5;/4-5 public ImageIcon picture = new ImageIcon(System.getProperty(user.dir) + /算珠.jpg ); /添加图片 public ImageIcon picture1 = new ImageIcon(System.getProperty(user.dir) + /算珠1.jpg ); Container con = getContentPane(); JPanel j1 = new JPane
17、l(); /添加面板 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
18、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
19、JButton25; /算盘上部 JButton Buttons_2 = new JButton55; /算盘下部 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);
20、 /设置大小 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
21、+ 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(); /面板叠加间距成为边框 j
22、2.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(
23、int i = 0;i 2;i+) for(int j = 0;j 5;j+) Buttons_1ij = new JButton(); Buttons_1ij.addActionListener(new ClickIt(); Buttons_1ij.setBorderPainted(false); Buttons_1ij.setContentAreaFilled(false); if(i != 1) Buttons_1ij.setIcon(picture1); j2.add(Buttons_1ij); for(int i = 0;i 5;i+) for(int j = 0;j 5;j+) B
24、uttons_2ij = new JButton(); Buttons_2ij.addActionListener(new ClickIt(); Buttons_2ij.setBorderPainted(false); Buttons_2ij.setContentAreaFilled(false); if(i != 0) Buttons_2ij.setIcon(picture); j3.add(Buttons_2ij); public void DrawMnue() /编辑选项 Font f1 = new Font(,Font.BOLD,30); Font f2 = new Font(,Fon
25、t.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
26、.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
27、, 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.set
28、Bounds(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); beginAn
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1