📄
字号:
19-例子1
public class Example19_1
{ static Lefthand left;static Righthand right;
public static void main(String args[])
{left=new Lefthand() ;//创建两个线程。
right=new Righthand();
left.start(); //线程开始运行后,Lefthand类中的run方法将被执行。
right.start();
}
}
class Lefthand extends Thread
{
public void run()
{
for(int i=0;i<=5;i++)
{ System.out.println("i am student");
try{sleep(500);}
catch(InterruptedException e){}
}
}
}
class Righthand extends Thread
{
public void run()
{
for(int i=0;i<=5;i++)
{System.out.println("i am oookkk");
try{sleep(300);}
catch(InterruptedException e){}
}
}
}
19-例子2
import java.applet.*;import java.awt.*;
public class Example19_2 extends java.applet.Applet implements Runnable
{ Thread circleThread;
public void start()
{
if (circleThread ==null)
{
circleThread =new Thread(this);
circleThread .start();
}
}
public void run ()
{
while(circleThread !=null)
{
repaint();
try{
circleThread .sleep(1000);
}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{double i=Math.random();
if(i<0.5)
g.setColor(Color.red);
else
g.setColor(Color.blue);
g.fillOval(100,100,(int)(100*i),(int)(100*i));
}
public void stop()
{
circleThread.yield();
circleThread =null;
}
}
19-例子3
import java.awt.*;
import java.awt.event.*;
public class Example19_3
{public static void main(String args[])
{Mywin win=new Mywin();win.pack();
}
}
class Mywin extends Frame implements Runnable
{ Button b=new Button("ok");int x=5;
Thread bird=null;
Mywin()
{setBounds(100,100,120,120);setLayout(new FlowLayout());
setVisible(true);
add(b);b.setBackground(Color.green);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}} );
bird=new Thread(this);//创建一个新的线程,窗口做目标对象,
//替线程bird实现接口Runnable。
bird.start(); //在创建窗口时又开始了线程bird。
}
public void run() //实现bird的操作。
{while(true)
{ x=x+1;
if(x>100) x=5;
b.setBounds(40,40,x,x);
try{bird.sleep(200);}
catch(InterruptedException e){}
}
}
}
19-例子4
import java.applet.*;import java.awt.*;
public class Example19_4 extends java.applet.Applet implements Runnable
{ int x=0; Thread Scrollwords=null;
public void init()
{setBackground(Color.cyan);
setForeground(Color.red);
setFont(new Font("TimesRoman",Font.BOLD,18));
}
public void start()
{
if (Scrollwords ==null)
{
Scrollwords =new Thread(this);
Scrollwords .start();
}
}
public void run ()
{
while(Scrollwords !=null)
{x=x+5;
if(x>500)
x=0;
repaint();
try{
Scrollwords .sleep(80);
}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{g.drawString("欢 迎 使 用 本 字 典",x,80);
}
public void stop()
{
Scrollwords.yield();
Scrollwords =null;
}
}
19-例子5
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example19_5 extends Applet implements ActionListener,Runnable
{ TextField text1,text2;
int x=0; Thread Scrollwords=null;
public void init()
{ setBackground(Color.cyan);
setForeground(Color.red);
setFont(new Font("TimesRoman",Font.BOLD,18));
text1=new TextField(10);
text2=new TextField(10);
add(new Label("输入一个英文单词:")); add(text1);
add(new Label("汉语意思:")); add(text2);
text1.addActionListener(this);
}
public void start()
{
if (Scrollwords ==null)
{
Scrollwords =new Thread(this);
Scrollwords .start();
}
}
public void run ()
{
while(Scrollwords !=null)
{x=x+5;
if(x>500)
x=0;
repaint();
try{
Scrollwords .sleep(80);
}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{g.drawString("欢 迎 使 用 本 字 典",x,120);
}
public void stop()
{
Scrollwords.yield();
Scrollwords =null;
}
public void actionPerformed(ActionEvent e)
{ if((e.getSource()==text1)&&(text1.getText().equals("boy")))
{ text2.setText("男孩");}
else if ((e.getSource()==text1)&&(text1.getText().equals("girl")))
{ text2.setText("女孩");}
else if ((e.getSource()==text1)&&(text1.getText().equals("sun")))
{ text2.setText("太阳");}
else
{ text2.setText("没有该单词");}
}
}
19-例子6
import java.applet.*;import java.awt.*;import java.awt.event.*;
public class Example19_6 extends Applet implements Runnable
{ Thread left ,right; Graphics mypen; int x,y;
public void init()
{ left=new Thread(this);right=new Thread(this);
x=10;y=10;mypen=getGraphics();
}
public void start()
{left.start();right.start();
}
public void run()
{ while(true)
if(Thread.currentThread()==left)
{ x=x+1;
if(x>240) x=10;
mypen.setColor(Color.blue);
mypen.clearRect(10,10,300,100);
mypen.drawRect(10+x,10,50,50);
try{left.sleep(60); }
catch(InterruptedException e){}
}
else if(Thread.currentThread()==right)
{ y=y+1;
if(y>240) y=10; mypen.setColor(Color.red);
mypen.clearRect(10,110,300,100);
mypen.drawOval(10+y,110,50,50);
try{right.sleep(60); }
catch(InterruptedException e){}
}
}
public void stop()
{left=null;right=null;
}
}
19-例子7
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Example19_7 extends Applet implements Runnable
{Thread 红色球,兰色球; Graphics redPen,bluePen;int t=0;
public void init()
{ 红色球=new Thread(this); 兰色球=new Thread(this);
redPen=getGraphics();
bluePen=getGraphics();
redPen.setColor(Color.red);
bluePen.setColor(Color.blue);
}
public void start()
{红色球.start();兰色球.start();
}
public void run()
{ while(true)
{ t=t+1;
if(Thread.currentThread()==红色球)
{if(t>100) t=0;
redPen.clearRect(0,0,110,400);
redPen.fillOval(50,(int)(1.0/2*t*9.8),5,5);
try{红色球.sleep(40);}
catch(InterruptedException e){}
}
else if(Thread.currentThread()==兰色球)
{bluePen.clearRect(120,0,900,500);
bluePen.fillOval(120+7*t,(int)(1.0/2*t*9.8),5,5);
try{兰色球.sleep(40);}
catch(InterruptedException e){}
}
}
}
}
19-例子8
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example19_8 extends Applet implements ActionListener
{ Toolkit toolkit;Button button;
public void init()
{ toolkit=getToolkit();//获得一个工具包对象
button=new Button("确定");add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button)
{for(int i=0;i<=9;i++)
{toolkit.beep();
try {Thread.sleep(500);} //每隔半秒钟发出嘟声
catch(InterruptedException e1){}
}
}
}
}
19-例子9
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example19_9 extends Applet implements Runnable
{ int money=100;TextArea text1,text2;
Thread 会计,出纳;
public void init()
{会计=new Thread(this); 出纳=new Thread(this);//创建两个线程:会计、出纳。
text1=new TextArea(20,8); text2=new TextArea(20,8);
add(text1);add(text2);
}
public void start()
{ 会计.start();出纳.start(); //线程开始。
}
public synchronized void 存取(int number) //存取方法。
{ if(Thread.currentThread()==会计)
{ for(int i=1;i<=3;i++) //会计使用存取方法存入90元,存入30元,稍歇一下
{money=money+number; //这时出纳仍不能使用存取方法,
try {Thread.sleep(1000);} //因为会计还没使用完存取方法。
catch(InterruptedException e){}
text1.append("\n"+money);
}
}
else if(Thread.currentThread()==出纳)
{for(int i=1;i<=2;i++) //出纳使用存取方法取出30元,取出15元,稍歇一下,
{money=money-number/2; //这时会计仍不能使用存取方法,
try {Thread.sleep(1000);} //因为出纳还没使用完存取方法。
catch(InterruptedException e){}
text2.append("\n"+money);
}
}
}
public void run()
{ if(Thread.currentThread()==会计||Thread.currentThread()==出纳)
{for(int i=1;i<=3;i++) //从周一到周三会计和出纳都要使用帐本。
{ 存取(30);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -