如何在计时器达到15秒时显示一次消息

时间:2014-03-17 12:14:49

标签: java timer

我试图在Java Timer达到15秒时显示一条短信Time is up但是每15秒显示一次。我做错了什么?

代码:

package testovi;

import java.awt.EventQueue;

public class Test extends JFrame implements MouseListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private Timer time;
    private JLabel vrijeme = new JLabel("");
    private JLabel l1 = new JLabel("Blah1");
    private JLabel l2 = new JLabel("Blah2");
    private int brojac = 15;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test frame = new Test();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 333);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton print = new JButton("New button");

        print.setBounds(24, 192, 89, 23);
        contentPane.add(print);

        JLabel lblVrijeme = new JLabel("Vrijeme:");
        lblVrijeme.setBounds(182, 196, 46, 14);
        contentPane.add(lblVrijeme);

        vrijeme.setBounds(245, 191, 26, 24);
        contentPane.add(vrijeme);

        JButton btnNewButton = new JButton("Start");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                startajBrojac();
            }
        });
        btnNewButton.setBounds(22, 226, 105, 23);
        contentPane.add(btnNewButton);

        l1.setBounds(182, 230, 89, 19);
        contentPane.add(l1);

        l2.setBounds(182, 260, 89, 19);
        contentPane.add(l2);

        JLabel lblNewLabel_1 = new JLabel("New label");
        lblNewLabel_1.setIcon(new ImageIcon(Test.class.getResource("/testovi/slike/sat.png")));
        lblNewLabel_1.setBounds(225, 173, 60, 60);
        contentPane.add(lblNewLabel_1);

        l1.addMouseListener(this);
        l2.addMouseListener(this);

        // Akciski listener za tajmer
        time = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                vrijeme.setText(String.valueOf(brojac));
                brojac--;
                if (brojac == -1) {
                    time.stop();
                    JOptionPane.showMessageDialog(new Test(), "Time is  up");
                }
            }
        });
        time.start();

    }

    private void startajBrojac() {
        brojac = 15;
        time.start();

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == l1) {

            startajBrojac();
        } else if (e.getSource() == l2) {
            startajBrojac();

        }

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}

3 个答案:

答案 0 :(得分:1)

Timer timer = new Timer(15000, listener);
timer.setRepeats(false);
timer.start();

您可以通过阅读api documentation来找到自己。

答案 1 :(得分:0)

只需添加.setRepeats()

Timer timer = new Timer(15000, new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {

    }
});
timer.setRepeats(false);
timer.start();

答案 2 :(得分:0)

问题是当你显示"时间到了"对话框,您创建一个新的Test框架作为该对话框的父级。框架未显示,因为未调用setVisible,但是使用框架,会创建一个新的计时器线程,再次开始倒计时。您应该使用现有的框架。

更改此行

JOptionPane.showMessageDialog(new Test() "Time is  up");

到这个

JOptionPane.showMessageDialog(Test.this, "Time is  up");