免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025贵州省修文县事业单位招聘135人易考易错模拟试题(共500题)试卷后附参考答案
- 2025贵州毕节市国资委监管国企业“脱贫攻坚专项引才”招聘73人易考易错模拟试题(共500题)试卷后附参考答案
- 2025贵州兴园工业发展限公司所属子公司招聘27名合同制员工人员易考易错模拟试题(共500题)试卷后附参考答案
- 2025芜湖轨道交通维保项目招聘79人易考易错模拟试题(共500题)试卷后附参考答案
- 2025福建福州地铁招聘488名工作人员易考易错模拟试题(共500题)试卷后附参考答案
- 2025福建厦门翔安区区属国企业招聘工作人员67人易考易错模拟试题(共500题)试卷后附参考答案
- 2025福建南孚电池限公司招聘86人易考易错模拟试题(共500题)试卷后附参考答案
- 2025湖南衡阳市人才发展集团限公司招聘7人易考易错模拟试题(共500题)试卷后附参考答案
- 2025湖南常德烟草机械限责任公司招聘笔试易考易错模拟试题(共500题)试卷后附参考答案
- 2025湖北黄冈市人民政府行政复议委员会办公室招聘易考易错模拟试题(共500题)试卷后附参考答案
- 《舾装设计篇》课件
- 民事诉讼业务流程实训
- 退费账户确认书
- 智能制造工程生涯人物访谈
- (初级)招采人员能力评价培训强化模拟练习题库(500题)
- 河道边坡护坡工程施工方案
- 地基验槽表格
- 淤地坝安全运用管理制度
- GB/T 17772-2018土方机械保护结构的实验室鉴定挠曲极限量的规定
- GA 836-2009建设工程消防验收评定规则
- 复杂网络-课件
评论
0/150
提交评论