java图形用户界面实验报告

上传人:go****ng 文档编号:163465258 上传时间:2022-10-21 格式:DOC 页数:10 大小:127.51KB
返回 下载 相关 举报
java图形用户界面实验报告_第1页
第1页 / 共10页
java图形用户界面实验报告_第2页
第2页 / 共10页
java图形用户界面实验报告_第3页
第3页 / 共10页
点击查看更多>>
资源描述
南京工程学院实 验 报 告课程名称 JAVA基础 实验项目名称 图形用户界面设计 实验学生班级 实验学生姓名 学号 同组学生姓名 无 实验时间 2012年11月 实验地点 实验成绩评定 指导教师签字 年 月 日一、实验目的和要求1.目的:掌握java AWT及Swing组件的使用方法,包括窗口、框架、对话框、布局方式、面板、文本编辑器、按钮、组合框等,合理利用委托事件处理模型,掌握不同组件,不同事件的事件处理方法,设计出能够响应事件的java图形用户界面。2.要求:设计图形用户界面,事件处理,异常处理并弹出对话框,提示重新输入信息。二、实验题目验证哥德巴赫猜想,添加图形用户界面三、实验方法与步骤(需求分析、算法设计思路、流程图等)1.添加图形用户界面import java.awt.*;import java.awt.event.*;import javax.swing.*;public class GDBH extends Frame implements ActionListener /窗口框架响应单击事件,利用awt组件设计框架 private Button button; /按钮 private TextField text1; /文本行 private TextArea text2; /文本区 public GDBH() super(验证哥德巴赫猜想); /设置框架窗口标题 this.setBounds(450, 200, 350, 400); /设置框架的位置和尺寸 this.setResizable(false); / 设置框架大小不可改变 this.setBackground(Color.lightGray); / 设置窗口背景颜色 this.setLayout(new FlowLayout(); / 默认窗口流布局 中对齐 this.add(new Label(输入一个整数); text1=new TextField(,20); this.add(text1); button=new Button(验证哥德巴赫猜想); this.add(button); button.addActionListener(this); /为按钮注册单击事件监听器,委托当前对象处理事件 this.setLayout(new FlowLayout(); /默认中对齐 text2=new TextArea(20,25); text2.setEditable(false); this.add(text2); this.setVisible(true); /设置组件可见 this.addWindowListener(new WinClose(); / 为框架注册窗口事件监听器,委托WinClose对象处理事件 2.实现单击事件和窗口事件监听器接口 public void actionPerformed(ActionEvent e) /单击事件处理方法,实现ActionListener接口 String str =text1.getText(); /获得文本行的字符串 try long num = Long.parseLong(str); / 将字符串转化成长整形 text2.setText(yz(num); catch(NumberFormatException n) JOptionPane.showMessageDialog(this, +str+字符串不能转换成整数,请重新输入!); return; class WinClose implements WindowListener /实现窗口事件监听器接口 public void windowClosing(WindowEvent e) / 窗口关闭事件处理方法 System.exit(0); /程序运行结束 public void windowOpened(WindowEvent e) public void windowClosed(WindowEvent e) public void windowIconified(WindowEvent e) public void windowDeiconified(WindowEvent e) public void windowActivated(WindowEvent e) public void windowDeactivated(WindowEvent e) 3.异常处理若文本行中的字符串不能转化成长整形,将弹出对话框“字符串不能转换成整数,请重新输入!;另外如果所输入的整数为小于等于2或大于2小于等于5也会弹出一个对话框“输入错误!必须是大于2的偶数或大于5的奇数!”(该窗口代码设置在yz()函数中)。try long num = Long.parseLong(str); text2.setText(yz(num); catch(NumberFormatException n) JOptionPane.showMessageDialog(this, +str+字符串不能转换成整数,请重新输入!); 弹出对话框 return; 四、实验原始纪录(源程序、数据结构等)源程序如下import java.awt.*;import java.awt.event.*;import javax.swing.*;public class GDBH extends Frame implements ActionListener private Button button; private TextField text1; private TextArea text2; public GDBH() super(验证哥德巴赫猜想); this.setBounds(450, 200, 350, 400); this.setResizable(false); this.setBackground(Color.lightGray); this.setLayout(new FlowLayout(); this.add(new Label(输入一个整数); text1=new TextField(,20); this.add(text1); button=new Button(验证哥德巴赫猜想); this.add(button); button.addActionListener(this); this.setLayout(new FlowLayout(); text2=new TextArea(20,25); text2.setEditable(false); this.add(text2); this.setVisible(true); this.addWindowListener(new WinClose(); public static void main(String args) new GDBH(); public void actionPerformed(ActionEvent e) String str =text1.getText(); try long num = Long.parseLong(str); text2.setText(yz(num); catch(NumberFormatException n) JOptionPane.showMessageDialog(this, +str+字符串不能转换成整数,请重新输入!); return; public String yz(long num) String str1 = ; if(num2&num%2=0) for (long i = 2;i= num / 2; i+) for(long j=num/2;j5&num%2!=0) for(long i=2;inum;i+) for(long j=i;jnum;j+) for(long k=j;knum;k+) if (Prime(i) & Prime(j)&Prime(k) if(num=i+j+k) str1 += num + = + i + + + j + + +k+ ;n; else JOptionPane.showMessageDialog(this, 输入错误!必须是大于2的偶数或大于5的奇数!); return str1; public boolean Prime(long t) for(long i=2;it;i+) if(t%i=0) return false; return true; class WinClose implements WindowListener public void windowClosing(WindowEvent e) System.exit(0); public void windowOpened(WindowEvent e) public void windowClosed(WindowEvent e) public void windowIconified(WindowEvent e) public void windowDeiconified(WindowEvent e) public void windowActivated(WindowEvent e) public void windowDeactivated(WindowEvent e) 五、实验结果及分析(计算过程与结果、数据曲线、图表等)1.当文本行中的字符串不能转化成长整形,抛出异常2. 当输入的整数为小于等于2或大于2小于等于5,弹出对话框3.当输入偶数时,显示结果如图4.当输入奇数时,显示结果如图注:在函数yz()中,验证奇数时应规定3个素数的大小顺序(ijk),否则会出现数据重复六、实验总结与思考通过本次实验,基本上掌握了使用awt与swing组件设计图形用户界面的方法,包括对窗口,对话框等工具的合理利用。此外,在注册单击事件监听器与窗口事件监听器时,一定要注意响应事件,为其提供事件处理方法。另外,在输入字符串时,要注意抛出异常,当字符串不能转化成整数或所输入的整数不符合规则时,弹出对话框,显示输入错误,重新输入。还有,在窗口的文本区中,所显示的数据要注意其正确性与不重复性,在函数中设定3个素数的大小顺序就可保证数据不重复。教师评语:
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 工作总结


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!