miércoles, 9 de mayo de 2007

SWING con Timer


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ejemplo extends JFrame implements ActionListener{
Timer timer;
JLabel avion;
JButton abajo;
int x=1,y=10;
public ejemplo(){
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
avion=new JLabel(new ImageIcon("avion.gif"));
avion.setBounds(0, 10, 200, 80);
getContentPane().add(avion);
timer=new Timer(1,this);
timer.start();
abajo=new JButton("abajo");
abajo.setBounds(200,500,100,40);
getContentPane().add(abajo);
abajo.addActionListener(this);
}
public static void main(String[] args) {
new ejemplo().setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==timer){
x=x+1;
avion.setLocation(x, y);
}
if(e.getSource()==abajo){
y=y+1;
avion.setLocation(x, y);
}
}
}

No hay comentarios: