可以将此代码添加到任何事件,例如侦听器,按钮等。JDialog将出现阻塞,并将一直保留到该过程完成为止。
final JDialog loading = new JDialog(parentComponent); JPanel p1 = new JPanel(new BorderLayout()); p1.add(new JLabel("请耐心等待..."), BorderLayout.CENTER); loading.setUndecorated(true); loading.getContentPane().add(p1); loading.pack(); loading.setLocationRelativeTo(parentComponent); loading.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); loading.setModal(true); SwingWorker<String, Void> worker = new SwingWorker<String, Void>() { @Override protected String doInBackground() throws InterruptedException /** Execute some operation */ } @Override protected void done() { loading.dispose(); } }; worker.execute(); //在这里进程线程启动 loading.setVisible(true); try { worker.get(); //此处父线程等待完成 } catch (Exception e1) { e1.printStackTrace(); }