我正在使用java swing和JButton。當我按下按鈕時,我想要一個定時器啓動並計算在三秒鐘內按下按鈕的次數。我正在嘗試使用java.util.timer計時器。這是正確的路嗎?如何啓動計時器並在三秒後停止計時?如何在java上以一定間隔停止計時器
import javax.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.util.Timer;
import java.awt.*;
import java.awt.event.*;
public class button{
public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(100, 75);
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton (" ");
button.setSize(300, 150);
panel.add(button);
button.addActionListener(new Action());
frame.setAlwaysOnTop(true);
frame.setBounds(1225, 675, 100, 75);
}
static class Action implements ActionListener{
public void actionPerformed (ActionEvent e){
}
}
public static int timer(){
Timer timer = new Timer();
return 7;
}
}
歡迎來到StackOverflow!你可以分享到目前爲止的代碼並解釋具體問題嗎?我們希望首先看到您的具體嘗試,以便我們能夠充分幫助您。 –
請問我的問題1.爲什麼util.Timer爲Swing,Swing有自己的Timer,2.某些interal約爲a)個數,b)時間 – mKorbel
1.我不知道swing有它自己的計時器。 2.我不明白你的第二個想法 – user2947797