当前位置:编程学习 > JAVA >>

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

[java]
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(); 
    } 
    @Override 
    public void actionPerformed(ActionEvent e) { 
        // TODO Auto-generated method stub 
        if(e.getSource()==timer) 
        { 
            py-=10; 
            if(py<10) 
                py=230; 
            l2.setLocation(170,py); 
        } 
    } 
}<img src=/2012/1101/20121101104033698.gif" alt=""> 
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,