ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:119.86KB ,
资源ID:9438228      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9438228.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(组件实验.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

组件实验.docx

1、组件实验姓名 学号 班级 年级 指导教师 西安财经学院信息学院 Java程序设计 实验报告实验名称 组件的应用 实验室 522 实验日期 2014年 月 日 组件的应用一、 实验目的1、 熟悉和理解Java中AWT及Swing,能够设计简单美观的用户界面。2、 掌握使用Java中图形界面设计的组件及事件处理。二、 实验内容 1、编写程序,创建一个Frame,添加相关组件,实现简易计算器的基本功能。三、 实验环境1. 硬件:一台微机2. 软件:操作系统和java编译器四、 实验步骤1、 设置计算器的布局按钮button面板panel(文本框+按键区)框架frameJFrame frame=new

2、 JFrame(简易mini计算器); /创建一个JFrame的实例 JPanel p1=new JPanel(); JPanel p2=new JPanel(); /创建两个JPanelJTextField t1=new JTextField(25); /创建一个文本框 JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16; /设置计算器键盘按钮/为框架设置BorderLayout布局管理器 frame.setLayout(new BorderLayout(4,4); /将计算器键盘面板p1放入框架 frame.a

3、dd(p1); /将文本框p2放入框架的顶部 frame.add(p2,BorderLayout.NORTH); /为p1设置4行4列的GridLayout布局管理器 p1.setLayout(new GridLayout(4,4,4,4); /为p2设置BorderLayout布局管理器 p2.setLayout(new BorderLayout(); 2、 对程序设置监听程序当按动按钮时,需要对事件作出响应,将内容显示到文本框中frame.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent

4、e) System.exit(0); );b0.addActionListener(new ActionListener() b1.addActionListener(new ActionListener() b2.addActionListener(new ActionListener()3、 完善计算器内部算法显示数字算法:public void actionPerformed(ActionEvent e) s+=1; s2+=1; t1.setText(s); 算数运算: public void actionPerformed(ActionEvent e) s1=s; s+=+; id=

5、1; s2=; t1.setText(s); 清除内容算法:public void actionPerformed(ActionEvent e) if(id1) ; else s+=; double a=Double.parseDouble(s1); double b=Double.parseDouble(s2); double c=0; switch(id) case 1:c=a+b; break; case 2:c=a-b; break; case 3:c=a*b; break; case 4:c=a/b; break; s+=c; t1.setText(s); 4、 调试程序,实现结果五

6、、 实验结果 六、 小结通过这次实验,我收获了许多知识同时发现了不少的问题。熟悉和理解了Java中AWT及Swing,能够设计简单美观的用户界面,还掌握了Java中图形界面设计的组件及事件处理的方法,达到了实验的目的。虽然在实验的过程中遇到一些问题,但大部分通过查阅资料解决了。在运行结果出现时,出现了如图所示的情况,结果正确,但原因有待探索七、 源程序清单import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.text.*; public class counter int id=0;

7、/设置标记 String s=,s1=null,s2=null; JFrame frame=new JFrame(简易mini计算器); /创建一个JFrame的实例 JPanel p1=new JPanel(); JPanel p2=new JPanel(); /创建两个JPanel JTextField t1=new JTextField(25); /创建一个文本框 JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16; /设置计算器键盘按钮 public static void main(String args

8、) counter that=new counter(); that.go(); public void go() /为框架设置BorderLayout布局管理器 frame.setLayout(new BorderLayout(4,4); /将计算器键盘面板p1放入框架 frame.add(p1); /将文本框p2放入框架的顶部 frame.add(p2,BorderLayout.NORTH); /为p1设置4行4列的GridLayout布局管理器 p1.setLayout(new GridLayout(4,4,4,4); /为p2设置BorderLayout布局管理器 p2.setLayo

9、ut(new BorderLayout(); /设置按钮名称及颜色 b0=new JButton(1); b1=new JButton(2); b2=new JButton(3); b3=new JButton(+);b3.setForeground(Color.red); b4=new JButton(4); b5=new JButton(5); b6=new JButton(6); b7=new JButton(-);b7.setForeground(Color.red); b8=new JButton(7); b9=new JButton(8); b10=new JButton(9);

10、b11=new JButton(*);b11.setForeground(Color.red); b12=new JButton(0); b13=new JButton(.); b14=new JButton(=);b14.setForeground(Color.green); b15=new JButton(/);b15.setForeground(Color.red); b16=new JButton(clean);b16.setForeground(Color.blue); p2.add(t1);/将文本框添加到p2面板中 /将b0-b15按钮添加到p1面板中 p1.add(b0); p

11、1.add(b1); p1.add(b2); p1.add(b3); p1.add(b4); p1.add(b5); p1.add(b6); p1.add(b7); p1.add(b8); p1.add(b9); p1.add(b10); p1.add(b11); p1.add(b12); p1.add(b13); p1.add(b14); p1.add(b15); /将b16按钮添加到框架的下方 frame.getContentPane().add(b16,BorderLayout.SOUTH); frame.setSize(300,300); /设置框架大小 frame.pack(); f

12、rame.setVisible(true);/显示计算器 /为按钮添加监听程序 frame.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); b0.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=1; s2+=1; t1.setText(s); ); b1.addActionListener(new ActionListener(

13、) public void actionPerformed(ActionEvent e) s+=2; s2+=2; t1.setText(s); ); b2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=3; s2+=3; t1.setText(s); ); b4.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=4; s2+=4; t1.setText

14、(s); ); b5.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=5; s2+=5; t1.setText(s); ); b6.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=6; s2+=6; t1.setText(s); ); b8.addActionListener(new ActionListener() public void action

15、Performed(ActionEvent e) s+=7; s2+=7; t1.setText(s); ); b9.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=8; s2+=8; t1.setText(s); ); b10.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=9; s2+=9; t1.setText(s); ); b12.addActi

16、onListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=0; s2+=0; t1.setText(s); ); b13.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=.; s2+=.; t1.setText(s); ); b3.addActionListener(new ActionListener() public void actionPerformed(ActionE

17、vent e) s1=s; s+=+; id=1; s2=; t1.setText(s); ); b7.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s1=s; s+=-; id=2; s2=; t1.setText(s); ); b11.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s1=s; s+=*; id=3; s2=; t1.setText(s); )

18、; b15.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s1=s; s+=/; id=4; s2=; t1.setText(s); ); b16.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s=; s2=; t1.setText(s); ); b14.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(id1) ; else s+=; double a=Double.parseDouble(s1); double b=Double.parseDouble(s2); double c=0; switch(id) case 1:c=a+b; break; case 2:c=a-b; break; case 3:c=a*b; break; case 4:c=a/b; break; s+=c; t1.setText(s); ); 窗体底端

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

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