gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:58:07 26 4
gpt4 key购买 nike

我试图在 Java 计时器达到 15 秒时仅显示一次文本消息时间到了,但它每 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

}
}

最佳答案

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

只需阅读api documentation,您就可以自己发现这一点.

关于java - 如何在计时器达到 15 秒时仅显示一次消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22454019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com