2012-05-26 51 views
0

我希望能夠控制定時器的速度或延遲,該定時器通過JTextField在屏幕上移動圖像。我得到NullPointerExceptionCarAnimationPanel第61行,這是timer.start()行。如何使用JTextField值控制Timer動畫?

這是我的代碼...

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.applet.*; 
import java.net.*; 

public class Exercise18_17 extends JApplet { 
    private static JTextField jtfCar1; 
    private int num1; 

    public Exercise18_17(){ 

     URL imageURL = this.getClass().getResource("images/TN_buick 1912small.GIF"); 
     Image image = new ImageIcon(imageURL).getImage(); 

     setLayout(new GridLayout(5,4,2,2)); 
     add(new CarAnimationPanel(image)); 

    }//endo of 15_15 constructor 

public static class CarAnimationPanel extends JPanel implements ActionListener { 
    private Image image; 
    private int delay ; 
    private int num1 = 0; 
    private Timer timer; 

    int x = 0; 
    int y = 20; 

    public CarAnimationPanel(Image image) { 
     add(jtfCar1 = new JTextField(5)); 

     jtfCar1.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       num1 = Integer.parseInt(jtfCar1.getText().trim()); 
       if (e.getSource() == jtfCar1){ 
        delay = num1; 
        timer = new Timer(delay, this); 
       } 
      } 
     }); 
     timer.start(); 
     this.image = image; 
    } 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 

     if (x > getWidth()) { 
      x -= 20; 
     } 
     x += 5; 
     g.drawImage(image, x, y, this); 
    } 

    public void actionPerformed(ActionEvent e) { 
     repaint(); 
    } 
} 

public static void main(String[] args) { 
    // Create a frame 
    JFrame frame = new JFrame("Exercise18_17"); 

    // Create an instance of the applet 
    JApplet applet = new Exercise18_17(); 

    // Add the applet to the frame 
    frame.add(applet, BorderLayout.CENTER); 

    // Invoke applet's init method 
    applet.init(); 

    // Display the frame 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(300, 200); 
    frame.setLocationRelativeTo(null); // Center the frame 
    frame.setVisible(true); 
    } 
} 

回答

1

唯一的例外可能是說timer尚未正確創建的,並且是null。如果你看看你的代碼,timer直到你的ActionListener運行時纔會被創建。您需要將timer.start()移動到ActionListener,以便在timer創建後僅運行start()

像這樣的東西(注ADDEDREMOVED意見,看看有什麼我改變了)...

jtfCar1.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
     num1 = Integer.parseInt(jtfCar1.getText().trim()); 
     if (e.getSource() == jtfCar1){ 
      delay = num1; 
      timer = new Timer(delay, this); 
      timer.start(); // ADDED 
     } 
    } 
}); 
//timer.start(); //REMOVED 
this.image = image; 
+0

你的權利,我沒有嘗試,但我收到的NoClassDefFoundError異常,但我看到在前面的貼子,我不僅需要移動timer.start(),但改變定時器的參數。 timer = new Timer(delay,CarAnimationPanel.this); timer.start(); – user1418705

2

的各種修補程序,請參見代碼的細節。我也換了JTextFieldJSpinner

Exercise18_17

package test.t100.t003; 

import javax.swing.*; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

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

public class Exercise18_17 extends JApplet { 
    private static final long serialVersionUID = 1L; 
    private static JSpinner jtfCar1; 

    public Exercise18_17() throws MalformedURLException { 

     URL imageURL = new URL(
       "http://pscode.org/media/starzoom-thumb.gif"); 
     Image image = new ImageIcon(imageURL).getImage(); 

     setLayout(new GridLayout(1, 0, 2, 2)); 
     add(new CarAnimationPanel(image)); 
    }// endo of 15_15 constructor 

    public static class CarAnimationPanel extends JPanel implements 
      ActionListener { 
     private static final long serialVersionUID = 1L; 
     private Image image; 
     private int delay; 
     private int num1 = 0; 
     private Timer timer; 

     int x = 0; 
     int y = 20; 

     public CarAnimationPanel(Image image) { 

      add(jtfCar1 = new JSpinner(new SpinnerNumberModel(
        150, 40, 200, 1))); 

      jtfCar1.addChangeListener(new ChangeListener() { 

       @Override 
       public void stateChanged(ChangeEvent arg0) { 
        num1 = ((Integer)jtfCar1.getValue()).intValue(); 
        delay = num1; 
        timer = new Timer(delay, CarAnimationPanel.this); 
        timer.start(); 
       } 
      }); 
      this.image = image; 
     } 

     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (x > getWidth()) { 
       x -= getWidth(); 
      } 
      x += 5; 
      g.drawImage(image, x, y, this); 
     } 

     public void actionPerformed(ActionEvent e) { 
      repaint(); 
     } 
    } 

    public static void main(String[] args) throws MalformedURLException { 
     // Create a frame 
     JFrame frame = new JFrame("Exercise18_17"); 

     // Create an instance of the applet 
     JApplet applet = new Exercise18_17(); 

     // Add the applet to the frame 
     frame.add(applet, BorderLayout.CENTER); 

     // Invoke applet's init method 
     applet.init(); 

     // Display the frame 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(300, 200); 
     frame.setLocationRelativeTo(null); // Center the frame 
     frame.setVisible(true); 
    } 
} 
+0

謝謝,這確實奏效了,但我不得不改回textfield和actionlistener,因爲我們沒有涵蓋JSpinners的章節。我看到了我的代碼的兩個問題。我需要在定時器中創建carAnimationPanel的對象,並將timer.start()移入actionevent。 – user1418705