⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadstate.java

📁 刘艺编著的java教程的课本习题加例题代码 很有用哦!
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
public class Threadstate extends WindowAdapter{ 
 static Frame f;
 static Thread1 th1,th2;
 public static void main(String arg[]){
  Threadstate ts=new Threadstate();
  ts.display();
  th1=new Thread1("Java Language");
  th2=new Thread1("Program");
  th1.start();
  th1.setButton();
 }
 public void display(){
   f=new Frame("Threadstate");
   f.setSize(300,350); 
   f.setLocation(300,150);
   f.setBackground(Color.lightGray);
   f.setLayout(new GridLayout(2,2));
   f.addWindowListener(this);
   f.setVisible(true);
 }
 public void windowClosing(WindowEvent e){
  System.exit(0);
 }
}
class Thread1 extends Thread implements ActionListener{
  Panel p1;
  Label lb1;
  TextField tf1,tf2;
  Button b1,b2;
  int sleeptime=(int)(Math.random()*10000);
  public Thread1(String str){
   super(str);
   for(int i=0;i<10;i++)
      str=str+" ";
   tf1=new TextField(str);
   Threadstate.f.add(tf1);
   p1=new Panel();
   p1.setLayout(new FlowLayout(FlowLayout.CENTER));
   lb1=new Label("sleep time");
   tf2=new TextField(""+sleeptime);
   p1.add(lb1);
   p1.add(tf2);
   b1=new Button("start");
   b2=new Button("Interrupt");
   p1.add(b1);
   p1.add(b2);
   b1.addActionListener(this);
   b2.addActionListener(this);
   Threadstate.f.add(p1);
   Threadstate.f.setVisible(true);
  }
  public void run() {
    String str;
    while(this.isAlive()&&!this.isInterrupted()){
      try{
        str=tf1.getText();
        str=str.substring(1)+str.substring(0,1);
        tf1.setText(str);
        this.sleep(sleeptime);
      }
      catch(InterruptedException e){
         System.out.println(e);
      }
    }
   }
   public void setButton(){ 
    if(this.isAlive())b1.setEnabled(false);
   }
   public void actionPerformed(ActionEvent e){
    if((e.getSource()==Threadstate.th1.b1||e.getSource()==Threadstate.th1.b2))
      actionPerformed(e,Threadstate.th1);
    if((e.getSource()==Threadstate.th2.b1||e.getSource()==Threadstate.th2.b2))
      actionPerformed(e,Threadstate.th2);
   }
   public void actionPerformed(ActionEvent e,Thread1 th){
    if(e.getSource()==th.b1){
      th.sleeptime=Integer.parseInt(th.tf2.getText());
      th.start();
    }
    if(e.getSource()==th.b2)
       th.interrupt();
    th.setButton();
  }
}

   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -