swing中,如何实现定时启动呢?
点击"定时启动"按钮,触发如下操作
- timeButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if(!startButton.isEnabled()){
- GUIUtil23.warningDialog("[启动]按钮不可用");
- return;
- }
- String delayStr=delayTextField.getText();
- if(!DialogUtil.verifyTFEmpty(delayTextField, "定时启动时间")){
- return;
- }
- if(!ValueWidget.isInteger(delayStr)){
- GUIUtil23.warningDialog("定时启动的时间必须是数字[秒]");
- return;
- }
- final int secondDelay=Integer.parseInt(delayStr);
- new Thread(new Runnable() {
- @Override
- public void run() {
- if(!validate22()){
- return;
- }
- try {
- timeButton.setEnabled(false);
- delayTextField.setEditable(false);
- Thread.sleep(secondDelay*1000);//单位是毫秒
- startConnect();
- timeButton.setEnabled(true);
- delayTextField.setEditable(true);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
- });