南京工程学院
实 验 报 告
课程名称 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;i<= num / 2; i++)
{
for(long j=num/2;j<num;j++)
{
if (Prime(i) && Prime(j))
{
if(num==i+j)
{
str1+=num + " = " + i + " + " + j + ";\n";
}
}
}
}
}
else if(num>5&&num%2!=0)
{
for(long i=2;i<num;i++)
{
for(long j=i;j<num;j++)
{
for(long k=j;k<num;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;i<t;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个素数的大小顺序(i≤j≤k),否则会出现数据重复
六、实验总结与思考
通过本次实验,基本上掌握了使用awt与swing组件设计图形用户界面的方法,包括对窗口,对话框等工具的合理利用。此外,在注册单击事件监听器与窗口事件监听器时,一定要注意响应事件,为其提供事件处理方法。另外,在输入字符串时,要注意抛出异常,当字符串不能转化成整数或所输入的整数不符合规则时,弹出对话框,显示输入错误,重新输入。还有,在窗口的文本区中,所显示的数据要注意其正确性与不重复性,在函数中设定3个素数的大小顺序就可保证数据不重复。
教师评语:
第二篇:昆明理工大学 java 实验报告 图形用户界面
昆明理工大学信息工程与自动化学院学生实验报告
( 2011 —2012 学年第 1 学期)
课程名称:java程序设计 开课实验室:信自楼机房445 20## 年11月 11日
一、上机目的及内容
上机内容:
把一个数转为二、十六、八进制,用awt组建实现窗口。
上机目的:
掌握java swing之间的使用方法,理解委托事件处理模型,掌握多种不及方式,掌握窗口菜单的快捷菜单设计方式,熟悉在组建上绘图的方法,设计出具有图形用户界面的、能够显影事件的java程序。
二、实验原理及基本技术路线图(方框原理图或程序流程图)
1.本程序使用【例题6.2】的【Unicode字符查询器】修改而成。
2.保留了QueryFrame类的基本构造方法但做了适当修改,框架变为5行2列,4个文本行,2个按钮。
3.进制转换部分调用了java API中的 Integer.toHexString(int),Integer.toOctalString(int),Integer.toBinaryString(int) 函数,实现八十进制数转为二、八、十六进制数字。
4.当点击鼠标产生事件e,判断点击的按钮,若为button_char按钮,则执行以上三个函数,并将结果分别输出到text_bin1、text_oct1、text_hex1三个文本框。
5.若点击button_dw按钮,则调用MessageJDialog类的show方法,弹出提示框。
三、所用仪器、材料(设备名称、型号、规格等或使用软件)
1台PC及eclipse软件
四、实验方法、步骤(或:程序代码或操作过程)
//用户图形界面实现进制转换。
import java.awt.*;
import java.awt.event.*;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
publicclassQueryFrameextends Frame implements ActionListener //框架窗口响应单击事件
{
privatestaticfinal JFrame QueryFrame = null;
private TextField text_dec,text_bin,text_oct,text_hex; //两个文本行
private Button button_char, button_dw; //两个按钮
private MessageJDialog jdialog;
public QueryFrame()
{
super("Unicode字符查询器"); //窗口标题
this.setBounds(300,240,300,400); //设置框架的位置和尺寸
this.setBackground(Color.lightGray); //设置框架的背景颜色
this.setLayout(new FlowLayout(FlowLayout.RIGHT)); //框架流布局且左对齐
this.setLayout(new GridLayout(5,2)); //框架流布局且左对齐
this.add(new Label("输入十进制数"));
text_dec = new TextField("",20);
this.add(text_dec);
this.add(new Label("转换为二进制"));
text_bin = new TextField("",20);
this.add(text_bin);
this.add(new Label("转换为八进制"));
text_oct = new TextField("",20);
this.add(text_oct);
this.add(new Label("转换为十六进制"));
text_hex = new TextField("",20);
this.add(text_hex);
button_char = new Button("转换");
this.add(button_char);
button_char.addActionListener(this); //为按钮注册单击事件监听器
button_dw = new Button("敢不敢点我!");
this.add(button_dw);
button_dw.addActionListener(this);
jdialog = new MessageJDialog(this);//为框架注册窗口事件监听器,委托WinClose类的对象处理事件
this.setVisible(true); //显示框架
this.addWindowListener(new WinClose());
}
privateclassMessageJDialogextends JDialog //内部类,显示提示信息的对话框,通用功能
{
private Frame frame; //对话框所依附的框架窗口
private JLabel jlabel; //对话框中的显示信息
public MessageJDialog(Frame frame) //内部类的构造方法
{
super(frame,"提示",true); //模式窗口
this.frame = frame;
this.setSize(300,80);
jlabel = new JLabel("", JLabel.CENTER); //标签的字符串为空,居中对齐
this.getContentPane().add(jlabel);
this.setDefaultCloseOperation(HIDE_ON_CLOSE); //单击对话框的关闭按钮时,隐藏对话框而不结束程序运行
}
publicvoid show(String message) //对话框显示信息
{
jlabel.setText(message);
this.setLocation(frame.getX()+100, frame.getY()+100);//对话框位置在框架的右下方
this.setVisible(true); //显示对话框
}
}//MessageJDialog内部类结束
publicvoid actionPerformed(ActionEvent e) //单击事件处理方法,实现ActionListener接口
{
if (e.getSource()==button_char) //e.getSource()获得当前事件源组件,比较引用
{
int dec = Integer.parseInt(text_dec.getText());
String text_bin1 = Integer.toBinaryString(dec);
text_bin.setText(""+text_bin1);
String text_oct1 = Integer.toOctalString(dec);
text_oct.setText(""+text_oct1);
String text_hex1 = Integer.toHexString(dec);
text_hex.setText(""+text_hex1);
}
if (e.getSource()==button_dw)
{
jdialog.show("(⊙o⊙)...你还真敢啊!好吧,我叫孙浩川...");
}
}
publicstaticvoid main(String arg[])
{
new QueryFrame();
}
}
class WinClose implements WindowListener //实现窗口事件监听器接口
{
publicvoid windowClosing(WindowEvent e) //窗口关闭事件处理方法
{
System.exit(0); //结束程序运行
}
publicvoid windowOpened(WindowEvent e) { }
publicvoid windowActivated(WindowEvent e) { }
publicvoid windowDeactivated(WindowEvent e) { }
publicvoid windowClosed(WindowEvent e) { }
publicvoid windowIconified(WindowEvent e) { }
publicvoid windowDeiconified(WindowEvent e) { }
}
/*
或者
class WinClose extends WindowAdapter //继承适配类
{
public void windowClosing(WindowEvent e) //单击窗口关闭按钮时触发并执行
{ //覆盖WindowAdapter类中方法
System.exit(0); //结束程序运行
}
}
*/
五、实验过程原始记录( 测试数据、图表、计算等)
1.窗口界面
2.输入一个十进制数
3.点击转换按钮执行进制转换
4.点击右侧“敢不敢点我”按钮弹出窗口
六、实验结果、分析和结论(误差分析与数据处理、成果总结等。其中,绘制曲线图时必须用计算纸或程序运行结果、改进、收获)
图形用户界面提供应用程序与用户进行数据交流的界面。图形用户界面由组件组成,组件需要响应约定的事件。Java在java.awt个javax.swing包中提供构成java图形用户界面的组件类和事件类。这次试验中了解了java组建,包括窗口、框架、面板、文本编辑框、按钮、组合框、列表框等。用进制转换的例子,实现了用于控制组件相对位置的布局管理器、委托时间处理模型等组建。本章所学习内容对以后实际应用有非常重要的作用,通过本次试验,对java.awt包有了基本的了解,在今后的学习中要更加重视。
注:教师必须按照上述各项内容严格要求,认真批改和评定学生成绩。