组件实验.docx

上传人:b****8 文档编号:9438228 上传时间:2023-02-04 格式:DOCX 页数:15 大小:119.86KB
下载 相关 举报
组件实验.docx_第1页
第1页 / 共15页
组件实验.docx_第2页
第2页 / 共15页
组件实验.docx_第3页
第3页 / 共15页
组件实验.docx_第4页
第4页 / 共15页
组件实验.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

组件实验.docx

《组件实验.docx》由会员分享,可在线阅读,更多相关《组件实验.docx(15页珍藏版)》请在冰豆网上搜索。

组件实验.docx

组件实验

姓名

学号

班级

年级

指导教师

西安财经学院信息学院

《Java程序设计》实验报告

实验名称组件的应用实验室522实验日期2014年月日

组件的应用

一、实验目的

1、熟悉和理解Java中AWT及Swing,能够设计简单美观的用户界面。

2、掌握使用Java中图形界面设计的组件及事件处理。

二、实验内容

1、编写程序,创建一个Frame,添加相关组件,实现简易计算器的基本功能。

三、实验环境

1.硬件:

一台微机

2.软件:

操作系统和java编译器

四、实验步骤

1、设置计算器的布局

按钮button→面板panel(文本框+按键区)→框架frame

JFrameframe=newJFrame("简易mini计算器");//创建一个JFrame的实例

JPanelp1=newJPanel();

JPanelp2=newJPanel();//创建两个JPanel

JTextFieldt1=newJTextField(25);//创建一个文本框

JButtonb0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;//设置计算器键盘按钮

 

//为框架设置BorderLayout布局管理器

frame.setLayout(newBorderLayout(4,4));

//将计算器键盘面板p1放入框架

frame.add(p1);

//将文本框p2放入框架的顶部

frame.add(p2,BorderLayout.NORTH);

//为p1设置4行4列的GridLayout布局管理器

p1.setLayout(newGridLayout(4,4,4,4));

//为p2设置BorderLayout布局管理器

p2.setLayout(newBorderLayout());

 

2、对程序设置监听程序

当按动按钮时,需要对事件作出响应,将内容显示到文本框中

frame.addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvente)

{

System.exit(0);

}

}

);

b0.addActionListener(newActionListener()

b1.addActionListener(newActionListener()

b2.addActionListener(newActionListener()

……

3、完善计算器内部算法

显示数字算法:

publicvoidactionPerformed(ActionEvente)

{

s+=1;

s2+=1;

t1.setText(s);

}

算数运算:

publicvoidactionPerformed(ActionEvente)

{

s1=s;

s+='+';

id=1;

s2="";

t1.setText(s);}

清除内容算法:

publicvoidactionPerformed(ActionEvente)

{

if(id<1);

else{

s+='=';

doublea=Double.parseDouble(s1);

doubleb=Double.parseDouble(s2);

doublec=0;

switch(id)

{

case1:

c=a+b;break;

case2:

c=a-b;break;

case3:

c=a*b;break;

case4:

c=a/b;break;

}

s+=c;

t1.setText(s);

}

4、调试程序,实现结果

五、实验结果

六、小结

通过这次实验,我收获了许多知识同时发现了不少的问题。

熟悉和理解了Java中AWT及Swing,能够设计简单美观的用户界面,还掌握了Java中图形界面设计的组件及事件处理的方法,达到了实验的目的。

虽然在实验的过程中遇到一些问题,但大部分通过查阅资料解决了。

在运行结果出现时,出现了如图所示的情况,结果正确,但原因有待探索

七、源程序清单

importjava.awt.*;

importjavax.swing.*;

importjava.awt.event.*;

importjavax.swing.text.*;

publicclasscounter{

intid=0;//设置标记

Strings="",s1=null,s2=null;

JFrameframe=newJFrame("简易mini计算器");//创建一个JFrame的实例

JPanelp1=newJPanel();

JPanelp2=newJPanel();//创建两个JPanel

JTextFieldt1=newJTextField(25);//创建一个文本框

JButtonb0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;

//设置计算器键盘按钮

publicstaticvoidmain(Stringargs[])

{

counterthat=newcounter();

that.go();

}

publicvoidgo(){

//为框架设置BorderLayout布局管理器

frame.setLayout(newBorderLayout(4,4));

//将计算器键盘面板p1放入框架

frame.add(p1);

//将文本框p2放入框架的顶部

frame.add(p2,BorderLayout.NORTH);

//为p1设置4行4列的GridLayout布局管理器

p1.setLayout(newGridLayout(4,4,4,4));

//为p2设置BorderLayout布局管理器

p2.setLayout(newBorderLayout());

//设置按钮名称及颜色

b0=newJButton("1");

b1=newJButton("2");

b2=newJButton("3");

b3=newJButton("+");b3.setForeground(Color.red);

b4=newJButton("4");

b5=newJButton("5");

b6=newJButton("6");

b7=newJButton("-");b7.setForeground(Color.red);

b8=newJButton("7");

b9=newJButton("8");

b10=newJButton("9");

b11=newJButton("*");b11.setForeground(Color.red);

b12=newJButton("0");

b13=newJButton(".");

b14=newJButton("=");b14.setForeground(Color.green);

b15=newJButton("/");b15.setForeground(Color.red);

b16=newJButton("clean");b16.setForeground(Color.blue);

p2.add(t1);//将文本框添加到p2面板中

//将b0-b15按钮添加到p1面板中

p1.add(b0);

p1.add(b1);

p1.add(b2);

p1.add(b3);

p1.add(b4);

p1.add(b5);

p1.add(b6);

p1.add(b7);

p1.add(b8);

p1.add(b9);

p1.add(b10);

p1.add(b11);

p1.add(b12);

p1.add(b13);

p1.add(b14);

p1.add(b15);

//将b16按钮添加到框架的下方

frame.getContentPane().add(b16,BorderLayout.SOUTH);

frame.setSize(300,300);//设置框架大小

frame.pack();

frame.setVisible(true);//显示计算器

//为按钮添加监听程序

frame.addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvente)

{

System.exit(0);

}

}

);

b0.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+=1;

s2+=1;

t1.setText(s);

}

}

);

b1.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+=2;

s2+=2;

t1.setText(s);

}

}

);

b2.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+=3;

s2+=3;

t1.setText(s);

}

}

);

b4.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+=4;

s2+=4;

t1.setText(s);

}

}

);

b5.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+=5;

s2+=5;

t1.setText(s);

}

}

);

b6.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+=6;

s2+=6;

t1.setText(s);

}

}

);

b8.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+=7;

s2+=7;

t1.setText(s);

}

}

);

b9.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+=8;

s2+=8;

t1.setText(s);

}

}

);

b10.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+=9;

s2+=9;

t1.setText(s);

}

}

);

b12.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+=0;

s2+=0;

t1.setText(s);

}

}

);

b13.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s+='.';

s2+='.';

t1.setText(s);

}

}

);

b3.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s1=s;

s+='+';

id=1;

s2="";

t1.setText(s);

}

}

);

b7.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s1=s;

s+='-';

id=2;

s2="";

t1.setText(s);

}

}

);

b11.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s1=s;

s+='*';

id=3;

s2="";

t1.setText(s);

}

}

);

b15.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s1=s;

s+='/';

id=4;

s2="";

t1.setText(s);

}

}

);

b16.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

s="";

s2="";

t1.setText(s);

}

}

);

b14.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

if(id<1);

else{

s+='=';

doublea=Double.parseDouble(s1);

doubleb=Double.parseDouble(s2);

doublec=0;

switch(id)

{

case1:

c=a+b;break;

case2:

c=a-b;break;

case3:

c=a*b;break;

case4:

c=a/b;break;

}

s+=c;

t1.setText(s);

}

}

}

);

}

}

窗体底端

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 水产渔业

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1