?? 第十九章例子.txt
字號:
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 + -