Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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 我想在swing中做一些后台工作_Java_Swing_Event Dispatch Thread - Fatal编程技术网

Java 我想在swing中做一些后台工作

Java 我想在swing中做一些后台工作,java,swing,event-dispatch-thread,Java,Swing,Event Dispatch Thread,我使用swing定时器在swing应用程序中加载不同的pdf文件 但是我遇到了一个问题,当我执行一个程序时,屏幕会保持空白几秒钟,比如4到5秒钟,然后pdf文件就会呈现出来,所以在这段时间里,我想显示一条消息,比如请稍候。这是我的示例代码 if (type[i].equalsIgnoreCase("PDF")) { int k = i; pdfTimer = new Timer(0, (ActionEvent e) ->

我使用swing定时器在swing应用程序中加载不同的pdf文件 但是我遇到了一个问题,当我执行一个程序时,屏幕会保持空白几秒钟,比如4到5秒钟,然后pdf文件就会呈现出来,所以在这段时间里,我想显示一条消息,比如请稍候。这是我的示例代码

          if (type[i].equalsIgnoreCase("PDF")) {
            int k = i;
            pdfTimer = new Timer(0, (ActionEvent e) -> {
                renderPDF(k);

            });
            pdfTimer.setDelay(1000*2);
            pdfTimer.start();

可以显示进度条并在第二个线程上计算渲染

JLabel lblWait = new JLabel("Please wait...");
    lblWait .setBounds(116, 26, 113, 14);
    contentPanel.add(lblWait );

    final JProgressBar progressBar = new JProgressBar();
    progressBar.setStringPainted(true);
    progressBar.setBounds(72, 66, 187, 16);
    contentPanel.add(progressBar);
    {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            final JButton btFin = new JButton("Cancel");
            btFin.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    dispose();
                }
            });

            buttonPane.add(btFin);
        }
    }
     Thread t = new Thread() {
            public void run() {
              //You launch the thread with your progress bar as an argument
                maStructure.setaAfficher(new MgtSimulation(aAfficher, nombreSimulations).jouerSimulations(progressBar));
                maStructure.actualiserAffichage();
                dispose();
            }
          };
          t.start();
}
并在方法中更改进度条值

public  BeanAffichage jouerSimulations(JProgressBar progressBar){
    //Variables

     for (int i = 0; i < nombreSimulations; i++) {           
        //Computing

         progressBar.setValue(Whatever you want);
    }



    return aAfficher;
}
公共BeanAffichage期刊模拟(JProgressBar progressBar){
//变数
对于(inti=0;i<0;i++){
//计算
progressBar.setValue(任何您想要的);
}
返回阿菲舍尔;
}
在后台线程(
doInBackground
)方法上运行渲染。这样,您的GUI将保持响应性。通过
done
方法,您可以通知用户渲染已完成。请记住不要从
doInBackground
方法更新任何Swing
GUI
,因为它在EDT之外运行

另外,Swing Timer用于重复性任务。

只是SwingWorker的一个替代品

默认情况下显示一些消息,如加载。。。并创建一个
线程
在后台运行,加载PDF并更新窗口

class BackgroundThread implements Runnable {

   @Override
   public void run() {

      // the Swing call below must be queued onto the Swing event thread
      SwingUtilities.invokeLater(new Runnable(){
         @Override
         public void run() {
            // OK To make Swing method calls here
            loadFile(args..);
            repaint();//Or something similar that fits your purpose
         }
      });
   }
}

使用
SwingWorker
。请参阅了解更多详细信息。这是一个可怕的想法。只是说说而已。我总是做那种事。为什么这是一个可怕的想法?因为swing不是线程安全的。在您的示例中,您正在从一些外部线程更新其组件。你认为SwingWorker存在的原因是什么?我还是个学生,对SwingWorker一无所知。感谢您的建议,我还想做重复性的任务来呈现pdf的每一页,这只是第一次屏幕保持空白