Java圖形界面學習

<span style="font-size:18px;">import java.awt.*;
import java.awt.event.*;

public class Main extends WindowAdapter{
	Frame f;
	T wt1, wt2;
	public class T extends Thread implements ActionListener{
		Panel p; TextField tf; Button b1,b2;
		boolean isPaused;
		public T(String name){
			super(name);
			for(int i  = 1; i <= 10; ++i)
				name += " ";
			tf = new TextField(name);
			p = new Panel();
			b1 = new Button("Start");
			b2 = new Button("Dead");
			b1.addActionListener(this);
			b2.addActionListener(this);
			b2.setEnabled(false);
			p.setLayout(new FlowLayout(FlowLayout.CENTER));
			p.add(b1);p.add(b2);f.add(tf);f.add(p);
		}
		public void actionPerformed(ActionEvent e){
			String s;
			if(e.getSource() == b1){
				s = b1.getLabel();
				if(s.compareTo("Start") == 0){
					b1.setLabel("Stop");
					b2.setEnabled(true);
					start();
				}
				if(s.compareTo("Stop") == 0){
					b1.setLabel("Restart");
					isPaused = true;
				}
				if(s.compareTo("Restart") == 0){
					b1.setLabel("Stop");
					isPaused = false;
				}
			}
			if(e.getSource() == b2){
				b1.setEnabled(false);
				b2.setEnabled(false);
				interrupt();
			}
		}
		public void run(){
			while(true){
				if(isPaused == false){
					String s = tf.getText();
					s = s.substring(1)+s.substring(0,1);
					tf.setText(s);
				}
				try{
					sleep(100);
				}
				catch(InterruptedException e){
					break;
				}
			}
			tf.setText("The Thread has dead already");
		}
	}
	public void windowClosing(WindowEvent e){
		System.exit(0);
	}
	public void display(){
		f = new Frame("Learning Java Graphic Interface");
		wt1 = this.new T("Welcome!");
		wt2 = this.new T("How are you?");
		f.setSize(300, 240);
		f.setLocation(400, 240);
		f.setBackground(Color.lightGray);
		f.setLayout(new GridLayout(4, 2));
		f.addWindowListener(this);
		f.setVisible(true);
	}
	public static void main(String []args){
		Main m = new Main();
		m.display();
	}
}</span>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章