如果启动流程,JFrame中的组件不会显示

时间:2018-10-31 10:23:53

标签: java swing exec event-dispatch-thread

我有以下代码(简体):

public class MyForm extends JFrame {
  private SplashForWaiting generating = new SplashForWaiting();
  private String texContent, batContent;

  // boilerplate code
  // ...

  private String insertFormValues(String template) {
    // modify String
  }

  private void buttonGenerateActionPerformed(ActionEvent evt) {
    generating.setVisible(true);
    Path texFile = Files.createTempFile(tmpDir,"test", ".tex");
    Path batFile = Files.createTempFile(tmpDir,"test", ".bat");
    OutputStreamWriter batWriter = new OutputStreamWriter(new FileOutputStream(batFile.toFile()), java.nio.charset.Charset.forName("IBM850"));
    batWriter.write(batContent.replace("¦filename",texFile.getFileName().toString()));
    batWriter.close();
    LinkedList<String> cmd = new LinkedList<>();
    cmd.add("explorer"); // cmd.exe is not directly accessible
    cmd.add(batFile.getFileName().toString());
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(texFile.toFile()), StandardCharsets.UTF_8);
    writer.write(insertFormValues(texContent));
    writer.close();
    ProcessBuilder pb = new ProcessBuilder(cmd);
    pb.directory(tmpDir.toFile());
    if (!debug || debugGenerate==true) {
      if (debug) System.out.print("Generating");
      Process p = pb.start();
      int exitval = p.waitFor();
      Path done = tmpDir.resolve("done.txt");
      do {
        if (debug) System.out.print(".");
        TimeUnit.SECONDS.sleep(2);
      } while (!Files.exists(done));
      Files.delete(done);
      generating.setVisible(false);
    }
  }
}

public class SplashForWaiting extends JFrame {
  private JLabel jLabel2 = new JLabel();
  private JPanel jPanel1 = new JPanel();
  public SplashForWaiting() {
    setTitle("Generating...");
    setAlwaysOnTop(true);
    setBounds(new Rectangle((Toolkit.getDefaultToolkit().getScreenSize().width-730)/2, (Toolkit.getDefaultToolkit().getScreenSize().height-312)/2, 730, 312));
    setIconImages(MyForm.icons);
    setMaximumSize(new Dimension(750, 312));
    setMinimumSize(new Dimension(750, 312));
    setResizable(false);
    //setUndecorated(true);
    getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.LINE_AXIS));

    jPanel1.setMaximumSize(new Dimension(750, 312));
    jPanel1.setMinimumSize(new Dimension(750, 312));
    jPanel1.setPreferredSize(new Dimension(750, 312));
    jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.LINE_AXIS));

    jLabel2.setIcon(new ImageIcon(getClass().getResource("/generating.jpg")));
    jLabel2.setMaximumSize(new Dimension(750, 312));
    jLabel2.setMinimumSize(new Dimension(750, 312));
    jLabel2.setPreferredSize(new Dimension(750, 312));
    jPanel1.add(jLabel2);

    getContentPane().add(jPanel1);

    pack();
  }              
}

可以看到(尽管有点模糊)是创建Windows批处理文件以及要处理的文件,然后执行该批处理文件。在批处理过程运行时,类似“启动”屏幕的信息应通知用户正在发生的事情。

问题如下:

  • 如果程序跳过运行批处理文件(debugGenerate否),则“ Splash”窗口将按预期显示,但显然不会生成任何内容。
  • 如果程序确实运行了批处理文件(debugGenerate为true),则仅显示“ Splash”窗口的装饰(如果未修饰),而不会显示“ Splash”窗口的内部JFrame完全没有如图所示,它看起来是透明的,但实际上更像是部分屏幕截图或区域受限的屏幕冻结。

0 个答案:

没有答案