public class Car{
private static int speed;
public static void main(String args[]){
//Timer timer=new Timer();
JFrame win=new JFrame();
JPanel panel=new JPanel();
JButton btn1=new JButton("速度1");
JButton btn2=new JButton("速度2");
Mycanvs canvs=new Mycanvs();
win.setLayout(null);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
speed=10;
}
});
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
speed=30;
//timer.schedule(, time);
}
});
//panel.setBounds(100,100, 200, 200);
canvs.setBounds(100,100, 200, 200);
canvs.setBorder(BorderFactory.createLineBorder(Color.blue,1));
canvs.setBackground(Color.BLUE);
//panel.setBorder(BorderFactory.createLineBorder(Color.blue,1));
//panel.add(canvs);
btn1.setBounds(40, 10, 100, 30);
btn2.setBounds(200, 10, 100, 30);
win.add(canvs);
win.add(btn1);
win.add(btn2);
win.setBounds(100, 100, 400, 400);
win.setVisible(true);
}
public static class Mycanvs extends JPanel{
int x=100;
int y=100;
public void paint(Graphics g){
g.fillRect(x, y, 20, 20);
}
public void repaint(){
if(x==100){
x=0;
y=0;
}
x+=speed;
y+=speed;
}
}