1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MoveButton
{
public static void main( String[] arg )
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JPanel panel = new JPanel();
panel.setLayout( null );
final Chariot chariot = new Chariot( 0, 0, 100, 100 );
panel.add( chariot );
chariot.applyLocation();
frame.getContentPane().add( panel );
frame.setSize( 640, 480 );
frame.setVisible( true );
ActionListener taskPerformer = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
test();
}
public void test() {
chariot.deplaceX( 10 );
}
};
Timer timer = new Timer( 500, taskPerformer );
timer.start();
}
static class Chariot extends JButton
{
private int pos_X = 0;
private int pos_Y = 0;
private int width = 0;
private int height = 0;
public Chariot( int x, int y, int w, int h )
{
pos_X = x;
pos_Y = y;
width = w;
height = h;
}
public void applyLocation()
{
setBounds( pos_X, pos_Y, width, height );
}
public void deplaceX( int deplace )
{
pos_X += deplace;
applyLocation();
}
}
} |
Partager