アットウィキロゴ

px09

import java.awt.*;
import java.awt.event.*;

class java0726 extends Frame implements Runnable{

Thread th;
int nx;
Button bu;
int dx;

public static void main(String [] args) {
     Frame f=new java0726(); 
     f.setTitle("java0726"); 
     f.setSize(600,600); 
     f.setBackground(Color.yellow); 
     f.setVisible(true); 
 } 

java0726(){
setLayout(new FlowLayout());
Button bu = new Button( "ボタン" );
TextField te = new TextField();
bu.addActionListener(new MyListener());
add(bu, BorderLayout.NORTH );
add(te, BorderLayout.SOUTH );
setVisible( true );
addWindowListener(new stopwin());
nx=1;
dx=1;
th=new Thread(this);
th.start();
}

class MyListener implements ActionListener {
// イベントが発生すると、actionPerformed()メソッドが呼ばれる
public void actionPerformed(ActionEvent e) {
	dx=-dx;
}
}



class stopwin extends WindowAdapter{
 public void windowClosing(WindowEvent we){System.exit(0);} 
 } 

public void update(Graphics g) {
paint(g);
}

public void paint( Graphics g ) {
int n;
System.out.println(nx);
g.setColor(Color.red);
g.drawRect(100+10*nx,100+10*nx,10-5*dx,10-5*dx);
nx=nx+1;
}

public void run() {
int i;
for (i=1;i<11;i++){
repaint();
try{th.sleep(1000);} catch(InterruptedException e) { }
}
}

}
最終更新:2010年09月20日 00:48