图形界面实验_第1页
图形界面实验_第2页
图形界面实验_第3页
图形界面实验_第4页
图形界面实验_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、深 圳 大 学 实 验 报 告 课程名称: Java程序设计 实验项目名称: 图形界面的实验 学院: 计算机与软件学院 专业: 指导教师: 傅向华 报告人:赖裕辉 学号:2012150089 班级: 计软2班 实验时间: 2013/12/15 实验报告提交时间: 2013/12/23 教务处制实验7 图形界面的实验一、实验目的GUI是java一个重要的内容,学习并掌握好GUI的捷径就是自己动手编一个用户图形交互应用软件,从原件、布局入手,再通过事件处理机制的调用,你会发现GUI不过也就是一件简单的过程。二、实验内容给定如下代码:import javax.swing.*; import java

2、.awt.*;import java.awt.event.*;public class Computer public static void main(String args) new Test(); class Test extends JFrame implements ActionListener JButton c1,c2,c3,c4,c5,c6,c7,c8;JButton b; JTextArea text;int x,y,sum; Test() JButton b=new JButton20; text=new JTextArea(3,20); text.setLineWrap(

3、true); String str = "MC","MR","MS","M+","M-","#","CE","C","#","#","7","8","9","/","%","4","5","6","*","1/

4、x" setLayout(null); add(text); text.setBounds(10,10,220,50); text.setBorder(BorderFactory.createLineBorder(Color.gray,5); text.setFont(new Font("宋体",Font.BOLD,30); for(int i=0;i<20;i+) bi=new JButton(stri); add(bi); if(i<5) bi.setBounds(2+52*i,80,50,50); else if(i<10) bi.setB

5、ounds(2+52*(i-5),131,50,50); else if(i<15) bi.setBounds(2+52*(i-10),182,50,50); else if(i<20) bi.setBounds(2+52*(i-15),233,50,50); c1=new JButton("1"); add( c1 ); c1.setBounds(2, 284, 50, 50); c2=new JButton("2"); c2.setBounds(54, 284, 50, 50); c2.addActionListener(this); a

6、dd( c2 ); c3=new JButton("3"); c3.setBounds(106, 284, 50, 50); c3.addActionListener(this); add( c3 ); c4=new JButton("-"); c4.setBounds(158, 284, 50, 50); add( c4 ); c5=new JButton("="); c5.addActionListener(this); c5.setBounds(210, 284, 50, 102); add( c5 ); c6=new JBut

7、ton("0"); c6.setBounds(2, 335, 102, 50); add( c6 ); c7=new JButton("."); c7.setBounds(106, 335, 50, 50); add( c7 ); c8=new JButton("+"); add( c8 ); c8.setBounds(158, 335, 50, 50); c8.addActionListener(this); validate(); setBounds(0,0,266,416); setTitle("我的计算器"

8、); setVisible(true); setResizable(false); /取消放大功能按钮 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif (e.getSource()=c3) text.setText("3"); if(e.getSource()=c8) x=3; text.setText(" "); if(e.ge

9、tSource()=c2) text.setText("2"); if(e.getSource()=c5) y=2; sum=x+y; text.setText(""+sum); 1. 仔细研究布局代码的编写,看看结果,能否进行优化?比如某些字符无法连续显示(MS),能否采用ImageIcon的形式来初始化空间?2. 对我们代码进行修改,能够达到对任意的两个十进制数进行加法操作,比如先点击1,再点击2,再点击+,再点击3,再点击9,再点击=,结果为51.3. 之后在2的基础上进行两个任意十进制数的除法操作!注意必须保证不能零除,结果也有可能为浮点数!按照

10、我所给的实验报告格式,认真完成各项要求并在指定的时间内提交你的报告(注意,只需提交word格式文档一份,千万不要压缩,增加我的打开工作量!),请各位注意提交时间,过了截止时间,如果补交,成绩会相应进行打折!三、程序代码 /* 如果代码长,给出你认为的关键部分即可,我不会没个代码都运行*/源代码package acm;import javax.swing.*; import java.awt.*;import java.awt.event.*;public class MyCalculator public static void main(String args) new Test(); cl

11、ass Test extends JFrame implements ActionListener JButton c1,c2,c3,c4,c5,c6,c7,c8;JButton b; JTextArea text;double x,y,sum;int z;StringBuffer sss; Test() b=new JButton20; sss=new StringBuffer(); /str.setLength(0); text=new JTextArea(3,20); text.setLineWrap(true); /布局及起名字等 String str = "MC"

12、,"MR","MS","M+","M-","#","CE","C","#","#","7","8","9","/","%","4","5","6","*","1/x" setLayout(null); add(text)

13、; text.setBounds(10,10,250,50); text.setBorder(BorderFactory.createLineBorder(Color.gray,5); text.setFont(new Font("宋体",Font.BOLD,30); for(int i=0;i<20;i+)/设置格式 bi=new JButton(stri); bi.addActionListener(this); if(i<5) bi.setBounds(2+62*i,80,60,50); else if(i<10) bi.setBounds(2+62

14、*(i-5),131,60,50); else if(i<15) bi.setBounds(2+62*(i-10),182,60,50); else if(i<20) bi.setBounds(2+62*(i-15),233,60,50); add(bi); /几个特别拿出来的 c1=new JButton("1"); c1.setBounds(2, 284, 60, 50); c1.addActionListener(this); add( c1 ); c2=new JButton("2"); c2.setBounds(64, 284, 6

15、0, 50); c2.addActionListener(this); add( c2 ); c3=new JButton("3"); c3.setBounds(126, 284, 60, 50); c3.addActionListener(this); add( c3 ); c4=new JButton("-"); c4.setBounds(188, 284, 60, 50); c4.addActionListener(this); add( c4 ); c5=new JButton("="); c5.addActionListen

16、er(this); c5.setBounds(250, 284, 60, 102); c5.addActionListener(this); add(c5); c6=new JButton("0"); c6.setBounds(2, 335, 102, 50); c6.addActionListener(this); add( c6 ); c7=new JButton("."); c7.setBounds(106, 335, 60, 50); c7.addActionListener(this); add( c7 ); c8=new JButton(&q

17、uot;+"); add( c8 ); c8.setBounds(168, 335, 80, 50); c8.addActionListener(this); /设置大小格式什么的了勒勒 validate(); setBounds(0,0,316,416); setTitle("我的计算器"); setVisible(true); setResizable(false); /取消放大功能按钮 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Overridepublic void actionPerformed(

18、ActionEvent e) if(e.getSource()=b15)text.setText("4");/ TODO Auto-generated method stubtryif(e.getSource()=c8)/单击加号按钮获得x的值和z的值并清空y的值 x=Double.parseDouble(text.getText().trim(); sss.setLength(0);/清空缓冲区以便接收新的另一个运算数 y=0d; z=0; /-else if(e.getSource()=c4)/单击减号按钮获得x的值和z的值并清空y的值 x=Double.parseDo

19、uble(text.getText().trim(); sss.setLength(0); y=0d; z=1; /*else if(e.getSource()=b18)/单击乘号按钮获得x的值和z的值并清空y的值 x=Double.parseDouble(text.getText().trim(); sss.setLength(0); y=0d; z=2; / /else if(e.getSource()=b13)/单击除号按钮获得x的值和z的值并空y的值 x=Double.parseDouble(text.getText().trim(); sss.setLength(0); y=0d;

20、z=3; /=else if(e.getSource()=c5)/单击等号按钮输出计算结果 sss.setLength(0); switch(z) case 0 : text.setText(""+(x+y);break; case 1 : text.setText(""+(x-y);break; case 2 : text.setText(""+(x*y);break; case 3 : text.setText(""+(x/y);break; / .else if(e.getSource()=c7)/单击&qu

21、ot;."按钮输入小数 if(text.getText().trim().indexOf(".")!=-1)/判断字符串中是否已经包含了小数点 else/如果没数点有小 if(text.getText().trim().equals("0")/如果初时显示为0 sss.setLength(0); text.setText(sss.append("0"+e.getActionCommand().toString(); else if(text.getText().trim().equals("")/如果初时显示为空则不做任何操作 else text.setText(sss.append(e.getActionCommand().toString(); y=0d; else if(e.getSource()=c6|e.getSource()=c1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论