云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > Java龚永罡---答案

Java龚永罡---答案

  • 62 次阅读
  • 3 次下载
  • 2025/6/15 20:04:18

实验六 多线程

【目的】

①掌握使用Thread的子类创建线程; ②学习使用Thread类创建线程; ③学习处理线程同步问题。 【内容】

1.汉字打字练习。

? 实验要求:

编写一个Java应用程序,在主线程中再创建一个Frame类型的窗口,在该窗口中再创建1个线程giveWord。线程giveWord每隔2秒钟给出一个汉字,用户使用一种汉字输入法将该汉字输入到文本框中。

? 程序运行效果示例:

程序运行效果如下图所示:

? 程序模板:

WordThread.java

import java.awt.*;

public class WordThread extends Thread { char word; int k=19968; Label com;

WordThread(Label com) { this.com=com; }

public void run() { k=19968; while(true) {

word=(char)k; com.setText(\

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

catch(InterruptedException e){} k++;

if(k>=29968) k=19968; } } }

ThreadFrame.java

import java.awt.*; import java.awt.event.*;

public class ThreadFrame extends Frame implements ActionListener

33

{

Label wordLabel; Button button;

TextField inputText,scoreText;

【代码2】//用WordThread声明一个giveWord对象 int score=0; ThreadFrame()

{ wordLabel=new Label(\ wordLabel.setFont(new Font(\ button=new Button(\开始\ inputText=new TextField(3); scoreText=new TextField(5); scoreText.setEditable(false);

【代码3】//创建giveWord,将wordLabel传递给WordThread构造方法的参数 button.addActionListener(this); inputText.addActionListener(this); add(button,BorderLayout.NORTH); add(wordLabel,BorderLayout.CENTER); Panel southP=new Panel();

southP.add(new Label(\输入标签所显示的汉字后回车:\ southP.add(inputText); southP.add(scoreText);

add(southP,BorderLayout.SOUTH); setBounds(100,100,350,180); setVisible(true); validate();

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e) { System.exit(0); } } ); }

public void actionPerformed(ActionEvent e) {

if(e.getSource()==button)

{ if(!(【代码4】)) //giveWord调用方法isAlive() { giveWord=new WordThread(wordLabel); } try

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

catch(Exception exe){} }

34

else if(e.getSource()==inputText)

{ if(inputText.getText().equals(wordLabel.getText())) { score++; }

scoreText.setText(\得分:\ inputText.setText(null); } } }

WordThread.java

public class ThreadWordMainClass

{ public static void main(String args[]) { new ThreadFrame(); }

? 实验后的练习:

1. 在WordThread类中增加int型的成员time,其初值为6000,将其中的【代码1】更改为线程中断time毫秒。在WordThread类增加public void setTime(int n)方法,使得WordThread线程对象可以调用该方法修改time的值。

2.旋转的行星。

? 实验要求:

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

? 程序运行效果示例:

程序运行效果如下图所示:

? 程序模板:

Mycanvas.java

import java.awt.*;

public class Mycanvas extends Canvas { int r; Color c;

public void setColor(Color c) { this.c=c; }

public void setR(int r) { this.r=r; }

public void paint(Graphics g) { g.setColor(c);

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

public int getR() { return r;

35

} }

Planet.java

import java.awt.*;

public class Planet extends Panel implements Runnable { 【代码1】 //用Thread类声明一个moon对象 Mycanvas yellowBall;

double pointX[]=new double[360],

pointY[]=new double[360]; //用来表示画布左上角端点坐标的数组 int w=100,h=100; int radius=30; Planet()

{ setSize(w,h); setLayout(null);

yellowBall=new Mycanvas(); yellowBall.setColor(Color.yellow); add(yellowBall);

yellowBall.setSize(12,12); yellowBall.setR(12/2); pointX[0]=0; pointY[0]=-radius;

double angle=1*Math.PI/180; //刻度为1度

for(int i=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); }

for(int i=0;i<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线程,当前面板做为该线程的目标对象 }

public void start() { try{ moon .start(); }

catch(Exception exe){} }

public void paint(Graphics g) { g.setColor(Color.blue); g.fillOval(w/2-9,h/2-9,18,18); }

public void run()

36

搜索更多关于: Java龚永罡---答案 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

实验六 多线程 【目的】 ①掌握使用Thread的子类创建线程; ②学习使用Thread类创建线程; ③学习处理线程同步问题。 【内容】 1.汉字打字练习。 ? 实验要求: 编写一个Java应用程序,在主线程中再创建一个Frame类型的窗口,在该窗口中再创建1个线程giveWord。线程giveWord每隔2秒钟给出一个汉字,用户使用一种汉字输入法将该汉字输入到文本框中。 ? 程序运行效果示例: 程序运行效果如下图所示: ? 程序模板: WordThread.java import java.awt.*; public class Word

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com