Java 如何使用JTextField值控制计时器动画?
我希望能够通过Java 如何使用JTextField值控制计时器动画?,java,swing,timer,actionlistener,jtextfield,Java,Swing,Timer,Actionlistener,Jtextfield,我希望能够通过JTextField控制计时器在屏幕上移动图像的速度或延迟。我在CarAnimationPanel第61行得到一个NullPointerException,这是timer.start()行 这是我的密码 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; public class Exercise18_17 extend
JTextField
控制计时器在屏幕上移动图像的速度或延迟。我在CarAnimationPanel
第61行得到一个NullPointerException
,这是timer.start()
行
这是我的密码
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);
}
}
例外情况可能是
计时器
没有正确创建,并且为空
。如果查看代码,则在运行ActionListener
之前,不会实际创建计时器。您需要将timer.start()
移动到ActionListener
中,以便它仅在创建计时器后运行start()
类似这样的内容(注意添加的和删除的注释,以查看我所做的更改)
例外情况可能是计时器
没有正确创建,并且为空
。如果查看代码,则在运行ActionListener
之前,不会实际创建计时器。您需要将timer.start()
移动到ActionListener
中,以便它仅在创建计时器后运行start()
类似这样的内容(注意添加的和删除的注释,以查看我所做的更改)
各种修复,请参阅代码了解详细信息。我还将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);
}
}
各种修复,请参阅代码了解详细信息。我还将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);
}
}
谢谢,这确实有效,但我不得不改回textfield和actionlistener,因为我们还没有讨论JSpinners一章。我发现我的代码有两个问题。我需要在计时器中创建carAnimationPanel的一个对象,并将timer.start()移动到actionevent中。谢谢,这确实有效,但我不得不改回textfield和actionlistener,因为我们还没有讨论JSpinners一章。我发现我的代码有两个问题。我需要在计时器中创建carAnimationPanel的对象,并将timer.start()移动到actionevent中。您的权限和我确实尝试了此操作,但我收到了NoClassDefFoundError异常,但我在前面的帖子中看到,我不仅需要移动timer.start(),还需要更改计时器的参数。定时器=新定时器(延迟,CarAnimationPanel.this);timer.start();您的权利,我确实尝试了这个,但我收到了NoClassDefFoundError异常,但我在前面的帖子中看到,我不仅需要移动timer.start(),还需要更改timer的参数。定时器=新定时器(延迟,CarAnimationPanel.this);timer.start();