0

我什至不知道如何解决这个问题,但在做了一些阅读和很多尝试(失败)之后,我决定向社区寻求帮助。我有表单 A 打开并要求用户输入延迟表单 B 打开的时间。目前我正在sleep()这样做,但现在我想插入另一个对话框,以允许用户在计时器用完之前中断计时器并调出表格 B。我相信正确的方法是使用wait()并且notify()我似乎无法围绕生产者和消费者模型的众多示例。任何帮助表示赞赏。

4

2 回答 2

1

一个完美的工作javax.swing.Timer。有关详细信息,请参阅如何使用摆动定时器。这是一个示例,可引导您朝着正确的方向前进。

import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.*;

public class TimerDemo extends JFrame implements ActionListener {

    private Timer timer;
    private JButton jbDoSomethingDelayed;
    private JButton jbDoItImmediately;

    public TimerDemo() {        
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Timer demo");

        jbDoSomethingDelayed = new JButton("Do something with a delay");
        jbDoItImmediately = new JButton("Do it. Do it NOW!");

        add(jbDoSomethingDelayed);
        add(jbDoItImmediately);

        jbDoItImmediately.setEnabled(false);

        timer = new Timer(0, this); // we override delay later
        timer.setRepeats(false); // we don't want it firing repeatedly

        jbDoSomethingDelayed.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String msg = "Enter delay and confirm dialog";
                JSpinner spinner = new JSpinner(new SpinnerNumberModel(5, 1, 10, 1));
                Object[] content = new Object[] {msg, spinner};
                int showConfirmDialog = JOptionPane.showConfirmDialog(TimerDemo.this, content, "Choose", JOptionPane.OK_CANCEL_OPTION);
                if (showConfirmDialog == JOptionPane.OK_OPTION) {
                    // the important part
                    timer.setInitialDelay(((Integer)spinner.getValue()) * 1000);                    
                    jbDoSomethingDelayed.setEnabled(false);
                    jbDoItImmediately.setEnabled(true);
                    timer.start();
                }
            }
        });

        jbDoItImmediately.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                timer.stop();
                onTimerTimeout();
            }
        });

        pack();
        setLocationRelativeTo(null);
    }

    public void actionPerformed(ActionEvent e) {
        // called by timer on EDT, no worries here
        onTimerTimeout();
    }

    private void onTimerTimeout() {   
        jbDoSomethingDelayed.setEnabled(true);
        jbDoItImmediately.setEnabled(false);
        JOptionPane.showConfirmDialog(this, "You've done it now. No, really...", "It is done", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);        
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                TimerDemo demo = new TimerDemo();
                demo.setVisible(true);
            }
        });
    }
}
于 2013-06-12T13:28:49.220 回答
0

最简单的方法是做这样的事情

Thread a = new Thread(new Runnable(){
   public void run(){
      //do whatever display
      try{
        Thread.sleep(timeToShowBform);
      }
      catch(InterruptedException ex){
         //interrupted.
      }finally{
      //show form B
      SwingUtilities.invokeLater(...)

   }
});

class BRunnable implements Runnable{
public void run(){
 //if clicked, then this runnable is called.
 a.interrupt();
}
}

a假设线程在 被阻塞sleep,然后在调用a.interrupt()它时被唤醒a

于 2013-06-12T06:16:10.563 回答