程序中的计时器每次都变短

Timer in program gets shorter every time

我正在尝试制作一个计时器,以便在 GIF 结束后用实心框架替换它,这样 GIF 就好像结束了。它有效,但由于某种原因,计时器变得越来越短。 (原因可能很明显,但我对计时器的理解很差)。

public class Gui extends JFrame {
int sum = 0;
int deaths = 0;
JTextField devs;
JButton a;
ImageIcon guncold;
ImageIcon gunactive;

public Gui() {
    super("Russian Roulette");
    setLayout(new BorderLayout());

    Font f = new Font("Western", 20, 20);

    guncold = new ImageIcon(getClass().getResource("guncold.gif"));
    gunactive = new ImageIcon(getClass().getResource("gunfiring.gif"));
    a = new JButton(guncold);
    a.setToolTipText("Click To Play");

    JPanel p = new JPanel();
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();

    final JTextField display = new JTextField("Click Gun To Play!", 12);

    devs = new JTextField("Deaths: " + deaths, 10);
    devs.setEditable(false);
    devs.setFont(f);
    devs.setBackground(Color.BLACK);
    devs.setForeground(Color.WHITE);

    p.add(a);
    p.setBackground(Color.BLACK);
    p1.add(devs);
    p1.setBackground(Color.BLACK);
    p2.add(display);
    p2.setBackground(Color.BLACK);

    display.setFont(f);
    display.setEditable(false);
    display.setBackground(Color.BLACK);
    display.setForeground(Color.WHITE);

    add(p, BorderLayout.CENTER);
    add(p1, BorderLayout.NORTH);
    add(p2, BorderLayout.SOUTH);

    a.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Random r = new Random();
            int rand = r.nextInt(5);
            switch (rand) {
            case 0:
                display.setText("Bang! You're dead.");
                deaths++;
                devs.setText("Deaths: " + deaths);
                break;
            case 1:
                display.setText("You're alive!");
                break;
            case 2:
                display.setText("You're alive!");
                break;
            case 3:
                display.setText("You're alive!");
                break;
            case 4:
                display.setText("You're alive!");
                break;
            case 5:
                display.setText("You're alive!");
                break;
            }
            Timer t = new Timer(1000, new TimerListener()); 
            if (rand == 0) {
                t.start();
                a.setIcon(gunactive);
                t.restart();    
                } else {
                a.setIcon(guncold);
            }

        }

    });
}

private class TimerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        a.setIcon(guncold);

    }
}

}

您正在为每个动作创造一个时间。创建一次计时器并在执行操作时重新启动它。

这一行:

Timer t = new Timer(1000, new TimerListener()); 

应该在 actionpPerformed 之外。

你可以这样使用:

private class TimerListener implements ActionListener {
    private JButton button;
    public MyListener(JButton button){
        this.button = button;
    }

    public void actionPerformed(ActionEvent e) {
        this.button.setIcon(guncold);
    }
}

现在您已经有了对要更改其图像的按钮的引用,请将引用传递给构造:

add(p, BorderLayout.CENTER);
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.SOUTH);
// HERE
Timer t = new Timer(1000, new TimerListener(a)); 
a.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        Random r = new Random();
        int rand = r.nextInt(5);
        switch (rand) {
        // ... code
        if (rand == 0) {
            t.restart();    
        } else {
            a.setIcon(guncold);
        }

    }

});