JAVA======利用定时器时间来控制移动的对象

来源:互联网 发布:佳能2204n网络驱动 编辑:程序博客网 时间:2024/06/07 22:21
import javax.swing.*;import java.awt.*;import java.awt.event.*;class Day1031_Frame extends JFrame implements ActionListener{public static void main(String[] srgs){Day1031_Frame D=new Day1031_Frame();}Timer timer;int py;JLabel l1,l2;public Day1031_Frame(){super("窗口");Container c=getContentPane();c.setLayout(null);c.setBackground(Color.WHITE);ImageIcon i1=new ImageIcon("1.gif");ImageIcon i2=new ImageIcon("2.gif");l1=new JLabel(i1);l1.setSize(104, 120);l1.setLocation(170,230);c.add(l1);l2=new JLabel(i2);l2.setSize(500, 400);l2.setLocation(170,230);c.add(l2);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(500,400);setVisible(true);timer=new Timer(100,this);py=230;timer.start();}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()==timer){py-=10;if(py<10)py=230;l2.setLocation(170,py);}}}

原创粉丝点击