java图形界面实验报告材料.docx

上传人:b****7 文档编号:10373436 上传时间:2023-02-10 格式:DOCX 页数:18 大小:157.47KB
下载 相关 举报
java图形界面实验报告材料.docx_第1页
第1页 / 共18页
java图形界面实验报告材料.docx_第2页
第2页 / 共18页
java图形界面实验报告材料.docx_第3页
第3页 / 共18页
java图形界面实验报告材料.docx_第4页
第4页 / 共18页
java图形界面实验报告材料.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

java图形界面实验报告材料.docx

《java图形界面实验报告材料.docx》由会员分享,可在线阅读,更多相关《java图形界面实验报告材料.docx(18页珍藏版)》请在冰豆网上搜索。

java图形界面实验报告材料.docx

java图形界面实验报告材料

河南工业大学实验报告

专业班级:

计科F1401学号:

姓名:

实验单元八

【实验目的】

1、掌握?

?

?

?

?

?

?

程序设计方法

2、掌握?

?

?

?

?

?

?

程序设计方法

3、掌握?

?

?

?

?

?

?

程序设计方法

4、掌握?

?

?

?

?

?

?

程序设计方法

5、掌握使用?

程序设计方法。

【实验环境】

安装了jdk软件的PC机。

【实验内容】

第18章、图形界面。

【程序功能内容说明】

设置标签的显示字体、大小背景及颜色。

【实验程序原码】

importjava.awt.Dimension;

importjava.awt.Color;

importjava.awt.Font;

importjava.awt.Point;

importjavax.swing.JLabel;

importjavax.swing.JFrame;

publicclassJLabelDemo02{

publicstaticvoidmain(Stringargs[]){

JFrameframe=newJFrame("WelcomeToMLDN");

JLabellab=newJLabel("MLDN",JLabel.CENTER);//实例化标签对象

Fontfnt=newFont("Serief",Font.ITALIC+Font.BOLD,28);

lab.setFont(fnt);

frame.add(lab);//将组件件入到面板之中

Dimensiondim=newDimension();

frame.setBackground(Color.WHITE);//设置窗体的背景颜色

dim.setSize(200,70);

frame.setSize(dim);

Pointpoint=newPoint(300,200);//设置坐标

frame.setLocation(point);

frame.setVisible(true);

}

};

【实验结果】

【该程序关键技术说明】

JFrame作为基本容器用于创建窗口。

JLabel作为标签组件用于在窗口上的显示。

【程序功能内容说明】

设置GridLayout用于加入按钮。

【实验程序原码】

importjava.awt.GridLayout;

importjavax.swing.JFrame;

importjavax.swing.JButton;

publicclassGridLayoutDemo01{

publicstaticvoidmain(Stringargs[]){

JFrameframe=newJFrame("WelcomeToMLDN");

frame.setLayout(newGridLayout(3,5,3,3));

JButtonbut=null;

for(inti=0;i<13;i++){

but=newJButton("按钮-"+i);

frame.add(but);

}

frame.pack();

frame.setVisible(true);

}

};

【实验结果】

【该程序关键技术说明】

按钮组件JButton用于定义按钮。

GridLayout布局管理器用于摆放多个按钮。

【程序功能内容说明】

用户登录系统。

【实验程序原码】

importjava.awt.event.WindowAdapter;

importjava.awt.event.ActionListener;

importjava.awt.event.WindowEvent;

importjava.awt.event.ActionEvent;

importjava.awt.Color;

importjava.awt.GridLayout;

importjava.awt.Font;

importjavax.swing.JFrame;

importjavax.swing.JButton;

importjavax.swing.JLabel;

importjavax.swing.JTextField;

importjavax.swing.JPasswordField;

importjavax.swing.JPanel;

classLoginCheck{

privateStringname;

privateStringpassword;

publicLoginCheck(Stringname,Stringpassword){

this.name=name;

this.password=password;

}

publicbooleanvalidate(){

if("lixinghua".equals(name)&&"mldn".equals(password)){

returntrue;

}else{

returnfalse;

}

}

};

classActionHandle{

privateJFrameframe=newJFrame("WelcomeToMLDN");

privateJButtonsubmit=newJButton("登陆");

privateJButtonreset=newJButton("重置");

privateJLabelnameLab=newJLabel("用户名:

");

privateJLabelpassLab=newJLabel("密码:

");

privateJLabelinfoLab=newJLabel("用户登陆系统");

privateJTextFieldnameText=newJTextField(10);

privateJPasswordFieldpassText=newJPasswordField();

privateJPanelpan=newJPanel();

publicActionHandle(){

Fontfnt=newFont("Serief",Font.ITALIC+Font.BOLD,12);

infoLab.setFont(fnt);//设置标签的显示文字

submit.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==submit){

Stringtname=nameText.getText();

Stringtpass=newString(passText.getPassword());

LoginChecklog=newLoginCheck(tname,tpass);

if(log.validate()){

infoLab.setText("登陆成功,欢迎光临!

");

}else{

infoLab.setText("登陆失败,错误的用户名或密码!

");

}

}

}

});

reset.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==reset){

nameText.setText("");

passText.setText("");

infoLab.setText("用户登陆系统");

}

}

});

frame.addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

System.exit

(1);

}

});//加入事件

frame.setLayout(null);

nameLab.setBounds(5,5,60,20);

passLab.setBounds(5,30,60,20);

infoLab.setBounds(5,65,220,30);

nameText.setBounds(65,5,100,20);

passText.setBounds(65,30,100,20);

submit.setBounds(165,5,60,20);

reset.setBounds(165,30,60,20);

frame.add(nameLab);

frame.add(passLab);

frame.add(infoLab);

frame.add(nameText);

frame.add(passText);

frame.add(submit);

frame.add(reset);

frame.setSize(280,130);

frame.setBackground(Color.WHITE);

frame.setLocation(300,200);

frame.setVisible(true);

}

};

publicclassMyActionEventDemo03{

publicstaticvoidmain(Stringargs[]){

newActionHandle();

}

};

【实验结果】

【该程序关键技术说明】

JFame、JLabel、JButton等综合应用,实现窗口的显示、输入、按钮等。

【程序功能内容说明】

建立表格。

【实验程序原码】

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

importjavax.swing.JTable;

importjavax.swing.JScrollPane;

importjavax.swing.JFrame;

publicclassJTableDemo01{

publicstaticvoidmain(Stringargs[]){

JFrameframe=newJFrame("WelcomeToMLDN");

String[]titles={"姓名","年龄","性别","数学成绩","英语成绩","总分","是否及格"};

Object[][]userInfo={

{"李兴华",30,"男",89,97,186,true},

{"李康",23,"女",90,93,183,false}

};//定义数据

JTabletable=newJTable(userInfo,titles);//建立表格

JScrollPanescr=newJScrollPane(table);

frame.add(scr);

frame.setSize(370,90);

frame.setVisible(true);

frame.addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

System.exit

(1);

}

});

}

}

【实验结果】

【该程序关键技术说明】

JTable建立表格。

 

【程序功能内容说明】

编写程序,在文本框中输入一个浮点型华氏温度,根据下面的公式将其转化成摄氏温度。

摄氏温度=(华氏温度-32)*5/9

【实验程序原码】

importjava.awt.GridLayout;

importjava.awt.Container;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JButton;

importjavax.swing.JTextField;

importjavax.swing.JPanel;

classActionHandle{

privateJFrameframe=newJFrame("温度转换");

privateContainercont=frame.getContentPane();

privateJPanelpan=newJPanel(newGridLayout(2,2));

privateJPanelpan1=newJPanel(newGridLayout(2,1,3,10));

privateJTextFieldhuashi=newJTextField(30);

privateJTextFieldsheshi=newJTextField("=(华氏-32)*5/9",20);

privateJLabelhuashiLab=newJLabel("请输入华氏温度值:

");

privateJLabelsheshiLab=newJLabel("转换后的摄氏温度值:

");

privateJButtonbut=newJButton("开始转换");

publicActionHandle(){

frame.addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEventarg0){

System.exit

(1);

}

});

but.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventarg0){

if(arg0.getSource()==but){

Stringtemp=""+(Float.parseFloat(huashi.getText())-32)*5/9;

sheshi.setText(temp);

}

}

});

pan.add(huashiLab);

pan.add(huashi);

pan.add(sheshiLab);

pan.add(sheshi);

pan1.add(pan);

pan1.add(but);

cont.add(pan1);

frame.setSize(400,150);

frame.setLocation(300,200);

frame.pack();

frame.setVisible(true);

}

}

publicclassTemperation{

publicstaticvoidmain(Stringargs[]){

newActionHandle();

}

}

【实验结果】

【该程序关键技术说明】

向JFrame上加入组件。

JLabel显示信息。

JButton定义一个按钮。

GirdLayout布局管理器。

Action类的应用。

【程序功能内容说明】

编写程序,在文本框中输入英文字母,再根据大写字母按钮和小写字母按钮将输入的内容进行转换,并将转换的内容显示在标签上。

【实验程序原码】

importjava.awt.Container;

importjava.awt.GridLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JPanel;

importjavax.swing.JTextField;

classCharConvert{

privateJFrameframe=newJFrame("字母大小写转换");

privateContainercont=frame.getContentPane();

privateJPanelpan=newJPanel(newGridLayout(3,2));

privateJTextFieldsourceText=newJTextField(30);

privateJTextFieldtargetText=newJTextField();

privateJLabeltoUpperLabel=newJLabel("请输入一串英文字母:

");

privateJLabeltoLowerLabel=newJLabel("转换后的结果是:

");

privateJButtontoUpperBut=newJButton("大写字母按钮");

privateJButtontoLowerBut=newJButton("小写字母按钮");

publicCharConvert(){

toUpperBut.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventarg0){

if(arg0.getSource()==toUpperBut){

Stringtemp=sourceText.getText().toUpperCase();

targetText.setText(temp);

}

}

});

toLowerBut.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventarg0){

if(arg0.getSource()==toLowerBut){

Stringtemp=sourceText.getText().toLowerCase();

targetText.setText(temp);

}

}

});

pan.add(toUpperLabel);

pan.add(sourceText);

pan.add(toLowerLabel);

pan.add(targetText);

pan.add(toUpperBut);

pan.add(toLowerBut);

cont.add(pan);

frame.setSize(400,150);

frame.setLocation(300,200);

frame.pack();

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

publicclassLetter{

publicstaticvoidmain(Stringargs[]){

newCharConvert();

}

}

【实验结果】

【该程序关键技术说明】

向JFrame上加入组件。

JLabel显示信息。

JButton定义一个按钮。

GirdLayout布局管理器。

CharConvert字符串转换类的应用。

【实验体会】

这次实验报告的内容是第十八章,图形界面。

图形界面顾名思义就是可以将所写的代码通过窗口运行出来,比起以往的运行结果,更像我们平常用的电脑软件。

我们的计算机无论是打开我的电脑还是打开QQ打开游戏,其实打开的都是图形界面。

通过这章节的学习,我了解到了容器,组件,布局管理器的概念,这是学习图形界面必须要了解的。

容器为组件设置位置、大小、颜色等。

而图形界面中的按钮、标签、菜单都是组件。

使用布局管理器可以把组件按指定位置摆放。

这三者的概念是图形界面的基础,掌握了相关语法结合之前的内容就已经可以编写一些简单的程序了。

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

当前位置:首页 > 自然科学 > 数学

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

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