如何在计时器到达数字时更改JFrame中的图像?

时间:2016-01-20 12:48:01

标签: java swing

正如标题所说,我试图使用计时器更改jframe中的图像。但它会抛出NullPointerException。我是java的新手,虽然我正在研究异常处理,但是你能给我的任何方向都会对我的学习有所帮助。 这是我的代码:

public class PointTest extends JPanel {
    JLabel drawcloseLabel = new JLabel(new ImageIcon("src/images/draw_closeup.jpg"));
    JLabel monsterAttack = new JLabel("src/images/monsterAttack.jpg");
    JLabel endGameLabel = new JLabel(new ImageIcon("src/images/monsterAttack.jpg"));
    private static final long serialVersionUID = 1L; // i dont get this
    private JFrame frame;

    boolean off = false;
    public JButton opendraw;
    // public JButton button2;
    public JButton ok;
    public JButton back;
    public JButton pickUpKey;
    public JButton Key;
    public JButton unlockDoor;
    public JButton lockedDoor;

    public JButton btnNewButton;
    JPanel background;
    JPanel door;
    JPanel draw;
    JPanel drawopen;
    JPanel endGame;

    public void room() {

        frame = new JFrame("room");


        frame.setSize(1280, 960);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLayeredPane layeredPane = new JLayeredPane();
        frame.getContentPane().add(layeredPane, BorderLayout.CENTER);

        background = new JPanel();



endGame = new JPanel();
    JLabel endGameLabel = new JLabel(new ImageIcon("src/images`/OutOfTime.jpg"));`
    JLabel picLabel = new JLabel(new ImageIcon("src/images/room.jpg"));


        background.add(picLabel);

        endGame.add(endGameLabel);

        background.setBounds(0, 0, 1280, 960);


        layeredPane.add(background, JLayeredPane.DEFAULT_LAYER);


    }

这是一种方法,如果我捕获异常,将继续倒计时到0,但不会更改图像。

         public void EndTimer() {

        //error
        background.setVisible(false);

        endGame.setVisible(true);
        endGame.add(endGameLabel);
        endGame.setBounds(0, 0, 1280, 960);




    }

}

Timer的倒计时类。

import java.util.Timer;
import java.util.TimerTask;

public class Countdown {
    static int interval;
    static Timer timer;
    boolean off;
    PointTest pt = new PointTest();

    public void Countdown1() {

        int seconds = 10;
        int delay = 1000;
        int period = 1000;
        timer = new Timer();

        interval = seconds;

        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                System.out.println(setInterval());
                if (interval == 7) {
                    System.out.println("works");

                    pt.EndTimer();

                }

            }
        }, delay, period);
    }

    private int setInterval() {
        if (interval == 1)
            timer.cancel();
        return --interval;

    }

    public int getInterval() {

        return this.setInterval();
    }

}



import java.io.IOException;



public class windowMain {


    /**
     * Launch the application.
     * 
     */


    public static void main(String[] args) throws IOException {


        Countdown cd = new Countdown();
        cd.Countdown1();
        PointTest point = new PointTest();
        point.room();




    }

}

堆栈跟踪:

Exception in thread "Timer-0" works
test
java.lang.NullPointerException
    at PointTest.EndTimer(PointTest.java:207)
    at Countdown$1.run(Countdown.java:27)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

尝试从另一个线程更新Swing数据结构时存在一些问题。 您需要使用计时器中的invokeAndWait或invokeLater在Swing事件处理线程中进行更改。