有个需求,在swing的JFrame中有上一条、下一条按钮,点击上一条,关闭当前JFrame,显示JDialog(JDialog中是一个不确定进度条),当下一个JFrame加载好了,关闭JDialog,显示JFrame。
问题是,当下一个JFrame加载完成之前,JDialog处于假死状态,显示不出来不确定进度条,任何文字都不会显示,当JFrame成功显示出来的时候,JDialog才会显示。打不到我想要的效果
求大神赐教,以下是效果图
代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame{
private JFrame mf = new JFrame();
private JButton previousRecordButton = null;
private JButton nextRecordButton = null;
public MainFrame(){
Thread initThread = new Thread(new Runnable() {
@Override
public void run() {
init();
}
});
initThread.start();
(new WaitDialog(mf, initThread)).start();// 启动等待提示框线程
mf.setVisible(true);
}
public void init(){
JPanel mainPane = new JPanel(null);
previousRecordButton = new JButton("上一条");
previousRecordButton.setBounds(20, 350, 100, 25);
nextRecordButton = new JButton("下一条");
nextRecordButton.setBounds(240, 350, 100, 25);
previousRecordButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
close();
new DoOpen().open();
}
});
nextRecordButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
close();
new DoOpen().open();
}
});
mainPane.add(previousRecordButton);
mainPane.add(nextRecordButton);
mf.add(mainPane);
// this.setUndecorated(true); // 除去title
mf.setResizable(true);
mf.setSize(390, 500);
mf.setLocationRelativeTo(null); // 设置此窗口相对于指定组件的位置
mf.setAlwaysOnTop(true);
}
public void close(){
mf.dispose();
}
}
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
/**
@date 2016年8月16日 上午9:09:54
*/
public class WaitDialog extends Thread {
private JDialog dialog = null;
private JLabel lbStatus;
private JProgressBar progressBar;
private JFrame parentFrame = null;
private Thread currentThread = null;
public WaitDialog(JFrame parentFrame, Thread currentThread) {
this.parentFrame = parentFrame;
this.currentThread = currentThread;
initWaitDialogUI();// 初始化提示框
}
/**
构建显示进度条的对话框
*/
private void initWaitDialogUI() {
dialog = new JDialog(parentFrame);
JPanel mainPane = new JPanel(null);
progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
progressBar.setBounds(20, 20, 340, 15);
lbStatus = new JLabel("正在加载, 请稍后 ...");
lbStatus.setBounds(20, 60, 350, 25);
mainPane.add(progressBar);
mainPane.add(lbStatus);
dialog.add(mainPane);
// dialog.setUndecorated(true); // 除去title
dialog.setModal(false);
dialog.setResizable(true);
dialog.setSize(390, 150);
dialog.setLocationRelativeTo(null); // 设置此窗口相对于指定组件的位置
(new DisposeWaitDialog(currentThread,dialog)).start();// 启动关闭提示框线程
}
public void run(){
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
}
}
import javax.swing.JDialog;
public class DisposeWaitDialog extends Thread{
private Thread currentThread = null;
private JDialog clueDiag = null;
public DisposeWaitDialog(Thread currentThread, JDialog clueDiag) {
this.currentThread = currentThread;
this.clueDiag = clueDiag;
}
public void run() {
try {
currentThread.join();
} catch (InterruptedException e) {
System.out.println("Exception:" + e);
}
clueDiag.dispose();// 关闭提示框
}
}
public class DoOpen {
MainFrame mf = null;
public void open(){
mf = new MainFrame();
}
public static void main(String[] args) {
new DoOpen().open();
}
}
这个东西难道不应该把代码贴出来吗?