java实验报告4Word格式.docx

上传人:b****6 文档编号:18757295 上传时间:2023-01-01 格式:DOCX 页数:30 大小:839.92KB
下载 相关 举报
java实验报告4Word格式.docx_第1页
第1页 / 共30页
java实验报告4Word格式.docx_第2页
第2页 / 共30页
java实验报告4Word格式.docx_第3页
第3页 / 共30页
java实验报告4Word格式.docx_第4页
第4页 / 共30页
java实验报告4Word格式.docx_第5页
第5页 / 共30页
点击查看更多>>
下载资源
资源描述

java实验报告4Word格式.docx

《java实验报告4Word格式.docx》由会员分享,可在线阅读,更多相关《java实验报告4Word格式.docx(30页珍藏版)》请在冰豆网上搜索。

java实验报告4Word格式.docx

);

//TODOAuto-generatedEventstubwidgetSelected()

Dicedice=newDice();

int[]list=newint[6];

String[]urlimage=newString[6];

for(inti=0;

i<

6;

i++)

{

list[i]=dice.roll();

urlimage[i]="

/dsy1010/"

+Integer.toString(list[i])+"

.png"

;

}

label01.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimage[0])));

label02.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimage[1])));

label03.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimage[2])));

label04.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimage[3])));

label05.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimage[4])));

label06.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimage[5])));

Resultrst=newResult();

result_label.setText(rst.GetResult(list));

(6).在骰子归位下添加事件代码如下:

initDice();

});

initDice();

}

publicvoidinitDice()

{

Stringurlimg="

/dsy1010/4.png"

label01.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimg)));

label02.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimg)));

label03.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimg)));

label04.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimg)));

label05.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimg)));

label06.setImage(newImage(Display.getCurrent(),getClass().getResourceAsStream(urlimg)));

(7).将图片导入包里

(8).新建普通类Dice,设计方法,代码如下:

packagedsy1020;

 

publicclassDice{

privatefinalintMAX=6;

privateintfaceValue;

publicDice()

faceValue=1;

publicintroll()

faceValue=(int)(Math.random()*MAX)+1;

returnfaceValue;

publicvoidsetFaceValue(intvalue)

faceValue=value;

publicintgetFaceValue()

publicStringtoString()

Stringresult=Integer.toString(faceValue);

returnresult;

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

}

(9)新建普通类Result设计在获奖区显示输出结果的方法,代码如下:

publicclassResult{

publicStringGetResult(intlist[])

{

inta=0,b=0,c=0,d=0,e=0,f=0;

inti;

//Number[i]存储各个骰子的数值

for(i=0;

if(list[i]==1)//判断有几个1

{

a++;

}

if(list[i]==2)//判断有几个2

b++;

if(list[i]==3)//判断有几个3

c++;

if(list[i]==4)//判断有几个4

d++;

if(list[i]==5)//判断有几个5

e++;

if(list[i]==6)//判断有几个6

f++;

if(d==0&

&

a<

4&

b<

c<

e<

f<

4)//没有

return"

没有"

if(d==1&

4)//一秀

一秀"

if(d==2&

4)//二举

二举"

(a==4||b==4||c==4||e==4||f==4))//四进

四进"

(a==4||b==4||c==4||e==4||f==4))//四进带一秀

四进带一秀"

(a==4||b==4||c==4||e==4||f==4))//四进带二举

四进带二举"

if(d==3)//三红

三红"

if(a==1&

b==1&

c==1&

d==1&

e==1&

f==1)//对堂

对堂"

if(d==4&

2)//状元

状元"

if(a==5||b==5||c==5||e==5||f==5)//五子

五子"

if(d==5)//五四

五四"

if(a==6||b==6||c==6||e==6||f==6)//六同

六同"

a==2)//状元插金花

状元插金花"

if(d==6)

returnnull;

}

2.student登陆程序

(3).新建shell可视化类Zhujie(前三步如下图)

(4).设计登陆界面

(5).在第一个text控件下添加事件

代码如下:

publicvoidmodifyText(org.eclipse.swt.events.ModifyEvente){

modifyText()"

//TODOAuto-generatedEventstubmodifyText()

Stringtxt=text1.getText()==null?

"

:

text1.getText().trim();

if(txt.matches("

a-zA-Z]*+[\\d]*+[a-zA-Z]*+[\\d]*+"

)){

MessageBoxmsgBox=newMessageBox(sShell);

msgBox.setMessage("

用户名只能由字母和数字组成!

msgBox.open();

text1.selectAll();

}}

在登陆button控件下添加事件

Useruser=newUser(text1.getText().trim(),text12.getText().trim(),

combojiaose.getSelectionIndex());

ShelloldShell=null;

if(newUserset().isVaild(user)){

oldShell=sShell;

sShell=newMainWindow(user).getsShell();

oldShell.dispose();

sShell.open();

}else{

MessageBoxmsgBox=newMessageBox(sShell);

msgBox.setMessage("

用户名或密码错误!

msgBox.open();

text1.setText("

text12.setText("

(5).在combo控件下添加代码

privatevoidcreateCombojiaose(){

combojiaose=newCombo(group1,SWT.NONE);

combojiaose.setBounds(newRectangle(91,113,87,20));

combojiaose.setVisibleItemCount

(2);

combojiaose.add("

管理员"

学生"

教师"

(5)建user普通类,设计函数,生成get,set,hash,string方法,代码如下:

packageDenglu;

publicclassUser{

@Override

publicStringtoString(){

return"

User[name="

+name+"

password="

+password+"

job="

+job

+"

]"

publicinthashCode(){

finalintprime=31;

intresult=1;

result=prime*result+ADMIN;

result=prime*result+STUDENT;

result=prime*result+TEACHER;

publicbooleanequals(Objectobj){

if(this==obj)

returntrue;

if(obj==null)

returnfalse;

if(getClass()!

=obj.getClass())

Userother=(User)obj;

if(ADMIN!

=other.ADMIN)

if(STUDENT!

=other.STUDENT)

if(TEACHER!

=other.TEACHER)

returntrue;

privateStringname;

privateStringpassword;

privateintjob;

finalintADMIN=0;

finalintSTUDENT=2;

finalintTEACHER=1;

publicStringgetName(){

returnname;

publicvoidsetName(Stringname){

this.name=name;

publicStringgetPassword(){

returnpassword;

publicvoidsetPassword(Stringpassword){

this.password=password;

publicintgetJob(){

returnjob;

publicvoidsetJob(intjob){

this.job=job;

publicUser(Stringname,Stringpassword,intjob){

super();

(6)建userset普通类,键入代码,代码如下:

importjava.io.BufferedReader;

importjava.io.FileNotFoundException;

importjava.io.FileReader;

importjava.io.IOException;

importjava.util.HashSet;

publicclassUserset{

HashSet<

User>

userset;

publicUserset(){

super();

userset=newHashSet<

();

Stringstr=null;

String[]userStr=null;

try{

FileReaderfir=newFileReader("

E:

\\java\\Student\\src\\yonghuming.txt"

BufferedReaderbir=newBufferedReader(fir);

while((str=bir.readLine())!

=null){

userStr=str.split("

userset.add(newUser(userStr[0].trim(),userStr[1].trim(),Integer.parseInt(userStr[2])));

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

}}

publicbooleanisVaild(Useruser){

booleanuserValid=false;

if(userset.contains(user)){

userValid=true;

returnuserValid;

}}

(6).新建文件yonghuming,存放捆绑的用户名和密码。

用户名密码如下:

admin:

123:

wnb:

000:

1

student:

456:

2

(7)新建shell类,为程序登录后显示的界面

3.监听器实践

(3).新建普通类MuJFrame(前三步如下图)

(4)此为主窗口,调用监听器,设计主窗口,代码如下:

packagedsy;

importjava.awt.*;

importjavax.swing.*;

importjava.awt.event.*;

publicclassMuJFrameimplementsActionListener{//因为要执行button的单击事件,需要用到监听器

/**

*@paramargs

*/

//构造函数

publicMuJFrame(){

JFramef=newJFrame("

这是第一个窗口"

Containercp=f.getContentPane();

JButtonbt1=newJButton("

请点击"

bt1.setSize(100,50);

bt1.addActionListener(this);

cp.add(bt1);

f.setSize(200,150);

//f.pack();

f.show();

f.addWindowListener(newWinLis());

publicvoidactionPerformed(ActionEvente)//监听器的actionPerformed方法

{

JLabellb=newJLabel("

这是第二个窗口"

JFramenf=newJFrame();

Containercp1=nf.getContentPane();

nf.setTitle("

这是一个新JFrame"

nf.setSize(180,100);

//设置窗口大小

nf.show();

cp1.add(lb);

nf.addWindowListener(newWinLis());

}

//程序入口

newMuJFrame();

(5).建普通类,设计第二个窗口,代码如下:

publicclasschuangkou2{

JLabellb1=newJLabel("

JLabellb2=newJLabel("

publicchuangkou2(){

JFramef1=newJFrame();

//创建JFrame对象

JFramef2=newJFrame();

Containercp=f1.getContentPane();

//创建JFrame的容器对象,获得ContentPane

Containercp1=f2.

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

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

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

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