runnabledem.java

来自「java关于线程的一些实例」· Java 代码 · 共 43 行

JAVA
43
字号
import java.awt.*;
import java.awt.event.*;
public class RunnableDem {	

	
	public static void main(String[] args) {
		Mywindow my=new Mywindow();
		my.pack();

	}

}
class Mywindow extends Frame implements Runnable{
	Button b=new Button("ok");
	int x=5;
	Thread t=null;
	Mywindow(){
		setBounds(500,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);
			}
		});
		t=new Thread(this);
		t.start();
	}
	public void run(){
		while(true){
			x=x+1;
			if(x>100)
				x=5;
			b.setBounds(40,40,x,x);
			try{
				t.sleep(200);
			}catch(InterruptedException e){}
		}
	}
}

⌨️ 快捷键说明

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