1
我使用擺動Timer
使webNotification
(自定義JFrame
)出現在特定時間。我希望用戶可以選擇單擊「隱藏」按鈕來關閉通知,並在一小時後恢復。我怎樣才能做到這一點?使用擺動計時器臨時隱藏通知
我使用擺動Timer
使webNotification
(自定義JFrame
)出現在特定時間。我希望用戶可以選擇單擊「隱藏」按鈕來關閉通知,並在一小時後恢復。我怎樣才能做到這一點?使用擺動計時器臨時隱藏通知
javax.swing.Timer
有一個初始延遲;只需將其設置爲60 * 60 * 1000
。在調用start()
後,您的actionPerformed()
將被調用一小時。
附錄:下面是一個按鈕的例子,它隱藏了它在特定時間段內的封閉窗口。
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
/** @see http://stackoverflow.com/questions/4373493 */
public class TimerFrame extends JFrame {
private void display() {
this.setTitle("TimerFrame");
this.setLayout(new GridLayout(0, 1));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new TimerButton("Back in a second", 1000));
this.add(new TimerButton("Back in a minute", 60 * 1000));
this.add(new TimerButton("Back in an hour", 60 * 60 * 1000));
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
/** A button that hides it's enclosing Window for delay ms. */
private class TimerButton extends JButton {
private final Timer timer;
public TimerButton(String text, int delay) {
super(text);
this.addActionListener(new StartListener());
timer = new Timer(delay, new StopListener());
}
private class StartListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TimerFrame.this.setVisible(false);
timer.start();
}
}
private class StopListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
timer.stop();
TimerFrame.this.setVisible(true);
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TimerFrame().display();
}
});
}
}
但是,如果時間延遲不在24小時內,但在一週內或超過它的情況?我們是否應該把時間計算到那一天,並將其延遲?還是有其他方法? – bsm 2011-04-19 19:39:49