Java多线程实验报告详解Word文件下载.docx

上传人:b****2 文档编号:14721689 上传时间:2022-10-24 格式:DOCX 页数:14 大小:92.64KB
下载 相关 举报
Java多线程实验报告详解Word文件下载.docx_第1页
第1页 / 共14页
Java多线程实验报告详解Word文件下载.docx_第2页
第2页 / 共14页
Java多线程实验报告详解Word文件下载.docx_第3页
第3页 / 共14页
Java多线程实验报告详解Word文件下载.docx_第4页
第4页 / 共14页
Java多线程实验报告详解Word文件下载.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

Java多线程实验报告详解Word文件下载.docx

《Java多线程实验报告详解Word文件下载.docx》由会员分享,可在线阅读,更多相关《Java多线程实验报告详解Word文件下载.docx(14页珍藏版)》请在冰豆网上搜索。

Java多线程实验报告详解Word文件下载.docx

2.多线程的编程:

继承Thread类与使用Runnable接口。

3.使用多线程机制实现动画。

二、实验要求

1.掌握利用JAVA语言编写多线程程序的方法。

2.掌握线程的调度方法。

3.掌握多线程环境中GUI程序的编写方法。

三、实验内容

(一)汉字打字练习。

✧实验要求:

编写一个Java应用程序,在主线程中再创建一个Frame类型的窗口,在该窗口中再创建1个线程giveWord。

线程giveWord每隔2秒钟给出一个汉字,用户使用一种汉字输入法将该汉字输入到文本框中。

✧程序模板:

WordThread.java

importjava.awt.*;

publicclassWordThreadextendsThread

{charword;

intk=19968;

Labelcom;

WordThread(Labelcom)

{=com;

}

publicvoidrun()

{k=19968;

while(true)

{

word=(char)k;

com.setText("

"

+word);

try{【代码1】//调用sleep方法使得线程中断6000豪秒

}

catch(InterruptedExceptione){}

k++;

if(k>

=29968)k=19968;

}

ThreadFrame.java

importjava.awt.event.*;

publicclassThreadFrameextendsFrameimplementsActionListener

{

LabelwordLabel;

Buttonbutton;

TextFieldinputText,scoreText;

【代码2】//用WordThread声明一个giveWord对象

intscore=0;

ThreadFrame()

{wordLabel=newLabel("

"

Label.CENTER);

wordLabel.setFont(newFont("

Font.BOLD,72));

button=newButton("

开始"

);

inputText=newTextField(3);

scoreText=newTextField(5);

scoreText.setEditable(false);

【代码3】//创建giveWord,将wordLabel传递给WordThread构造方法的参数

button.addActionListener(this);

inputText.addActionListener(this);

add(button,BorderLayout.NORTH);

add(wordLabel,BorderLayout.CENTER);

PanelsouthP=newPanel();

southP.add(newLabel("

输入标签所显示的汉字后回车:

));

southP.add(inputText);

southP.add(scoreText);

add(southP,BorderLayout.SOUTH);

setBounds(100,100,350,180);

setVisible(true);

validate();

addWindowListener(newWindowAdapter()

{publicvoidwindowClosing(WindowEvente)

{System.exit(0);

);

publicvoidactionPerformed(ActionEvente)

if(e.getSource()==button)

{if(!

(【代码4】))//giveWord调用方法isAlive()

{giveWord=newWordThread(wordLabel);

try

{【代码5】//giveWord调用方法start()

catch(Exceptionexe){}

elseif(e.getSource()==inputText)

{if(inputText.getText().equals(wordLabel.getText()))

{score++;

scoreText.setText("

得分:

+score);

inputText.setText(null);

publicclassThreadWordMainClass

{publicstaticvoidmain(Stringargs[])

{newThreadFrame();

✧实验后的练习:

1.在WordThread类中增加int型的成员time,其初值为6000,将其中的【代码1】更改为线程中断time毫秒。

在WordThread类增加publicvoidsetTime(intn)方法,使得WordThread线程对象可以调用该方法修改time的值。

(二)旋转的行星。

编写一个应用程序,模拟月亮围绕地球旋转、地球围绕太阳旋转。

Mycanvas.java

publicclassMycanvasextendsCanvas

{intr;

Colorc;

publicvoidsetColor(Colorc)

{this.c=c;

publicvoidsetR(intr)

{this.r=r;

publicvoidpaint(Graphicsg)

{g.setColor(c);

g.fillOval(0,0,2*r,2*r);

publicintgetR()

{returnr;

Planet.java

publicclassPlanetextendsPanelimplementsRunnable

{【代码1】//用Thread类声明一个moon对象

MycanvasyellowBall;

doublepointX[]=newdouble[360],

pointY[]=newdouble[360];

//用来表示画布左上角端点坐标的数组

intw=100,h=100;

intradius=30;

Planet()

{setSize(w,h);

setLayout(null);

yellowBall=newMycanvas();

yellowBall.setColor(Color.yellow);

add(yellowBall);

yellowBall.setSize(12,12);

yellowBall.setR(12/2);

pointX[0]=0;

pointY[0]=-radius;

doubleangle=1*Math.PI/180;

//刻度为1度

for(inti=0;

i<

359;

i++)//计算出数组中各个元素的值

{pointX[i+1]=pointX[i]*Math.cos(angle)-Math.sin(angle)*pointY[i];

pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle);

}

360;

i++)

{pointX[i]=pointX[i]+w/2;

//坐标平移

pointY[i]=pointY[i]+h/2;

yellowBall.setLocation((int)pointX[0]-yellowBall.getR(),

(int)pointY[0]-yellowBall.getR());

【代码2】//创建moon线程,当前面板做为该线程的目标对象

publicvoidstart()

{try{moon.start();

catch(Exceptionexe){}

{g.setColor(Color.blue);

g.fillOval(w/2-9,h/2-9,18,18);

{inti=0;

{i=(i+1)%360;

yellowBall.setLocation((int)pointX[i]-yellowBall.getR(),

(int)pointY[i]-yellowBall.getR());

try{【代码3】//Thread类调用类方法sleep使得线程中断10豪秒

HaveThreadFrame.java

publicclassHaveThreadFrameextendsFrameimplementsRunnable

{【代码4】//用Thread类声明一个rotate对象

Planetearth;

intwidth,height;

intradius=120;

HaveThreadFrame()

{rotate=newThread(this);

earth=newPlanet();

setBounds(0,0,360,400);

width=getBounds().width;

height=getBounds().height;

doubleangle=1*Math.PI/180;

{pointX[i]=pointX[i]+width/2;

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

当前位置:首页 > 法律文书 > 调解书

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

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