0

この JOptionPane の「OK」ボタンを一定時間(たとえば5 秒)後にのみ表示したいと思います。(私の目的は、実際には、この他のスレッドの背後でいくつかのスレッドの作業を終了させることです)

JOptionPane jop2 = new JOptionPane();   
jop2.showMessageDialog(null, "Please wait 5s", "WAIT", JOptionPane.INFORMATION_MESSAGE);

その方法がまったくわかりません。この問題を解決するコードを教えていただけませんか? 事前にどうもありがとうございました!

4

3 に答える 3

4

を使用してこれを行う特定の方法はありませんJOptionPane。カスタム ダイアログを作成し、一定時間後に [OK] ボタンを表示する必要があります。Swing timerの 1 つのパスを使用できます。

ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        button.setVisible(true);
    }
};

Timer timer = new Timer(0, taskPerformer);
timer.setInitialDelay(5000);
timer.setRepeats(false);
timer.start();
于 2012-08-29T22:16:58.027 に答える
1

探しているのはとの組み合わせのようSwingWorkerですProgressMonitor。はSwingWorker、長時間実行されるタスク(5秒のタスク)を実行し、を使用して進行状況をユーザーに通知しますProgressMonitor。2つを連携させる方法を示す例は、次の場所にあります 。JavaProgressMonitorのキャンセルイベントの取得

もちろん、作業が完了したら続行ボタンを表示するアプローチを取りたいと確信している場合は、正しい方向から始めるための例を次に示します。を使用しSwingWorkerて、長時間実行されているバックグラウンドタスクが完了したことをダイアログに警告します。

import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import javax.swing.*;

public class TempProject extends Box{

    public TempProject(){
        super(BoxLayout.Y_AXIS);

        //Contains the content of the Alert Dialog
        Box info = Box.createVerticalBox();
        info.add(new Label("Please wait 5 seconds"));
        final JButton continueButton = new JButton("Continue");
        info.add(continueButton);

        //The alert to wait 5 seconds
        final JDialog d = new JDialog();
        d.setTitle("WAIT");
        d.setModalityType(ModalityType.APPLICATION_MODAL);
        d.setContentPane(info);
        d.pack();

        //The action of the "Continue Button"
        continueButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                d.dispose();
            }
        });
        continueButton.setVisible(false);

        //Thread That Does Work
        final SwingWorker sw = new SwingWorker<Integer, Integer>()
        {
            protected Integer doInBackground() throws Exception  {
                //Do long running thread work here
                int i = 0;
                while (i++ < 100) {
                    System.out.println(i);
                    Thread.sleep(100);
                }
                return null;
            }

            @Override
            protected void done(){
                // What to do when the long runnng thread is done
                continueButton.setVisible(true);
            }


        };


        //Button to start the long running task
        JButton button = new JButton("Click Me");
        button.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                sw.execute();
                d.setVisible(true);
            }});
        add(button);
    }


    public static void main(String args[])
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.setContentPane(new TempProject());
                frame.setPreferredSize(new Dimension(500, 400));
                frame.pack();
                frame.setVisible(true);
            }
        });
    }   


}
于 2012-08-30T04:27:03.023 に答える
-1

このようなものを使用して、コードを 5 秒間停止できます

       try {
            Thread.sleep(5000); // do nothing for 5000 miliseconds (5 seconds)
        } catch (InterruptedException e) {
            e.printStackTrace();
       }
于 2012-08-29T22:20:23.213 に答える