java图形界面之电子算盘.docx

上传人:b****5 文档编号:11886869 上传时间:2023-04-08 格式:DOCX 页数:25 大小:239.78KB
下载 相关 举报
java图形界面之电子算盘.docx_第1页
第1页 / 共25页
java图形界面之电子算盘.docx_第2页
第2页 / 共25页
java图形界面之电子算盘.docx_第3页
第3页 / 共25页
java图形界面之电子算盘.docx_第4页
第4页 / 共25页
java图形界面之电子算盘.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

java图形界面之电子算盘.docx

《java图形界面之电子算盘.docx》由会员分享,可在线阅读,更多相关《java图形界面之电子算盘.docx(25页珍藏版)》请在冰豆网上搜索。

java图形界面之电子算盘.docx

java图形界面之电子算盘

Java图形用户界面

院系:

计算机系姓名:

后人好乘凉

一实验目的

●使学生综合运用图形用户界面设计的概念;

●使学生熟悉使用中间面板,组成层次复杂的GUI界面;

●使学生掌握使用事件驱动编程;

二实验环境及实验准备

●所需硬件环境为微机;

●所需软件环境为JDK、Eclipse;

●掌握Java下界面容器与基本组件的基本知识与应用;

●掌握Java下事件处理模型;

●掌握Java下图形绘制的方法;

三实验内容

(一)、设计实现电子算盘,并完成测试;

[题目描述及要求]给小朋友设计一个电子算盘。

要求绘制电子算盘界面,设计并实现打珠算过程(界面参考如下图示)。

电子算盘参考界面示意

程序源代码:

packagecalculator;

importjava.awt.Color;//导入

importjava.awt.GridLayout;

importjava.awt.Container;

importjavax.swing.JLabel;

importjavax.swing.JButton;

importjavax.swing.ImageIcon;

importjavax.swing.JPanel;

importjavax.swing.JFrame;

importjavax.swing.JTextField;

importjava.awt.event.*;

publicclassCalculatorextendsJFrame{

publicinttotalNumber;

publicintt=0;

publicboolean[]isAddFive=newboolean[5];

publicintnumber[]=newint[5];

publicImageIconpicture=newImageIcon(System.getProperty("user.dir")+"/算珠.jpg");//添加图片

publicImageIconpicture1=newImageIcon(System.getProperty("user.dir")+"/算珠1.jpg");

Containercon=getContentPane();

JPanelj1=newJPanel();

JPanelj2=newJPanel();

JPanelj3=newJPanel();

JPaneltime=newJPanel();

JLabelq=newJLabel();

JLabelnA=newJLabel();

JLabelTime=newJLabel(newImageIcon("./时钟.jpg"));//添加时钟标签的图片

JTextFielda=newJTextField();

JButtonButtons_1[][]=newJButton[2][5];

JButtonButtons_2[][]=newJButton[5][5];

GridLayoutgl_1=newGridLayout(2,5);

GridLayoutgl_2=newGridLayout(5,5);

GridLayoutgl_3=newGridLayout();

publicCalculator()

{

super("电子算盘");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100,100,600,400);

setVisible(true);

setLayout(null);

AbacusPanel();

}

publicvoidAbacusPanel()//算盘面板

{

j1.setLayout(null);

j1.setBounds(15,60,400,300);

j1.setBackground(Color.blue);

con.add(j1);

JPanelj2=newJPanel();

j2.setLayout(gl_1);

j2.setBounds(10,10,380,80);

j1.add(j2);

JPanelj3=newJPanel();

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(inti=0;i<2;i++)

{

for(intj=0;j<5;j++)

{

Buttons_1[i][j]=newJButton("");

Buttons_1[i][j].addActionListener(newClickIt());

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(inti=0;i<5;i++)

{

for(intj=0;j<5;j++)

{

Buttons_2[i][j]=newJButton("");

Buttons_2[i][j].addActionListener(newClickIt());

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]);

}

}

}

privatevoidaddTotalNumber()//计算算盘的结果

{

for(intj=0;j<5;j++)

{

if(isAddFive[j]==true&&number[j]<5)

number[j]=number[j]+5;

else

if(isAddFive[j]==false&&number[j]>=5)

number[j]=number[j]-5;

}

totalNumber=0;

for(intj=0;j<5;j++)

{

totalNumber=totalNumber+number[j]*(int)Math.pow(10,(double)j);

}

System.out.println(totalNumber);

}

privatevoidDrawCol(intj)//绘制j列算珠

{

for(inti=1;i<=4;i++)

{

Buttons_2[i][j].setIcon(picture);

}

}

classClickItimplementsActionListener//单击事件监听器

{

publicvoidactionPerformed(ActionEvente)

{

for(inti=0;i<2;i++)

for(intj=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);

}

else

if(i==0)

{

isAddFive[5-j-1]=true;

Buttons_1[1][j].setIcon(picture1);

Buttons_1[0][j].setIcon(null);

}

}

for(inti=0;i<=4;i++)//点击后动作

for(intj=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();

}

}

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

Calculatorf=newCalculator();

}

}

运行结果截图

开始

添加框架

输出电子算盘

结束

添加时钟Label,并添加图片

添加单击事件监听器及单击后动作

添加面板j1、j2、j3实现算盘分块

按面板j2、j3上划分好的网格结构添加按钮及图片。

程序流程图

 

(二)、以

(一)中的电子算盘为基础,设计并实现珠算测试器,并完成测试。

[题目描述及要求]

给小朋友设计一个珠算测试器,要求能够完成珠算加减法的测试。

具体的要求功能如下:

(1)用户启动测试,输入用户名后系统随机生成特定数目的加减法测试题;

(2)要求测试使用表盘式或数字时秒表进行界面计时显示(参考如上图示);

(3)对于每道测试题目,要求用户使用电子算盘完成珠算过程,当按下确认键时,将珠算结果与正确答案比对,并在界面上显示总题数、已答题数和已做对题数;

(4)当测试完成,界面显示本次测试情况(包括用户名、测试题目及答题明细、对错情况、测试用时和测试成绩)

程序源代码

packagetestcal;

importjava.awt.Color;//导入

importjava.awt.Font;

importjava.awt.GridLayout;

importjava.awt.Container;

importjavax.swing.JLabel;

importjavax.swing.JButton;

importjavax.swing.ImageIcon;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.JFrame;

importjavax.swing.JTextField;

importjava.awt.event.*;

importjava.util.Timer;

importjava.util.TimerTask;

publicclassTestcalextendsJFrame{

publicinttotalNumber;

publicintanswer;

publicbooleanisAnswer=true;

publicintt=0;

publicintansweredQuestion=0;

publicintrightA_Number=0;

publicintquestion_Number;

publicboolean[]isAddFive=newboolean[5];///4-5

publicintnumber[]=newint[5];///4-5

publicImageIconpicture=newImageIcon(System.getProperty("user.dir")+"/算珠.jpg");//添加图片

publicImageIconpicture1=newImageIcon(System.getProperty("user.dir")+"/算珠1.jpg");

Containercon=getContentPane();

JPanelj1=newJPanel();//添加面板

JPanelj2=newJPanel();

JPanelj3=newJPanel();

JPaneltime=newJPanel();

JLabelq=newJLabel();

JLabelnA=newJLabel();

JLabelnB=newJLabel();

JLabeln=newJLabel();

JLabelb=newJLabel();

JLabels=newJLabel();

JLabelsN=newJLabel();

JLabeltM=newJLabel();

JLabelTime=newJLabel(newImageIcon("./时钟.jpg"));//添加时钟标签的图片

JTextFielduserName=newJTextField();//添加文本

JTextFieldsNumber=newJTextField();

JButtonbeginAnswer=newJButton("开始");//添加按钮

JButtonoK=newJButton("确定");

JButtonnQ=newJButton("下一题");

JTextFielda=newJTextField();

JButtonButtons_1[][]=newJButton[2][5];//算盘上部

JButtonButtons_2[][]=newJButton[5][5];//算盘下部

GridLayoutgl_1=newGridLayout(2,5);//网格布局

GridLayoutgl_2=newGridLayout(5,5);

GridLayoutgl_3=newGridLayout();

publicTestcal()

{

super("电子算盘");

Init();//初始化

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体关闭

setBounds(100,100,600,500);//设置大小

setVisible(true);//窗体可见

setLayout(null);

AbacusPanel();//算盘面板

DrawMnue();

}

Timertimer;//计时器

publicvoidQuestionTimer()

{

timer=newTimer();

timer.schedule(newRunTask(),1000,1000);

}

classRunTaskextendsTimerTask

{

publicvoidrun()

{

t=t+1;

Stringss="";

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);

}

}

publicvoidAbacusPanel()//算盘面板

{

j1.setLayout(null);

j1.setBounds(15,160,400,300);//面板大小

j1.setBackground(Color.blue);//面板颜色

con.add(j1);//添加面板

JPanelj2=newJPanel();//面板叠加间距成为边框

j2.setLayout(gl_1);

j2.setBounds(10,10,380,80);

j1.add(j2);

JPanelj3=newJPanel();

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(inti=0;i<2;i++)

{

for(intj=0;j<5;j++)

{

Buttons_1[i][j]=newJButton("");

Buttons_1[i][j].addActionListener(newClickIt());

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(inti=0;i<5;i++)

{

for(intj=0;j<5;j++)

{

Buttons_2[i][j]=newJButton("");

Buttons_2[i][j].addActionListener(newClickIt());

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]);

}

}

}

publicvoidDrawMnue()//编辑选项

{

Fontf1=newFont("",Font.BOLD,30);

Fontf2=newFont("",Font.BOLD,14);

Fontf3=newFont("",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(newClickOk());

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(newClickNext());

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(newFont("",Font.PLAIN,12));

beginAn

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1