实验三常用类与图形界面程序设计.docx

上传人:b****1 文档编号:23074908 上传时间:2023-04-30 格式:DOCX 页数:64 大小:458.28KB
下载 相关 举报
实验三常用类与图形界面程序设计.docx_第1页
第1页 / 共64页
实验三常用类与图形界面程序设计.docx_第2页
第2页 / 共64页
实验三常用类与图形界面程序设计.docx_第3页
第3页 / 共64页
实验三常用类与图形界面程序设计.docx_第4页
第4页 / 共64页
实验三常用类与图形界面程序设计.docx_第5页
第5页 / 共64页
点击查看更多>>
下载资源
资源描述

实验三常用类与图形界面程序设计.docx

《实验三常用类与图形界面程序设计.docx》由会员分享,可在线阅读,更多相关《实验三常用类与图形界面程序设计.docx(64页珍藏版)》请在冰豆网上搜索。

实验三常用类与图形界面程序设计.docx

实验三常用类与图形界面程序设计

湖北工业大学工程技术学院实验报告

课程名称:

JAVA实验内容:

实验三常用类与图形界面设计

学院:

工程技术学院专业班级:

11gb软件2班

教师:

程学先成绩:

一、实验目的:

1、掌握创建链表与使用链表的方法

2、掌握求排列的程序设计方法

3、了解回溯法程序设计方法

4、学习文本框、文本域、按钮、列表框、组合框、单选框、复选框、菜单、消息框的意义与程序设计方法,什么是事件,怎样读取有关控件的内容

5、了解数据库驱动、连接与读取数据程序设计方法

6、学习数据库程序设计方法

二、执行下述程序,说明各程序功能。

如果题后有问题,按问题进行设计并总结。

将每题运行结果复制粘贴到一个Word文件中。

每题注意写明题号(从1到3)。

1、创建链表再添加节点,之后测试其节点数,读出每个节点数据。

importjava.util.*;

publicclassExample13_1

{publicstaticvoidmain(Stringargs[])

{Listlist=newLinkedList();//创建链表

list.add("is");//添加节点

list.add("a");

intnumber=list.size();

System.out.println("现在链表中有"+number+"个节点:

");

for(inti=0;i

{Stringtemp=(String)list.get(i);

System.out.println("第"+i+"节点中的数据:

"+temp);

}

list.add(0,"It");

number=list.size();

list.add(number-1,"door");

number=list.size();

System.out.println("现在链表中有"+number+"个节点:

");

for(inti=0;i

{Stringtemp=(String)list.get(i);

System.out.println("第"+i+"节点中的数据:

"+temp);

}

list.remove(0);

list.remove

(1);

list.set(0,"open");

number=list.size();

System.out.println("现在链表中有"+number+"个节点:

");

for(inti=0;i

{Stringtemp=(String)list.get(i);

System.out.println("第"+i+"节点中的数据:

"+temp);

}

}

}

复习链表的结构,结合本题说明如何创建链表、怎样添加数据、怎样查询与显示链表中的数据。

2、给定正整数k,以及1--k共k个正整数的一个排列,假如是1,2,3,...,k,求所有和此排列“不相交”的排列。

(所谓不相交,是指新的排列和已有排列在同一位置上的数都不相同。

如假设k=3,则其排列:

123和321是相交的,因为第二个位置上都是2;而排列123和312则不相交。

importjava.util.ArrayList;

importjava.util.List;

publicclassNoIntersect{

publicstaticListlist=newArrayList();

publicstaticListperc(int[]buf,intstart,intend){

if(start==end){

Stringstr="";

for(inti=0;i

str+=buf[i];//1、

}

list.add(str);//2、

}else{

for(inti=start;i<=end;i++){//3、

inttemp=buf[start];

buf[start]=buf[i];

buf[i]=temp;

perc(buf,start+1,end);//4、

temp=buf[start];

buf[start]=buf[i];

buf[i]=temp;

}

}

returnlist;

}

publicstaticbooleanisEqual(Stringstr,int[]buf){

booleanflag=true;

int[]str1=newint[str.length()];//5、

for(inti=0;i

str1[i]=Integer.parseInt(str.substring(i,i+1));//6、

}

for(inti=0;i

if(str1[i]==buf[i]){//7、

flag=false;

break;

}}

returnflag;//8、

}

publicstaticvoidperc1(int[]buf){

Listlist=perc(buf,0,buf.length-1);//9、

for(inti=0;i

if(isEqual(list.get(i).toString(),buf)){//10、

System.out.println(list.get(i));

}}}

publicstaticvoidmain(String[]args){

int[]arr={1,2,3,4,5};

perc1(arr);

}}

本题为什么采用List?

有何优点?

对程序中10个注释点进行注释。

总结本程序回溯的程序设计方法。

以下操作结合数据库进行,首先请将“student”数据库恢复到你的机中

恢复数据库操作步骤:

1、进入SQLServer企业管理器

选MicroseftSQLServer2005→SQLServerManagementStudioExpress,选数据库,例如B200\SQLEXPRESS,连接。

2、将数据文件“附加”恢复数据库。

右键点击“数据库”(database);选恢复(attach);点击按钮“添加”(add);找到数据文件:

student.mdf;

点击“确定”(ok)。

应当发现在数据库中新加了数据库:

student。

2、建立数据源(这次实验预定数据源名为sql1)

操作:

开始、设置、进入控制面板、管理工具、数据源(ODBC)、按添加、选驱动程序:

SQLServer、按“完成”、输入数据源名称:

(本例中用sql1)、“描述”中也填sql1、选当前服务器(一般默认的服务器名为【计算机名】\SQLEXPRESS。

例如,在控制面板的“系统”中查到完整计算机名为“B200”,则服务器名常为:

B200\SQLEXPRESS。

如果这个不正确而下拉框中没有选项,有时输入“local(本地)”即可。

要是还不行,可以进入SQLServer,在连接时观察其数据库名。

如果还不行,就必须找安装数据库的程序员询问了)、下一步、选WindowsNT验证,下一步选“更改默认的数据库为(D)、选“student”数据库、选下一步、完成、测试数据源、确定。

于是建立了数据源sql1,

之后再练习以下各题。

3、设有“学生”数据表,包括学号、姓名、性别、年龄等字段,需要设计一个数据输入程序,通过键盘输入学生数据,存入到计算机中。

importjava.awt.*;

importjava.awt.event.*;

importjava.applet.*;

importjavax.swing.*;

importjava.sql.*;

publicclassa082extendsJApplet{

privateJFrameframe;//1、

privateJButtonb1,b2,b3;//2、

staticJTextFieldf1,f2,f3,f4;//3、

privateJLabell1,l2,l3,l4;//4、

static{

try{//5、

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//6、

}

catch(java.lang.ClassNotFoundExceptione){//7、

System.out.println("ForName:

"+e.getMessage());

}}

publicstaticvoidmain(String[]args){

a082ca=newa082();

ca.go();}

voidgo(){//8、

frame=newJFrame("a082");//9、

frame.setSize(330,210);//10、

frame.setVisible(true);//11、

ContainercontentPane=frame.getContentPane();//12、

contentPane.setLayout(null);//13、

b1=newJButton("录入");//14、

contentPane.add(b1);//15、

b3=newJButton("退出");contentPane.add(b3);

b1.setBounds(60,110,80,20);//16、

b3.setBounds(170,110,80,20);

b1.addActionListener(newButtonHandler1());//17、

frame.getContentPane().add(b1,BorderLayout.CENTER);

b3.addActionListener(newButtonHandler3());

frame.getContentPane().add(b3,BorderLayout.CENTER);

l1=newJLabel("学号");//18、

contentPane.add(l1);//19、

l2=newJLabel("姓名");contentPane.add(l2);

l3=newJLabel("性别");contentPane.add(l3);

l4=newJLabel("年龄");contentPane.add(l4);

f1=newJTextField(0);//20、

contentPane.add(f1);//21、

f2=newJTextField(0);contentPane.add(f2);

f3=newJTextField(0);contentPane.add(f3);

f4=newJTextField(0);contentPane.add(f4);

l1.setBounds(30,30,40,20);//22、

f1.setBounds(70,30,80,20);//

l2.setBounds(160,30,40,20);f2.setBounds(190,30,80,20);

l3.setBounds(30,60,40,20);f3.setBounds(70,60,80,20);

l4.setBounds(160,60,40,20);f4.setBounds(190,60,80,20);

}

publicclassButtonHandler1implementsActionListener{//23、

publicvoidactionPerformed(ActionEvente){

Connectioncon=null;

Stringurl="jdbc:

odbc:

sql1";//24、

try{

con=DriverManager.getConnection(url);

Statementstmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

Strings1=f1.getText();

Strings2=f2.getText();Strings3=f3.getText();

Strings4=f4.getText();ints4_i=Integer.parseInt(s4);

Stringsss="insertinto学生values('"+s1+"','"+s2+"','"+s3+"',"+s4_i+")";

PreparedStatementps=con.prepareStatement(sss);

intc=ps.executeUpdate();//25、

stmt.close();//26、

}

catch(Exceptione1){

e1.printStackTrace();}

}}

publicclassButtonHandler3implementsActionListener{//23、

publicvoidactionPerformed(ActionEvente){

System.exit(0);

}}}

对以上26个注释点进行注释。

二、执行以下程序,说明程序功能,总结其算法与程序设计方法。

将每题运行结果复制粘贴到同一个Word文件中(界面设计要求将结果截图粘到Word文件中)。

每题注意写明题号(从4到12)。

4、利用Map进行单词词频的统计

importjava.util.*;

publicclassFreq{

publicstaticvoidmain(Stringargs[]){

String[]words={"if","it","is","to","be","it","is","up",

"to","me","to","delegate"};

Integerfreq;

Mapm=newTreeMap();

//构造字符串数组words的单词频率表.以单词为key,以词频为value。

for(inti=0;i

freq=(Integer)m.get(words[i]);//获取指定单词的词频。

//词频递增

if(freq==null){

freq=newInteger

(1);

}else{

freq=newInteger(freq.intValue()+1);

}

m.put(words[i],freq);//在Map中更改词频。

}

System.out.println(m.size()+"distinctwordsdetected:

");

System.out.println(m);

}

}

5、创建学生数组列表集合,用迭代器方法,删除一个元素、添加一个元素。

importjava.util.*;

classStudent

{

Stringname;

charsex;

intscore;

publicStudent(Stringname,charsex,intscore)

{

this.name=name;

this.sex=sex;

this.score=score;

}

publicvoidprintStudent()

{

System.out.println("姓名"+name+"性别"+sex+"分数"+score);

}

}

classArrayStudentList{

publicstaticvoidmain(String[]args){

ArrayListal=newArrayList();

al.add(newStudent("a",'f',90));

al.add(newStudent("b",'m',80));

al.add(newStudent("c",'f',80));

ListIteratorlt=al.listIterator();

System.out.println("增加一个学生");

lt.add(newStudent("q",'m',90));

System.out.println("删掉第二学生");

lt.next();lt.next();lt.remove();

for(ListIteratori=al.listIterator();i.hasNext();){

(i.next()).printStudent();

}}}

6、设计红色、绿色、蓝色三个按钮,使用最基本布局,当按下某按钮时窗口变成相应颜色。

6_1程序1

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

classButtonPanelextendsJPanelimplementsActionListener

//JPanel:

具有布局管理器的面板,ActionListener:

侦听器接口

{publicButtonPanel()

{yellowButton=newJButton("红色");//定义三个按钮对象

blueButton=newJButton("绿色");

redButton=newJButton("蓝色");

add(yellowButton);//加到窗口中

add(blueButton);

add(redButton);

yellowButton.addActionListener(this);//定义点击按钮的事件方法

blueButton.addActionListener(this);

redButton.addActionListener(this);

}

publicvoidactionPerformed(ActionEventevt)

//ActionEvent:

组件定义的动作的高一级事件

{Objectsource=evt.getSource();//返回源事件名称

Colorcolor=getBackground();

if(source==yellowButton)color=Color.red;

elseif(source==blueButton)color=Color.green;

elseif(source==redButton)color=Color.blue;

setBackground(color);//刷新面板颜色

//repaint();

}

privateJButtonyellowButton;//定义三个按钮对象名

privateJButtonblueButton;

privateJButtonredButton;

}

classButtonFrameextendsJFrame

{publicButtonFrame()

{setTitle("ButtonTest");

setSize(300,200);

ContainercontentPane=getContentPane();

contentPane.add(newButtonPanel());//建立面板

}

}

publicclassmyButtons

{publicstaticvoidmain(String[]args)

{JFrameframe=newButtonFrame();

frame.setVisible(true);//窗口显示

}

}

试验一下:

将JButton("红色"中的红色二字增加到6个汉字或更多,执行一下;再试验在界面中增加B1、B2、B3三个按钮,(每增加一个按钮,至少需要privateJButtonb1、b1=newJButton("红色");add(b1);等三条语句,另外再考虑事件定义语句。

6_2、设计红色、绿色、蓝色三个按钮,不使用布局管理器,当按下某按钮时窗口变成相应颜色。

importjava.awt.*;

importjava.awt.event.*;

importjava.applet.*;

importjavax.swing.*;

publicclassmyButtonsextendsJApplet{

privateJFrameframe;//定义窗口对象

privateJButtonyellowButton,blueButton,redButton;//按钮对象

privateColorcolor=getBackground();//颜色对象

publicstaticvoidmain(String[]args){

myButtonsca=newmyButtons();

ca.go();

}

voidgo(){

frame=newJFrame("ButtonTest");

ContainercontentPane=frame.getContentPane();

contentPane.setLayout(null);

frame.setSize(300,200);

frame.setVisible(true);

yellowButton=newJButton("红色");//定义三个按钮对象

blueButton=newJButton("绿色");

redButton=newJButton("蓝色");

contentPane.add(yellowButton);//安装到容器中

contentPane.add(blueButton);

contentPane.a

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

当前位置:首页 > 工作范文 > 其它

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

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