java图形用户界面实验报告.doc
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(num>2&&num%2==0){ for (long i = 2;i5&&num%2!=0){for(long i=2;i 此外,在注册单击事件监听器与窗口事件监听器时,一定要注意响应事件,为其提供事件处理方法另外,在输入字符串时,要注意抛出异常,当字符串不能转化成整数或所输入的整数不符合规则时,弹出对话框,显示输入错误,重新输入还有,在窗口的文本区中,所显示的数据要注意其正确性与不重复性,在函数中设定 3 个素数的大小顺序就可保证数据不重复教师评语:。





