通过计时器运动

时间:2016-05-05 04:41:00

标签: java swing timer

我试图根据几个属性在页面上移动几个小框。运动本身由方法private void direction_move_creature(){处理。然后我打电话给:

public class FormEvents : BindableBase
{
   private string someName;
    public string SomeName
    {
        get {return someName;}
        set { SetProperty(ref someName, value);}
    }

    public FormEvents()
    {
        DXTabItem myTabItem= new DXTabItem();
        myTabItem.Header = new Label()
        { 
            Name= "lblTabAccountHeader", 
            Content = "MyTab" + Convert.ToString(UserID) 
        };
        SomeName = lblTabAccountHeader.Content;
    }
}

我的主要方法是:

public class YourView 
{
    prvate FormEvents instance = new FormEvents();

    public YourView()
    {
        instance.SomeName.PropertyChanged += EventHandler;

    }

    private void EventHandler(object obj)
    {
        TextBoxinYourView.Text = instance.SomeName;
    }
}

new Draw只填充包含移动形状坐标的结构。当我尝试运行该程序时,我什么都没有运动。我知道这些只是代码片段,但任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:4)

你误解了Swing Timer的工作原理。 Timer 替换 for循环。摆脱循环并在计时器的ActionListener中增加一个计数器。

如,

class MyActionListener implements ActionListener {
    private int counter = 0;

    @Override        
    public void actionPerformed(ActionEvent e) {
        // assuming MAX_COUNTER is 20
        if (counter < MAX_COUNTER) {
            direction_move_creature();
            repaint(); 
            counter++;
        } else {
            // stop the Timer
            ((Timer) e.getSource()).stop();
        }
    }
}