[英]How can I control Timer animation with a JTextField value?
I want to be able to control the speed or delay of a timer that moves a image across the screen with a JTextField
. 我希望能够控制使用
JTextField
在屏幕上移动图像的计时器的速度或延迟。 I getting a NullPointerException
at CarAnimationPanel
line 61, which is the timer.start()
line. 我在
CarAnimationPanel
第61行获得了NullPointerException
,这是timer.start()
行。
This is my code... 这是我的代码...
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);
}
}
Various fixes, see code for details. 各种修复程序,请参见代码以获取详细信息。 I also swapped out the
JTextField
for a JSpinner
. 我还将
JTextField
换成JSpinner
。
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);
}
}
The exception is probably saying that timer
hasn't been created properly, and is null
. 例外可能是
timer
未正确创建,并且为null
。 If you look at your code, the timer
isn't actually created until your ActionListener
is run. 如果您查看代码,则直到运行
ActionListener
才会真正创建timer
。 You will need to move your timer.start()
into the ActionListener
, so that it only runs start()
after the timer
is created. 您将需要将
timer.start()
移到ActionListener
,以便仅在创建timer
后才运行start()
。
Something like this (Note the ADDED
and REMOVED
comments to see what I changed)... 这样的事情(请注意
ADDED
和REMOVED
注释以查看我所做的更改)...
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.