




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
电子信息学院实验报告书课程名: 面向对象程序设计(Java) 题 目: 实验05 可视化程序设计 实验类别: 【验证、设计】 班 级: BX1210 学 号: 28 姓 名: 俞佳星 评语:实验态度:认真( ) 一般( ) 较差( )实验结果:正确( ) 部分正确( ) 错( )实验理论:掌握( ) 熟悉( ) 了解( ) 生疏( )操作技能:较强( ) 一般( ) 较差( )实验报告:较好( ) 一般( ) 较差( )成绩: 指导教师: 王中华 批阅时间: 2014年4月22日面向对象程序设计(Java)实验报告 14 一、实验目的(1)熟悉GUI基本容器JFrame、JPanel等容器的基本使用方法;(2)熟悉布局管理的作用及其使用方法;(3)熟悉常用基本控件(按钮、文本框、标签、列表框、菜单)的使用;(4)重点掌握Java的事件管理机制和程序执行过程。二、实验内容(1)先运行程序下面程序,然后按【思考问题】来验证、分析并修改程序。以下源程序为ExpGui工程文件下的CalculatorUsingInnerClass.java。package .sdju.joshua.exp.gui.calculatorUsingInnerClass;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;SuppressWarnings(serial)class CalculatorUsingInnerClass extends JFrame JPanel topPal, cenPal, botPal;JTextField tfOprand1;JTextField tfOprand2;JTextField tfRst;JComboBox lstOperator;JLabel labAdd, labRst, labInfo;JButton btnOperate, btnReset;Container conPan = null;public CalculatorUsingInnerClass() topPal = new JPanel();topPal.setLayout(new FlowLayout();cenPal = new JPanel();cenPal.setLayout(new FlowLayout();botPal = new JPanel();botPal.setLayout(new FlowLayout();tfOprand1 = new JTextField(8);tfOprand2 = new JTextField(8);tfRst = new JTextField(8);String lstItem=+,-,*,/;lstOperator = new JComboBox(lstItem);labRst = new JLabel(=);btnOperate = new JButton(开始计算);btnOperate.setFont(new Font(楷体,Font.BOLD,16);btnReset = new JButton(清 除);labInfo = new JLabel();labInfo.setForeground(Color.RED);labInfo.setFont(new Font(黑体, Font.PLAIN, 16);conPan = this.getContentPane();conPan.setLayout(new BorderLayout();conPan.add(cenPal, BorderLayout.CENTER);conPan.add(topPal, BorderLayout.NORTH);conPan.add(botPal, BorderLayout.SOUTH);cenPal.add(tfOprand1);cenPal.add(lstOperator);cenPal.add(tfOprand2);cenPal.add(labRst);cenPal.add(tfRst);topPal.add(btnOperate);topPal.add(btnReset);botPal.add(labInfo);/ 设置窗口居中显示this.setLocationRelativeTo(null);this.setVisible(true);btnOperate.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) if(.equals(tfOprand1.getText().trim() |.equals(tfOprand1.getText().trim()labInfo.setText(加数和被加数均不允许为空!);elsedouble a = Double.valueOf(tfOprand1.getText().doubleValue();double b = Double.valueOf(tfOprand2.getText().doubleValue();char operator = (String)lstOperator.getSelectedItem().charAt(0);double result = 0;switch(operator)case +:result = a + b;break;case -:result = a - b;break;case *:result = a * b;break;case /:result = a / b;break;tfRst.setText(String.valueOf(result););btnReset.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) tfOprand1.setText();tfOprand2.setText();tfRst.setText(String.valueOf();labInfo.setText(以上文本框中的内容已清除!););this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););public static void main(String args) CalculatorUsingInnerClass mainFrame = new CalculatorUsingInnerClass();mainFrame.setSize(400, 150);mainFrame.setTitle(GUI实验简单算术运算器);【思考问题】 基本容器JFrame比起Frame有何特点?请解释构造方法中的conPan = this.getContentPane(); 语句是何作用? 如果将程序中的JFrame类换作Frame类,是否仍需要此条语句? 该程序中使用了哪几个容器,它们分别采用了什么布局管理器,在不同的布局管理器中,各组件加入容器中的次序对组件的显示位置有无影响? 程序中有多处地方用到this,这个this指什么? 分析事件执行方法public void actionPerformed(ActionEvent e)里面的下面三条语句的执行过程,它们分别起何作用?double a = Double.valueOf(tfOprand1.getText().doubleValue();double b = Double.valueOf(tfOprand2.getText().doubleValue();char operator = (String)lstOperator.getSelectedItem().charAt(0);该方法的形式参数ActionEvent e是何含义,对应的实参是如何形成的? 程序中对按钮事件的监听采用了匿名内部类的方法,请在不修改程序任何功能的前提下将程序修改成有名内部类监听的方式,原类名不变,内部类的名字为BtnHandlerUsingInnerClass,并使用该类的一个对象同时监听界面上的两个按钮。 请在不修改程序任何功能的前提下,将程序修改成外部类监听的方式,原类名不变,外部监听类的名字为BtnHandlerUsingExternalClass,并使用该类的一个对象同时监听界面上的两个按钮。 请在不修改程序任何功能的前提下,将程序修改成容器类监听的方式,原类名不变。 如果将程序中的new WindowAdapter()改写为new WindowListener(),则该匿名类的实现部分需要如何修改?三、实验结果(修改过的关键代码和运行结果截图)(1)JFrame与Frame的最大区别在于前者不能直接通过add()方法加入组件,也不能通过setLayout()方法设置布局。每个JFame都有一个与之关联的内容面板(contentPane),只能针对这个contentPane设置布局及加入组件。如果将JFrame类换做Frame类,则不再需要这条语句。这个程序中使用了一个Contaniner容器和3个JPanel容器。Contaniner采用了便捷布局管理器。边界布局管理器需要设置插入的位置,如果插入相同位置,先前插入的组件会被覆盖。3个JPanel采用了流失布局管理器。流失布局管理器按照从左到右的先后顺序布置在容器中。如果一行布置不下,则会自动换行。流失布局管理器中插入的顺序决定了组件在容器中的大致位置。程序中多次用到的this是指向CalculatorUsingInnerClas这个类的引用。通过this这个引用,我们可以直接对这个窗体进行设置。第一条语句先用getText()获得tfOprand1中的文本。然后再用Double.valueOf()将String类型的内容转化成double型数值。最后将Double对象的值用doubleValue()转化成double基本数据类型给a。第二条语句和第一条语句一样,将tfOprand2的文本内容转成double型数值给b。第三条语句先用getSelectedItem()获得lsOperator中选择的内容,再将所获得内容强制转化成String类型。最后再通过charAt()方法获得该字符串中的第一个字符,并返回给operator。package .sdju.no28.johnson;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;SuppressWarnings(serial)class CalculatorUsingInnerClass extends JFrame JPanel topPal, cenPal, botPal;JTextField tfOprand1;JTextField tfOprand2;JTextField tfRst;JComboBox lstOperator;JLabel labAdd, labRst, labInfo;JButton btnOperate, btnReset;Container conPan = null;public CalculatorUsingInnerClass() topPal = new JPanel();topPal.setLayout(new FlowLayout();cenPal = new JPanel();cenPal.setLayout(new FlowLayout();botPal = new JPanel();botPal.setLayout(new FlowLayout();tfOprand1 = new JTextField(8);tfOprand2 = new JTextField(8);tfRst = new JTextField(8);String lstItem=+,-,*,/;lstOperator = new JComboBox(lstItem);labRst = new JLabel(=);btnOperate = new JButton(开始计算);btnOperate.setFont(new Font(楷体,Font.BOLD,16);btnReset = new JButton(清 除);labInfo = new JLabel();labInfo.setForeground(Color.RED);labInfo.setFont(new Font(黑体, Font.PLAIN, 16);conPan = this.getContentPane();conPan.setLayout(new BorderLayout();conPan.add(cenPal, BorderLayout.CENTER);conPan.add(topPal, BorderLayout.NORTH);conPan.add(botPal, BorderLayout.SOUTH);cenPal.add(tfOprand1);cenPal.add(lstOperator);cenPal.add(tfOprand2);cenPal.add(labRst);cenPal.add(tfRst);topPal.add(btnOperate);topPal.add(btnReset);botPal.add(labInfo);/ 设置窗口居中显示this.setLocationRelativeTo(null);this.setVisible(true);BtnHandlerUsingInnerClass ALx=new BtnHandlerUsingInnerClass();btnOperate.addActionListener(ALx);btnReset.addActionListener(ALx);class BtnHandlerUsingInnerClass implements ActionListenerOverridepublic void actionPerformed(ActionEvent e) if(e.getActionCommand().compareTo(开始计算)=0)if(.equals(tfOprand1.getText().trim() |.equals(tfOprand1.getText().trim()labInfo.setText(加数和被加数均不允许为空!);else double a = Double.valueOf(tfOprand1.getText().doubleValue();double b = Double.valueOf(tfOprand2.getText().doubleValue();char operator = (String)lstOperator.getSelectedItem().charAt(0);double result = 0;switch(operator)case +:result = a + b;break;case -:result = a - b;break;case *:result = a * b;break;case /:result = a / b;break;tfRst.setText(String.valueOf(result);elsetfOprand1.setText();tfOprand2.setText();tfRst.setText(String.valueOf();labInfo.setText(以上文本框中的内容已清除!);public static void main(String args) CalculatorUsingInnerClass mainFrame = new CalculatorUsingInnerClass();mainFrame.setSize(400, 150);mainFrame.setTitle(GUI实验简单算术运算器);package .sdju.no28.johnson;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;SuppressWarnings(serial)class CalculatorUsingInnerClass extends JFrame JPanel topPal, cenPal, botPal;JTextField tfOprand1;JTextField tfOprand2;JTextField tfRst;JComboBox lstOperator;JLabel labAdd, labRst, labInfo;JButton btnOperate, btnReset;Container conPan = null;public CalculatorUsingInnerClass() topPal = new JPanel();topPal.setLayout(new FlowLayout();cenPal = new JPanel();cenPal.setLayout(new FlowLayout();botPal = new JPanel();botPal.setLayout(new FlowLayout();tfOprand1 = new JTextField(8);tfOprand2 = new JTextField(8);tfRst = new JTextField(8);String lstItem=+,-,*,/;lstOperator = new JComboBox(lstItem);labRst = new JLabel(=);btnOperate = new JButton(开始计算);btnOperate.setFont(new Font(楷体,Font.BOLD,16);btnReset = new JButton(清 除);labInfo = new JLabel();labInfo.setForeground(Color.RED);labInfo.setFont(new Font(黑体, Font.PLAIN, 16);conPan = this.getContentPane();conPan.setLayout(new BorderLayout();conPan.add(cenPal, BorderLayout.CENTER);conPan.add(topPal, BorderLayout.NORTH);conPan.add(botPal, BorderLayout.SOUTH);cenPal.add(tfOprand1);cenPal.add(lstOperator);cenPal.add(tfOprand2);cenPal.add(labRst);cenPal.add(tfRst);topPal.add(btnOperate);topPal.add(btnReset);botPal.add(labInfo);/ 设置窗口居中显示this.setLocationRelativeTo(null);this.setVisible(true);BtnHandlerUsingExternalClass ALx=new BtnHandlerUsingExternalClass(this);btnOperate.addActionListener(ALx);btnReset.addActionListener(ALx);public static void main(String args) CalculatorUsingInnerClass mainFrame = new CalculatorUsingInnerClass();mainFrame.setSize(400, 150);mainFrame.setTitle(GUI实验简单算术运算器);class BtnHandlerUsingExternalClass implements ActionListenerCalculatorUsingInnerClass mainFrame;public void actionPerformed(ActionEvent e) if(e.getActionCommand().compareTo(开始计算)=0)if(.equals(mainFrame.tfOprand1.getText().trim() |.equals(mainFrame.tfOprand1.getText().trim()mainFrame.labInfo.setText(加数和被加数均不允许为空!);else double a = Double.valueOf(mainFrame.tfOprand1.getText().doubleValue();double b = Double.valueOf(mainFrame.tfOprand2.getText().doubleValue();char operator = (String)mainFrame.lstOperator.getSelectedItem().charAt(0);double result = 0;switch(operator)case +:result = a + b;break;case -:result = a - b;break;case *:result = a * b;break;case /:result = a / b;break;mainFrame.tfRst.setText(String.valueOf(result);elsemainFrame.tfOprand1.setText();mainFrame.tfOprand2.setText();mainFrame.tfRst.setText(String.valueOf();mainFrame.labInfo.setText(以上文本框中的内容已清除!);public BtnHandlerUsingExternalClass(CalculatorUsingInnerClass mainFrame) this.mainFrame = mainFrame;package .sdju.no28.johnson;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;SuppressWarnings(serial)class CalculatorUsingInnerClass extends JFrame implements ActionListenerJPanel topPal, cenPal, botPal;JTextField tfOprand1;JTextField tfOprand2;JTextField tfRst;JComboBox lstOperator;JLabel labAdd, labRst, labInfo;JButton btnOperate, btnReset;Container conPan = null;public CalculatorUsingInnerClass() topPal = new JPanel();topPal.setLayout(new FlowLayout();cenPal = new JPanel();cenPal.setLayout(new FlowLayout();botPal = new JPanel();botPal.setLayout(new FlowLayout();tfOprand1 = new JTextField(8);tfOprand2 = new JTextField(8);tfRst = new JTextField(8);String lstItem=+,-,*,/;lstOperator = new JComboBox(lstItem);labRst = new JLabel(=);btnOperate = new JButton(开始计算);btnOperate.setFont(new Font(楷体,Font.BOLD,16);btnReset = new JButton(清 除);labInfo = new JLabel();labInfo.setForeground(Color.RED);labInfo.setFont(new Font(黑体, Font.PLAIN, 16);conPan = this.getContentPane();conPan.setLayout(new BorderLayout();conPan.add(cenPal, BorderLayout.CENTER);conPan.add(topPal, BorderLayout.NORTH);conPan.add(botPal, BorderLayout.SOUTH);cenPal.add(tfOprand1);cenPal.add(lstOperator);cenPal.add(tfOprand2);cenPal.add(labRst);cenPal.add(tfRst);topPal.add(btnOperate);topPal.add(btnReset);botPal.add(labInfo);/ 设置窗口居中显示this.setLocationRelativeTo(null);this.setVisible(true);btnOperate.addActionListener(this);btnReset.addActionListener(this);public static void main(String args) CalculatorUsingInnerClass mainFrame = new CalculatorUsingInnerClass();mainFrame.setSize(400, 150);mainFrame.setTitle(GUI实验简单算术运算器);public void actionPerformed(ActionEvent arg0) if(arg0.getActionCommand().compareTo(开始计算)=0)if(.equals(tfOprand1.getText().trim() |.equals(tfOprand1.getText().trim()labInfo.setText(加数和被加数均不允许为空!);e
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 初中数学第2课时++用计算器进行运算课件+北师大版七年级数学上册
- 人教版八年级物理上册 第四章《光的反射》分层作业练习题
- 收银员高级工练习题库及参考答案
- 重庆介绍课件
- 老年人穿衣训练课件
- 老年人的家庭护理课件
- 《英语语法2》课程介绍与教学大纲
- 老年人春节防护知识培训课件
- 老年人急救知识培训内容课件
- 己亥杂诗课件
- 征兵心理测试题及答案
- 高温中暑急救教学
- 妇科临床科室管理制度
- 直销团队文化课件
- 广西南宁市三中2025届高三第二次模拟考试英语试卷含解析
- 五年级体育课教案全集
- 新审计法知识讲解课件
- 幼儿教育幼儿园安全知识教育试题
- 哮喘患儿自我管理指导
- 2022学年上海复旦附中高一(上)期末信息技术试题及答案
- 数学思维与问题解决能力-深度研究
评论
0/150
提交评论