6.2java上课用
package xu01;
public class xch2 extends Thread{
public xch2(String name,int priorty) {
super(name);
this.setPriority(priorty);
}
public void run()
{System.out.println(this.getName()+"正在执行");
try {
Thread.sleep(1000);//让线程休眠1秒钟,然后再出现异常
}catch(Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
xch2 t1=new xch2("a",5);
xch2 t2=new xch2("b",10);
xch2 t3=new xch2("c",8);
t1.start();
t2.start();
t3.start();
}
}
package xu01;
import java.awt.*;
import javax.swing.JFrame;
public class MoveingText extends JFrame implements Runnable{
Label m_label;
int i=0;
boolean bRight=true;
public MoveingText() {
Container con=getContentPane();
con.setLayout(null);
m_label=new Label("多线程可实现动画效果");
m_label.setBounds(10,100,150,50);
con.add(m_label,"Center");
setTitle("飘动的字幕");
setSize(340,260);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread td=new Thread(this);
td.start();
}
public static void main(String[] args) {
MoveingText fr=new MoveingText();
}
public void run()
{try {
Thread t=Thread.currentThread();
System.out.println("当前线程是:"+t);
while(true) {
Thread.sleep(1000);
m_label.setBounds(10+i*10, 100+i*5, 150, 50);
if(i>20) {bRight=false;
m_label.setFont(new Font("黑体",Font.ITALIC,20));
m_label.setForeground(Color.red);
}
if(i<0) {bRight=true;
m_label.setFont(new Font("黑体",Font.BOLD,25));
m_label.setForeground(Color.green);
}
if(bRight)
i++;
else
i--;
}
}catch(Exception e) {}
}
}