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

时间:2015-06-27 01:19:14

标签: java swing timer

我试图制作一个计时器,所以在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);

    }
}

}

1 个答案:

答案 0 :(得分:3)

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

这一行:

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);
        }

    }

});