English 中文(简体)
如何用 JText Field 值控制定时器动画?
原标题:How can I control Timer animation with a JTextField value?

我希望能够控制一个定时器的速度或延迟, 该定时器将图像用 JText Field 移动到屏幕上。 我在 CarAnimation Panel 第61行, 即 timer.start () 线上获得一个 NullPointer Expendion

这是我的密码...

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);
    }
}
问题回答

各种修正, 详情请见代码 。 我还将 < code> JText Field 换成 < code> JSpinner 。

"https://i.sstatic.net/vHsHM.png" alt="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);
    }
}

例外可能是说 timer 尚未正确创建, 并且是 null 。 如果您查看您的代码, 在您的 ActionListener 运行之前, actionListener 实际上是没有创建的。 您需要将您的 timer. start () 移动到 ActionListener , 这样它只能在 start () 创建后运行 start ()

类似的话(注意 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;




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签