java图形实验报告.docx
《java图形实验报告.docx》由会员分享,可在线阅读,更多相关《java图形实验报告.docx(9页珍藏版)》请在冰豆网上搜索。
java图形实验报告
java图形实验报告
篇一:
java实验报告实验六Java图形用户界面
信息工程学院
Java程序设计实习报告
JAVA图形用户界面
实验六Java图形用户界面
1.实验目的
(1)把握图形用户界面大体组件。
(2)了解如何利用布局治理器对组件进行治理。
(3)把握Java事件处置机制。
2.实验内容
实验题1编写一个模拟计算器的程序,利用面板和网格布局,添加一个文本框,10个数字按钮(0-9),4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。
运行结果:
实验报告的内容与格式按任课教师的要求书写。
加法:
要紧代码:
privatevoidinitComponents(){
setStub(null);jButton1=newjavax.swing.JButton();jButton2=newjavax.swing.JButton();jButton3=newjavax.swing.JButton();jButton4=newjavax.swing.JButton();jButton5=newjavax.swing.JButton();jButton6=newjavax.swing.JButton();jButton7=newjavax.swing.JButton();jButton8=newjavax.swing.JButton();jButton9=newjavax.swing.JButton();jButton10=newjavax.swing.JButton();jButton11=newjavax.swing.JButton();jButton12=newjavax.swing.JButton();jButton13=newjavax.swing.JButton();jButton14=newjavax.swing.JButton();jButton15=newjavax.swing.JButton();jTextField1=newjavax.swing.JTextField();
jButton1.setText("3");jButton1.addActionListener(newjava.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton2.setText("1");jButton2.addActionListener(new});}jButton1ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton3.setText("5");jButton3.addActionListener(new});}jButton2ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton4.setText("2");jButton4.addActionListener(new});}jButton3ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton5.setText("6");});}jButton4ActionPerformed(evt);
jButton5.addActionListener(newjava.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton6.setText("8");jButton6.addActionListener(new});}jButton5ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton7.setText("4");jButton7.addActionListener(new});}jButton6ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton8.setText("7");jButton8.addActionListener(new});}jButton7ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton9.setText("0");jButton9.addActionListener(new});}jButton8ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton10.setText("9");jButton10.addActionListener(new});}jButton9ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton11.setText("\u00f7");jButton11.addActionListener(new});}jButton10ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton12.setText("\u00d7");jButton12.addActionListener(new});}jButton11ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton13.setText("-");jButton13.addActionListener(new});}jButton12ActionPerformed(evt);java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
篇二:
JAVA实验报告
附件2:
实验报告封皮
20—学年第学期
课程
实验报告
学院:
运算机科学技术
专业:
软件工程
班级:
姓名:
学号:
任课教师:
王薇
实验日期:
XX年11月02日
-1-
-2-
实验日期:
XX年11月06日
-3-
-4-
篇三:
java图形用户界面实验报告
南京工程学院
实验报告
课程名称JAVA基础
实验项目名称图形用户界面设计
实验学生班级
实验学生姓名
学号
同组学生姓名无
实验时刻XX年11月
实验地址
实验成绩评定
指导教师签字年月日
一、实验目的和要求
1.目的:
把握javaAWT及Swing组件的利用方式,包括窗口、框架、对话框、布局方式、面板、文本编辑器、按钮、组合框等,合理利用委托事件处置模型,把握不同组件,不同事件的事件处置方式,设计出能够响应事件的java图形用户界面。
2.要求:
设计图形用户界面,事件处置,异样处置并弹出对话框,提示从头输入信息。
二、实验题目
验证哥德巴赫猜想,添加图形用户界面
三、实验方式与步骤(需求分析、算法设计思路、流程图等)
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassGDBHextendsFrameimplementsActionListener//窗口框架响应单击事件,利用awt组件设计框架
{
privateButtonbutton;//按钮
privateTextFieldtext1;//文本行
privateTextAreatext2;//文本区
publicGDBH()
{
super("验证哥德巴赫猜想");//设置框架窗口题目
this.setBounds(450,200,350,400);//设置框架的位置和尺寸this.setResizable(false);//设置框架大小不可改变this.setBackground(Color.lightGray);//设置窗口背景颜色
this.setLayout(newFlowLayout());//默许窗口流布局中对齐this.add(newLabel("输入一个整数"));
text1=newTextField("",20);
this.add(text1);
button=newButton("验证哥德巴赫猜想");
this.add(button);
button.addActionListener(this);//为按钮注册单击事件监听器,委托当前对象处置事件
this.setLayout(newFlowLayout());//默许中对齐
text2=newTextArea(20,25);
text2.setEditable(false);
this.add(text2);
this.setVisible(true);//设置组件可见
this.addWindowListener(newWinClose());//为框架注册窗口事件监听器,委托WinClose对象处置事件
}
publicvoidactionPerformed(ActionEvente)//单击事件处置方式,实现ActionListener接口
{
Stringstr=text1.getText();//取得文本行的字符串
try
{
longnum=Long.parseLong(str);//将字符串转化成长整形
text2.setText(yz(num));
}
catch(NumberFormatExceptionn)
{
JOptionPane.showMessageDialog(this,"\""+str+"\"字符串不能转换成整数,请从头输入!
!
");
return;
}
}
classWinCloseimplementsWindowListener//实现窗口事件监听器接口
{
publicvoidwindowClosing(WindowEvente)//窗口关闭事件处置方式
{
System.exit(0);//程序运行终止
}
publicvoidwindowOpened(WindowEvente){}
publicvoidwindowClosed(WindowEvente){}
publicvoidwindowIconified(WindowEvente){}
publicvoidwindowDeiconified(WindowEvente){}
publicvoidwindowActivated(WindowEvente){}
publicvoidwindowDeactivated(WindowEvente){}
}
假设文本行中的字符串不能转化成长整形,将弹出对话框“字符串不能转换成整数,请从头输入!
!
";另外若是所输入的整数为小于等于2或大于2小于等于5
也会弹出一个对话框“输入错误!
必需是大于2的偶数或大于5的奇数!
!
”(该窗口代码设置在yz()函数中)。
try
{
longnum=Long.parseLong(str);
text2.setText(yz(num));
}
catch(NumberFormatExceptionn)
{
JOptionPane.showMessageDialog(this,"\""+str+"\"字符串不能转换成整数,请从头输入!
!
");弹出对话框
return;
}
四、实验原始纪录(源程序、数据结构等)
源程序如下
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassGDBHextendsFrameimplementsActionListener
{
privateButtonbutton;
privateTextFieldtext1;
privateTextAreatext2;
publicGDBH()
{
super("验证哥德巴赫猜想");this.setBounds(450,200,350,400);this.setResizable(false);this.setBackground(Color.lightGray);this.setLayout(newFlowLayout());this.add(newLabel("输入一个整数"));text1=newTextField("",20);this.add(text1);button=newButton("验证哥德巴赫猜想");this.add(button);button.addActionListener(this);this.setLayout(newFlowLayout());text2=newTextArea(20,25);text2.setEditable(false);this.add(text2);this.setVisible(true);
this.addWindowListener(newWinClose());
}
publicstaticvoidmain(Stringargs[])
{
newGDBH();
}
publicvoidactionPerformed(ActionEvente)
{
Stringstr=text1.getText();
try{longnum=Long.parseLong(str);text2.setText(yz(num));}catch(NumberFormatExceptionn){
JOptionPane.showMessageDialog(this,"\""+str+"\"字符串不能转换成整数,请从头输入!
!
");
return;
}
}
publicStringyz(longnum)
{
Stringstr1="";if(num>2&&num%2==0){for(longi=2;i
j5&&num%2!
=0){for(longi=2;i