




免费预览已结束,剩余6页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java实验三图形用户界面编程一实验目的1.学习编写带有图形化界面的程序。2.掌握JFrame、JLabel、JTextField、JButton和JPanel等组件的使用方法。3.掌握事件处理的方法。4.掌握常用的布局管理器。2 实验内容 2.1 程序要求1设计一个简单的图形化界面计算器程序,实现两个实数的四则运算。2创建2个文本字段,用于输入操作数;创建1个不可编辑的文本字段,用于输出运算结果;创建4个按钮,用于输入运算符。3对按钮出发动作进行处理。4将前面创建的组建添加到框架中,合理布局,给文本字段加上说明。 22.2 设计工作 界面设计:在panel上添加四个按钮,分别为+、-、*、/、=,再添加两个文本框分别输入和输出算式和结果。监听器监听按钮动作和回车操作。三思考题1AWT和Swing布局管理器和事件处理机制是什么样的?答:常用的布局管理器有:流布局,网格布局,卡片布局,边界布局,网格包布局。处理事件的机制:主要包含事件源,监听器,事件三部分。事件源一旦触发事件,系统便将事件的相关信息封装成对应类型的事件对象,并将事件对象发送给相应的监听器对象,监听器对象便能访问事件对象,对事件对象中的信息作出响应,执行相应的事件处理方法。四程序代码import java.awt.*;import java.awt.event.*;import javax.swing.*; /导入类包class simplecalculator static String point=new String();static String Amal=new String();static String ONE=new String();static String TWO=new String();static String THREE=new String();static String FOUR=new String();static String FIVE=new String();static String SIX=new String();static String SEVEN=new String();static String EIGHT=new String();static String NINE=new String();static String ZERO=new String();static String ResultState=new String();static Double QF;static JButton zero=new JButton(0);static JButton one=new JButton(1);static JButton two=new JButton(2);static JButton three=new JButton(3);static JButton four=new JButton(4);static JButton five=new JButton(5);static JButton six=new JButton(6);static JButton seven=new JButton(7);static JButton eight=new JButton(8);static JButton nine=new JButton(9);static JButton add=new JButton(+);static JButton sub=new JButton(-);static JButton mul=new JButton(*);static JButton div=new JButton(/);static JButton QuFan=new JButton(+/-);static JButton Dian=new JButton(.);static JButton equal=new JButton(=);static JButton clear=new JButton(C);static JButton BaiFen=new JButton(%);static JButton FenZhiYi=new JButton(1/x);static int i=0;static Double addNumber;static Double subNumber;static Double mulNumber;static Double divNumber;static Double equalNumber;static Double temp;static JTextArea result=new JTextArea(1,20);public static void main(String args)JFrame frame=new JFrame(计算器);result.setEditable(false);result.setText();ResultState=窗口空;JPanel ForResult=new JPanel();JPanel ForButton7_clear=new JPanel();JPanel ForButton4_mul=new JPanel();JPanel ForButton1_sub=new JPanel();JPanel ForButton0_equal=new JPanel();FlowLayout FLO=new FlowLayout();ForResult.add(result);ForButton7_clear.setLayout(FLO);ForButton7_clear.add(seven);ForButton7_clear.add(eight);ForButton7_clear.add(nine);ForButton7_clear.add(div);ForButton7_clear.add(clear);ForButton4_mul.setLayout(FLO);ForButton4_mul.add(four);ForButton4_mul.add(five);ForButton4_mul.add(six);ForButton4_mul.add(mul);ForButton4_mul.add(BaiFen);ForButton1_sub.setLayout(FLO);ForButton1_sub.add(one);ForButton1_sub.add(two);ForButton1_sub.add(three);ForButton1_sub.add(sub);ForButton1_sub.add(FenZhiYi);ForButton0_equal.setLayout(FLO);ForButton0_equal.add(zero);ForButton0_equal.add(QuFan);ForButton0_equal.add(Dian);ForButton0_equal.add(add);ForButton0_equal.add(equal);frame.getContentPane().setLayout(FLO);frame.getContentPane().add(ForResult);frame.getContentPane().add(ForButton7_clear);frame.getContentPane().add(ForButton4_mul);frame.getContentPane().add(ForButton1_sub);frame.getContentPane().add(ForButton0_equal);frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);frame.setBounds(250,250,245,245);frame.setResizable(false);frame.setVisible(true);clear.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)result.setText();ZERO=;ONE=;TWO=;THREE=;FOUR=;FIVE=;SIX=;SEVEN=;EIGHT=;NINE=;ResultState=窗口空;point=;i=0;);zero.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)ZERO=已经点击;ResultState=窗口不为空;if(ONE=已经点击|TWO=已经点击|THREE=已经点击|FOUR=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击)result.append(0);if(ResultState=窗口空)result.setText(0););one.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)ONE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(1);if(ResultState=窗口空)result.setText(1););two.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)TWO=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(2);if(ResultState=窗口空)result.setText(2););three.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)THREE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(3);if(ResultState=窗口空)result.setText(3); );four.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)FOUR=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(4);if(ResultState=窗口空)result.setText(4););five.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)FIVE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(5);if(ResultState=窗口空)result.setText(6););six.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)SIX=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(6);if(ResultState=窗口空)result.setText(6););seven.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)SEVEN=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(7);if(ResultState=窗口空)result.setText(7););eight.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)EIGHT=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(8);if(ResultState=窗口空)result.setText(8););nine.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)NINE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(9);if(ResultState=窗口空)result.setText(9););Dian.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)point=已经点击;i=i+1;if(ResultState=窗口不为空&i=1)result.append(.););add.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择加号;addNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);sub.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择减号;subNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);mul.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择乘号;mulNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);div.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择除号;divNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);QuFan.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)QF=new Double(Double.valueOf(result.getText().doubleValue();QF=QF*(-1);result.setTe
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《程控数字交换技术》课件第8章
- 威海市金猴集团有限责任公司校园招聘模拟试题附带答案详解学生专用
- 口腔科主任培训课件
- 2025中煤水文局集团有限公司社会化招聘13人笔试备考题库含答案详解(突破训练)
- 2023年山东济南市章丘区社区工作者(专职网格员)招考386人笔试备考试题及答案详解(必刷)
- XX企业数字化转型中的绿色制造与可持续发展报告
- 安全培训演练素材摘抄课件
- 酒店工程建造方案(3篇)
- 金湖工程围挡方案(3篇)
- 皮肤卫生的评估及护理
- 中考语文复习 议论文之论点及论证方法练习题(含答案)
- GB/T 28714-2023取水计量技术导则
- 供应商纠正预防措施报告
- 《插画》名师优质课获奖市赛课一等奖课件
- 一带一路详解
- 中小学英语课件-Go-away-Mr-Wolf
- 蛋白质-能量营养不良课件
- 1输变电工程施工质量验收统一表式(线路工程)
- 二年级语文上册《有趣的动物》课件PPT
- 不干胶贴标机设计学士学位论文
- 《劳动合同书》-河南省人力资源和社会保障厅劳动关系处监制(2016.11.15)
评论
0/150
提交评论