Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JOptionPane:显示按钮";“好的”;经过一段时间_Java_Swing_Button_Wait_Joptionpane - Fatal编程技术网

Java JOptionPane:显示按钮";“好的”;经过一段时间

Java JOptionPane:显示按钮";“好的”;经过一段时间,java,swing,button,wait,joptionpane,Java,Swing,Button,Wait,Joptionpane,我只想在一定时间后显示此JopPane的按钮“OK”(确定)(例如5秒)。(我的目标实际上是让一些线程在另一个线程后面完成工作) 我根本不知道怎么做,你能给我提供一些代码来解决这个问题吗? 提前非常感谢 使用JOptionPane没有具体的方法来实现这一点。您必须创建一个自定义对话框,并在固定时间后显示“确定”按钮。你可以用一次传球 使用JOptionPane没有具体的方法来执行此操作。您必须创建一个自定义对话框,并在固定时间后显示“确定”按钮。你可以用一次传球 您可以使用类似的方法停止代码5秒

我只想在一定时间后显示此JopPane的按钮“OK”(确定)(例如5秒)。(我的目标实际上是让一些线程在另一个线程后面完成工作)

我根本不知道怎么做,你能给我提供一些代码来解决这个问题吗?
提前非常感谢

使用
JOptionPane
没有具体的方法来实现这一点。您必须创建一个自定义对话框,并在固定时间后显示“确定”按钮。你可以用一次传球


使用
JOptionPane
没有具体的方法来执行此操作。您必须创建一个自定义对话框,并在固定时间后显示“确定”按钮。你可以用一次传球


您可以使用类似的方法停止代码5秒钟

       try {
            Thread.sleep(5000); // do nothing for 5000 miliseconds (5 seconds)
        } catch (InterruptedException e) {
            e.printStackTrace();
       }

您可以使用类似的方法停止代码5秒钟

       try {
            Thread.sleep(5000); // do nothing for 5000 miliseconds (5 seconds)
        } catch (InterruptedException e) {
            e.printStackTrace();
       }

听起来您正在寻找的是
SwingWorker
ProgressMonitor
的组合。
SwingWorker
将执行长时间运行的任务(5秒任务),并使用
ProgressMonitor
通知用户任务进展情况。您可以在此处找到一个示例,说明如何使这两种方法协同工作:

当然,如果您确信希望在工作完成后立即显示“继续”按钮,那么下面的示例将帮助您朝着正确的方向开始。您将使用
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);
            }
        });
    }   


}
import java.awt.*;
导入java.awt.Dialog.ModalityType;
导入java.awt.event.*;
导入javax.swing.*;
公共类项目扩展框{
公共工程({
超级(Y_轴框布局);
//包含警报对话框的内容
Box info=Box.createVerticalBox();
信息添加(新标签(“请等待5秒”);
最终JButton continueButton=新JButton(“Continue”);
信息添加(继续按钮);
//警报将显示为等待5秒
final JDialog d=新JDialog();
d、 setTitle(“等待”);
d、 setModalityType(ModalityType.APPLICATION_MODAL);
d、 设置内容窗格(信息);
d、 包装();
//“继续”按钮的操作
continueButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
d、 处置();
}
});
continueButton.setVisible(false);
//行得通的线
最终SwingWorker sw=新SwingWorker()
{
受保护的整数doInBackground()引发异常{
//长时间运行的线程在这里工作吗
int i=0;
而(i++<100){
系统输出打印LN(i);
睡眠(100);
}
返回null;
}
@凌驾
受保护的void done(){
//完成长runnng线程时要做什么
continueButton.setVisible(true);
}
};
//按钮启动长时间运行的任务
JButton按钮=新JButton(“单击我”);
addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件arg0){
sw.execute();
d、 setVisible(真);
}});
添加(按钮);
}
公共静态void main(字符串参数[])
{
invokeLater(新的Runnable()
{
公开募捐
{
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(新的TempProject());
frame.setPreferredSize(新尺寸(500400));
frame.pack();
frame.setVisible(true);
}
});
}   
}

听起来你要找的是
SwingWorker
进度监视器的组合。
SwingWorker
将执行长时间运行的任务(5秒任务),并使用
ProgressMonitor
通知用户任务进展情况。您可以在此处找到一个示例,说明如何使这两种方法协同工作:

当然,如果您确信希望在工作完成后立即显示“继续”按钮,那么下面的示例将帮助您朝着正确的方向开始。您将使用
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);
            }
        });
    }   


}
import java.awt.*;
导入java.awt.Dialog.ModalityType;
导入java.awt.event.*;
导入javax.swing.*;
公共类项目扩展框{
公共工程({
超级(Y_轴框布局);
//包含警报对话框的内容
Box info=Box.createVerticalBox();
信息添加(新标签(“请等待5秒”);
最终JButton continueButton=新JButton(“Continue”);
信息添加(继续按钮);
//警报将显示为等待5秒
final JDialog d=新JDialog();
d、 setTitle(“等待”);
d、 setModalityType(ModalityType.APPLICATION_MODAL);
d、 设置内容窗格(信息);
d、 包装();
//“继续”按钮的操作
continueButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
d、 处置();
}
});
continueButton.setVisible(false);
//行得通的线
最终SwingWorker sw=新SwingWorker()
{
受保护的整数doInBackground()引发异常{
//长时间运行的线程在这里工作吗
int i=0;
W