2015-09-08 17 views
0

我认为我的标题很好地总结了我的问题。我对编程非常陌生(使用Head First Java进行大约2-3周的自我教学),并且我尝试使用Action Listener在屏幕上来回移动,直到用户终止程序。我的问题是我有一个Timer和2个类实现Action Listener。我可以让它来回运行一次,但这就是我所能做到的。使用Java中的EventHandlers来回移动形状

package Animation; 

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class Window extends JPanel implements ActionListener{ 
int x = 100; 
Timer timer = new Timer(5, this); 
int velX = 2; 

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    this.setBackground(Color.BLACK); 
    g.setColor(Color.BLUE); 
    g.fill3DRect(x, 150, 200, 200, true); 
    timer.start(); 
    if(x >= 1000){ 
     timer.addActionListener(new HandlerClass()); 
      }//end if 

    }//end paintComponent 

public void actionPerformed(ActionEvent e){ 
    x += velX; 
    repaint(); 
}//end actionP method 

public class HandlerClass implements ActionListener{ 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(x >= 100){ 
     x -= velX; 
     repaint(); 
     } 
    }//end actionP 
}//end Handler 

}//end Window 

我见过人们使用按钮来搬东西来回或移动的东西的设定次数的问题,但我希望定时器做的一切,直到用户退出。这样用户只需要观看。我没有将我的主要方法包括进来,因为它只是设置了gui并称为这个类。

回答

1

首先,请查看Painting in AWT and SwingPerforming Custom Painting了解有关如何在Swing中绘画的更多详细信息。

基本上,Swing使用被动渲染算法,这意味着任何时候出于任何原因都可能出现绘画循环,大部分时间没有您的直接干预。

您不应该在任何绘画方法中更改组件的状态,绘画方法应该绘画,就是这样。

有一些小问题,首先,要添加的Window类和HandlerClassActionListener年代到Timer,这意味着你可能会进入一两个听众,每个竞争的位置其他物体应该以何种方式移动。更好地把你的运动逻辑放在一个单一的方法,例如:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Window extends JPanel { 

    int x = 100; 
    Timer timer = new Timer(5, new HandlerClass()); 
    int velX = 2; 

    public Window() { 
     this.setBackground(Color.BLACK); 
     timer.start(); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(400, 400); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.BLUE); 
     g.fill3DRect(x, 150, 200, 200, true); 

    }//end paintComponent 

    public class HandlerClass implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      x += velX; 
      if (x + 200 >= getWidth()) { 
       x = getWidth() - 200; 
       velX *= -1; 
      } else if (x < 0) { 
       x = 0; 
       velX *= -1; 
      } 
      repaint(); 
     }//end actionP 
    }//end Handler 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new Window()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

}//end Window 
+0

谢谢,它现在看到它应该如何工作更有意义。我想我过于复杂了。 – user5311076