GUI 程序设计实验报告.docx
- 文档编号:27575910
- 上传时间:2023-07-03
- 格式:DOCX
- 页数:16
- 大小:103.65KB
GUI 程序设计实验报告.docx
《GUI 程序设计实验报告.docx》由会员分享,可在线阅读,更多相关《GUI 程序设计实验报告.docx(16页珍藏版)》请在冰豆网上搜索。
GUI程序设计实验报告
广州大学
实验报告
学院工商管理学院
专业、班级电子商务
姓名、学号
课程名称Java语言程序设计
项目名称GUI程序设计
实验时间:
2013年06月09日
GUI程序设计实验报告
开课实验室:
文俊东606
2013年06月09日
学院
工商管理学院
年级、专业、班
姓名
成绩
课程
名称
Java语言程序设计
实验项目
名称
GUI程序设计
指导教师
签名
一、实验目的
(1)掌握GUI程序设计的基本技术;
(2)掌握基本Widget的使用;
(3)掌握通GUI设计用户接口的方法;
二、实验原理(算法分析(包括流程图))
1.CalcFace类按照Lcd类进行改造(将两个类中的相关功能合并);
2.由于GUI程序已是消息驱动的,程序运行后就处于一个循环中,因此原仿真程序中Calcface中的input()方法以及Calculator中的on()方法就可弃之不用了。
3.改造Toyface,为计算器界面布局,并将Toyface与Runcale合并使之能与calculator相连。
4.System.out.println需改成向Jlabel对象输出的语句
三、实验过程原始记录(数据、图表、计算等)
1.运行程序
2.进行运算
3.计算器其他按钮的功能实现
1)C的功能实现按C后计算机只录入9*;
2)N的功能实现按N后结果变成-15
3)A的功能实现按A后清空归零
四、实验感悟
本次实验相对比较简单,只需要安排好显示界面和将两个合并之后再对一些细节部分进行修改就可以了。
完成实验后我对显示界面的布局更加了解了。
五、附录(自己实现的源代码)
///////CalcFace.java///////
importjava.awt.BorderLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.*;
importjavax.swing.BorderFactory;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.border.CompoundBorder;
classCalcFaceextendsJPanelimplementsActionListener
{
publicCalcFace(Stringk,bytepr){
keys=k;
prec=pr++;
nbuf=newStringBuffer(prec);
reset1();
errorMode=false;
errorStr="";
d=newJLabel(in,JLabel.RIGHT);
setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createLoweredBevelBorder(),BorderFactory.createEmptyBorder(5,
10,5,10)));
setLayout(newBorderLayout());//设置布管理器
reset2();
add(d);
addMouseListener(newClickHandler(this));//注册事件监听器
}
publicvoidsetCalc(Calculatorca){
calc=ca;
}
publicvoidshowNumber(Strings){
d.setText(hl+prompt+s+ht);
}
publicvoidshowOp(charop){
}
//如果输入的str为空,则清除errorMode
//相当于主控模决发送一个消息给计算器,使其可知道当前计算器对象当前的状态
publicvoiderrorInput(Stringstr){
if(str.equals("")){
errorMode=false;
errorStr="";
}else{
errorMode=true;
errorStr=str;
prompt=prompt+"("+str+")";
}
}
publicvoidshowStatus(Stringe){
prompt=e;
}
privatevoidenter(charc)
{
//当计算器处于溢出状态时,只充许其在下面这段代码中执行。
if(errorMode){//进入errormode,只接收规定字符
if(errorStr.indexOf(c)==-1){
reset1();
return;
}
}
//计算器处于正常状态时,下面这段代码会被执行
if(keys.indexOf(c)!
=-1){//ifcanoperator
showOp(c);
if(num)
calc.enterNumber(extractNumber(),c);
else
calc.enterOp(c);
reset1();
}elseif(nump(c)&&nbuf.length() num=true; buildNumber(c); } } privatebooleannump(charc){ return(c=='.'||Character.isDigit(c)); } privateStringextractNumber(){ return(nbuf.length()==0)? "0": nbuf.toString(); } privatevoidbuildNumber(charc){ inti=nbuf.length(); if(i==0&&c=='0') return;//ignoreleadingzeros if(c=='.'){//atmostonedecimalpoint if(! before_point) return; else before_point=false; } nbuf.append(c); } privatevoidreset1(){ before_point=true; nbuf.setLength(0); num=false; } protectedvoidreset2(){ count=0; in="0"; d.setText(hl+in+ht); } publicvoidactionPerformed(ActionEvente){ Stringcmd=e.getActionCommand(); inti=cmd.charAt(0); if(cmd.equals("reset2")) reset2(); elseif(count<13){ if(count==0) in=cmd; else in+=cmd; d.setText(hl+in+ht); count++; } enter((char)i); } publicvoiddoMouseEntered(){ d.setText("选择数字进行运算"); } publicvoiddoMouseExited(){ d.setText("鼠标不在显示区域"); } protectedintcount=0; protectedStringin="0"; protectedJLabeld; protectedStringhl=" protectedStringht="
privateStringprompt="Calc:
";
privateCalculatorcalc;
privateStringkeys;//keysrecognized
privateStringBuffernbuf;//bufferforinputnumber
privateStringerrorStr;//用于记录在计算器处于溢出状态时,充许用户输入的符号
privatebooleanerrorMode;//用于记录计算器当前的状态,正常或溢出状态
privatebyteprec;//maxnoofcharsdisplayable
privatebooleanbefore_point=true;
privatebooleannum=false;
}
importjava.awt.GridLayout;
importjavax.swing.JFrame;
///////RunCalc.java///////
publicclassRunCalc
{
publicstaticvoidmain(String[]args)
throwsjava.io.IOException
{
CalcEnge=newCalcEng();
CalcFacef=newCalcFace(e.keys(),e.precision());
Calculatorx=newCalculator(e,f);
JFramewin=newJFrame("Calc");
win.addWindowListener(newWindowHandler(win));
Keypadkeypad=newKeypad(f);//此处f为原Toyface中的lcd
//Onecolumngrid,布局设置成一列
win.setLayout(newGridLayout(0,1));//LCD和Keyboard各占一行
keypad.setLayout(newGridLayout(4,4));
win.getContentPane().add(f);
win.getContentPane().add(keypad);
win.pack();
win.setVisible(true);
return;
}
}
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JPanel;
classKeypadextendsJPanel{
Keypad(ActionListenerf){
r7=newJButton("A");
r7.setVerticalTextPosition(JButton.CENTER);
r7.setHorizontalTextPosition(JButton.CENTER);
r7.setActionCommand("reset2");//计算机清归零
r7.addActionListener(f);
add(r7);
r9=newJButton("N");
r9.setVerticalTextPosition(JButton.CENTER);
r9.setHorizontalTextPosition(JButton.CENTER);
r9.addActionListener(f);
add(r9);
r1=newJButton("C");
r1.setVerticalTextPosition(JButton.CENTER);
r1.setHorizontalTextPosition(JButton.CENTER);
r1.setActionCommand("C");
r1.addActionListener(f);
add(r1);
r2=newJButton("+");
r2.setVerticalTextPosition(JButton.CENTER);
r2.setHorizontalTextPosition(JButton.CENTER);
r2.addActionListener(f);
add(r2);
r3=newJButton("-");
r3.setVerticalTextPosition(JButton.CENTER);
r3.setHorizontalTextPosition(JButton.CENTER);
r3.addActionListener(f);
add(r3);
r4=newJButton("*");
r4.setVerticalTextPosition(JButton.CENTER);
r4.setHorizontalTextPosition(JButton.CENTER);
r4.addActionListener(f);
add(r4);
r5=newJButton("/");
r5.setVerticalTextPosition(JButton.CENTER);
r5.setHorizontalTextPosition(JButton.CENTER);
r5.addActionListener(f);
add(r5);
r6=newJButton("=");
r6.setVerticalTextPosition(JButton.CENTER);
r6.setHorizontalTextPosition(JButton.CENTER);
r6.addActionListener(f);
add(r6);
r8=newJButton(".");
r8.setVerticalTextPosition(JButton.CENTER);
r8.setHorizontalTextPosition(JButton.CENTER);
r8.addActionListener(f);
add(r8);
for(inti=10;i>0;i--){
b[i-1]=newJButton(""+(i-1));
b[i-1].setVerticalTextPosition(JButton.CENTER);
b[i-1].setHorizontalTextPosition(JButton.CENTER);
b[i-1].setActionCommand(""+(i-1));
b[i-1].addActionListener(f);
add(b[i-1]);
}
}
protectedJButtonr7;
protectedJButtonr1;
protectedJButtonr2;
protectedJButtonr3;
protectedJButtonr4;
protectedJButtonr5;
protectedJButtonr6;
protectedJButtonr8;
protectedJButtonr9;
protectedJButton[]b=newJButton[10];
}
六、联系方式(e-mail)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- GUI 程序设计实验报告 程序设计 实验 报告