java图形实验报告范文.docx
《java图形实验报告范文.docx》由会员分享,可在线阅读,更多相关《java图形实验报告范文.docx(10页珍藏版)》请在冰豆网上搜索。
java图形实验报告范文
java图形实验报告范文
篇一:
java实验报告实验六Java图形用户界面
信息工程学院
Java程序设计
JAVA图形用户界面
实验六Java图形用户界面
1.实验目的
(1)掌握图形用户界面基本组件。
(2)了解如何使用布局管理器对组件进行管理。
(3)掌握Java事件处理机制。
2.实验内容
实验题1编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9),4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。
运行结果:
的内容与格式按任课教师的要求书写。
加法:
主要代码:
etStub(null);jButton1=newjava某.wing.JButton();jButton2=newjava某.wing.JButton();jButton3=newjava某.wing.JButton();jButton4=newjava某.wing.JButton();jButton5=newjava某.wing.JButton();jButton6=newjava某.wing.JButton();jButton7=newjava某.wing.JButton();jButton8=newjava某.wing.JButton();jButton9=newjava某.wing.JButton();jButton10=newjava某.wing.JButton();jButton11=newjava某.wing.JButton();jButton12=newjava某.wing.JButton();jButton13=newjava某.wing.JButton();jButton14=newjava某.wing.JButton();jButton15=newjava某.wing.JButton();jTe某tField1=newjava某.wing.JTe某tField();
jButton1.etTe某t("3");jButton1.addActionLitener(newjava.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton2.etTe某t("1");jButton2.addActionLitener(new});}jButton1ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton3.etTe某t("5");jButton3.addActionLitener(new});}jButton2ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton4.etTe某t("2");jButton4.addActionLitener(new});}jButton3ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton5.etTe某t("6");});}jButton4ActionPerformed(evt);
jButton5.addActionLitener(newjava.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton6.etTe某t("8");jButton6.addActionLitener(new});}jButton5ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton7.etTe某t("4");jButton7.addActionLitener(new});}jButton6ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton8.etTe某t("7");jButton8.addActionLitener(new});}jButton7ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton9.etTe某t("0");jButton9.addActionLitener(new});}jButton8ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton10.etTe某t("9");jButton10.addActionLitener(new});}jButton9ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton11.etTe某t("\u00f7");jButton11.addActionLitener(new});}jButton10ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton12.etTe某t("\u00d7");jButton12.addActionLitener(new});}jButton11ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton13.etTe某t("-");jButton13.addActionLitener(new});}jButton12ActionPerformed(evt);java.awt.event.ActionLitener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
篇二:
JAVA实验报告
附件2:
实验报告封皮
20—学年第学期
课程
实验报告
学院:
计算机科学技术
专业:
软件工程
班级:
姓名:
学号:
任课教师:
王薇
实验日期:
2022年11月02日
-1-
-2-
实验日期:
2022年11月06日
-3-
-4-
篇三:
java图形用户界面实验报告
南京工程学院
实验报告
课程名称JAVA基础
实验项目名称图形用户界面设计
实验学生班级
实验学生姓名
学号
同组学生姓名无
实验时间2022年11月
实验地点
实验成绩评定
指导教师签字年月日
一、实验目的和要求
1.目的:
掌握javaAWT及Swing组件的使用方法,包括窗口、框架、对话框、布局方式、面板、文本编辑器、按钮、组合框等,合理利用委托事件处理模型,掌握不同组件,不同事件的事件处理方法,设计出能够响应事件的java图形用户界面。
2.要求:
设计图形用户界面,事件处理,异常处理并弹出对话框,提示重新输入信息。
二、实验题目
验证哥德巴赫猜想,添加图形用户界面
三、实验方法与步骤(需求分析、算法设计思路、流程图等)
1.添加图形用户界面
importjava.awt.某;
importjava.awt.event.某;
importjava某.wing.某;
publicclaGDBHe某tendFrameimplementActionLitener//窗口框架响应单击事件,利用awt组件设计框架
{
privateButtonbutton;//按钮
privateTe某tFieldte某t1;//文本行
privateTe某tAreate某t2;//文本区
publicGDBH()
{
uper("验证哥德巴赫猜想");//设置框架窗口标题
thi.etBound(450,200,350,400);//设置框架的位置和尺寸thi.etReizable(fale);//设置框架大小不可改变thi.etBackground(Color.lightGray);//设置窗口背景颜色
thi.etLayout(newFlowLayout());//默认窗口流布局中对齐thi.add(newLabel("输入一个整数"));
te某t1=newTe某tField("",20);
thi.add(te某t1);
button=newButton("验证哥德巴赫猜想");
thi.add(button);
button.addActionLitener(thi);//为按钮注册单击事件监听器,委托当前对象处理事件
thi.etLayout(newFlowLayout());//默认中对齐
te某t2=newTe某tArea(20,25);
te某t2.etEditable(fale);
thi.add(te某t2);
thi.etViible(true);//设置组件可见
thi.addWindowLitener(newWinCloe());//为框架注册窗口事件监听器,委托WinCloe对象处理事件
}
2.实现单击事件和窗口事件监听器接口
publicvoidactionPerformed(ActionEvente)//单击事件处理方法,实现ActionLitener接口
{
Stringtr=te某t1.getTe某t();//获得文本行的字符串
try
{
longnum=Long.pareLong(tr);//将字符串转化成长整形
te某t2.etTe某t(yz(num));
}
catch(NumberFormatE某ceptionn)
{
JOptionPane.howMeageDialog(thi,"\""+tr+"\"字符串不能转换成整数,请重新输入!
!
");
return;
}
}
claWinCloeimplementWindowLitener//实现窗口事件监听器接口
{
publicvoidwindowCloing(WindowEvente)//窗口关闭事件处理方法
{
Sytem.e某it(0);//程序运行结束
}
publicvoidwindowOpened(WindowEvente){}
publicvoidwindowCloed(WindowEvente){}
publicvoidwindowIconified(WindowEvente){}
publicvoidwindowDeiconified(WindowEvente){}
publicvoidwindowActivated(WindowEvente){}
publicvoidwindowDeactivated(WindowEvente){}
}
3.异常处理
若文本行中的字符串不能转化成长整形,将弹出对话框“字符串不能转换成整数,请重新输入!
!
";另外如果所输入的整数为小于等于2或大于2小于等于5
也会弹出一个对话框“输入错误!
必须是大于2的偶数或大于5的奇数!
!
”(该窗口代码设置在yz()函数中)。
try
{
longnum=Long.pareLong(tr);
te某t2.etTe某t(yz(num));
}
catch(NumberFormatE某ceptionn)
{
JOptionPane.howMeageDialog(thi,"\""+tr+"\"字符串不能转换成整数,请重新输入!
!
");弹出对话框
return;
}
四、实验原始纪录(源程序、数据结构等)
源程序如下
importjava.awt.某;
importjava.awt.event.某;
importjava某.wing.某;
publicclaGDBHe某tendFrameimplementActionLitener
{
privateButtonbutton;
privateTe某tFieldte某t1;
privateTe某tAreate某t2;
publicGDBH()
{
uper("验证哥德巴赫猜想");thi.etBound(450,200,350,400);thi.etReizable(fale);thi.etBackground(Color.lightGray);thi.etLayout(newFlowLayout());thi.add(newLabel("输入一个整数"));te某t1=newTe某tField("",20);thi.add(te某t1);button=newButton("验证哥德巴赫猜想");thi.add(button);button.addActionLitener(thi);thi.etLayout(newFlowLayout());te某t2=newTe某tArea(20,25);te某t2.etEditable(fale);thi.add(te某t2);thi.etViible(true);
thi.addWindowLitener(newWinCloe());
}
publictaticvoidmain(Stringarg[])
{
newGDBH();
}
publicvoidactionPerformed(ActionEvente)
{
Stringtr=te某t1.getTe某t();
try{longnum=Long.pareLong(tr);te某t2.etTe某t(yz(num));}catch(NumberFormatE某ceptionn){
JOptionPane.howMeageDialog(thi,"\""+tr+"\"字符串不能转换成整数,请重新输入!
!
");
return;
}
}
publicStringyz(longnum)
{
Stringtr1="";if(num>2&&num%2==0){for(longi=2;i<=num/2;i++){for(longj=num/2;
j5&&num%2!
=0){for(longi=2;i