java–運行程式片

來源:互聯網
上載者:User

運行程式片,通過人機互動的過程,熟悉一些基本的事件機制

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

 

class StaticTest{

  //靜態全域變數

  static int count=0;

  public StaticTest(){

    ++count;

    System.out.println("count:"+count);

  }

}

 

public class Test extends Applet{

  private Button btn=new Button("建立");

  private TextField tf=new TextField(10);

 

  //匿名內部類  

  class Create implements ActionListener{

    public void actionPerformed(ActionEvent e){

      //每次執行個體化對象時,count的值會一直累加,而不是重新從0開始

      StaticTest st=new StaticTest();

    }  

  }

 

  @Override

  public void init(){

    add(btn);

    //添加點擊事件

    btn.addActionListener(new Create());

    add(tf);

  }

 

  public static void main(String[] args){

    Test t=new Test();

    Frame frame=new Frame();

    //給視窗添加關閉事件

    frame.addWindowListener(

      new ActionAdapter(){

        public void windowClosing(WindowEvent e){

          System.exit(0);

        }

      }

    );

    frame.add(t,BoderLayout.CENTER);

    frame.setSize(300,200);

    //初始化視窗內控制項

    t.init();

    t.start();

    //顯示程式片視窗

    frame.setVisible(true);

  }

}

 

//多線程

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

 

public class Test extends Applet{

  private Button start=new Button("開始“),

          stop=new Button("停止");

  private TextField tf=new TextField(10);

  private runFlag=true;

  private static int count=0;

  private ThreadTest tt=null;

  //將ThreadTest作為一個匿名內部類

  private ThreadTest extends Thread{

    //在構造ThreadTest樣本對象的時候,啟動線程

    public ThreadTest(){

      start();

    }

    @Override

    public void run(){

      while(true){

        try{

          Thread.currentThread().sleep(200);

        }catch(InterruptedException e){

          e.printStackTrace();

        }

        if(runFlag){

          tf.setText(Integer.toString(++count));

        }

      }

    }

  }

 

  class Start implements ActionListener{

    @Override

    public void actionPerformed(ActionEvent e){

      if(tt==null)

        tt=new ThreadTest();

    }

  }

 

  class Stop implements ActionListener{

     @Override

     public void actionPerformed(ActionEvent e){

        if(tt!=null)

          runFlag=!runFlag;

     }

  }

 

  @Override

  public void init(){

    add(stop);

    stop.addActionListener(new Stop());

    add(start);

    start.addActionListener(new Start());

    add(tf);

  }

 

  public static void main(String[] args){

    Frame frame=new Frame("程式片");

    Test t=new Test();

    frame.addWindowListener(

      new WindowAdapter(){

        @Override

        public void windowClosing(WindwoEvent e){

          System.exit(0);

        }

      }

    );

    frame.add(t,BorderLayout.CENTER);

    frame.setSize(300,200);

    t.init();

    t.start();

    frame.setVisible(true);

  }

}

 

//在主類中合并線程

要將線程與主類合并,必須實現Runnable介面,重寫run()方法,但這與從Thread繼承的類還是有很大的差別。若要開啟一個新的線程,只需將主類作為一個參數傳入即可(類型上溯(Runnable))

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

 

//實現Runnable介面

public class Test extends Applet implements Runnable{

 

  private Button start=new Button("Start"),

          stop=new Button("Stop");

  private TextField tf=new TextField(10);

  private Thread th=null;

  private boolean runFlag=true;

  private static int count=0;

 

  //重寫run方法

  @Override

  public void run(){

    while(true){

      try{

        th.sleep(200);

      }catch(InterruptedException e){

        e.printStackTrace();

      }

      if(runFlag)

        tf.setText(Integer.toString(++count));

    }

  }

 

  class Start implements ActionListener{

    @Override

    public void actionPerformed(ActionEvent e){

      if(th==null){

        //傳入當前內容物件,即上溯類型Runnable(因為實現了此介面)

        th=new Thread(Test.this);

        th.start();

      }

    }

  }

 

  class Stop implements ActionListener{

    @Override

    public void actionPerformed(ActionEvent e){

      runFlag=!runFlag;

    }

  }

 

  @Override

  public void init(){

    add(start);

    start.addActionListener(new Start());

    add(stop);

    stop.addActionListener(new Stop());

    add(tf);

  }

 

  public static void main(String[] args){

    Frame frame=new Frame("程式片");

    Test t=new Test();

    frame.add(t,BorderLayout.CENTER);

    frame.addWindowListener(

       new WindowAdapter(){

         @Override

          public void windowClosing(WindwoEvent e){

            System.exit(0);

          }

        }

    );

    frame.setSize(300,200);

    t.init();

    t.start();

    frame.setVisible(true);

  }

}

 

//基礎知識:在匿名內部類中不能聲明靜態變數

 

 

/**--注意--**/

以上代碼都是在文字編輯器中寫的,可能會有些許紕漏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.