Java关于JDialog的问题

te_ar te_ar 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
正在写一个爬虫程序,然后因为爬取数据需要时间,所以就想在爬取的时候增加一个进度条来表示正在爬取。于是创建了一个Probar类,当开始爬取数据时,new Probar()来显示进度条,表示正在爬取数据,同时显示消息日志来说明哪些数据爬到了,哪些没有,当爬取完数据之后,就关闭Probar。

主JFrame中相关代码:

/**** 准备开始爬取数据 ***/
Probar probar=new Probar(this);//这里this是作为parentComponent传入
new Thread(probar).start();
下面爬取数据,当爬取完数据时:
probar.setCanRun(false);

Probar类,表示进度条

public class Probar extends JDialog implements Runnable{
private boolean canRun=true;
private JLabel jl1=new JLabel("正在加载中,请稍后");
private JProgressBar pb=new JProgressBar(JProgressBar.HORIZONTAL);//进度条
private JLabel lbLog=new JLabel("消息日志");
private JTextArea taLog=new JTextArea();
private JScrollPane spLog=new JScrollPane(taLog);
public Probar(Component parentComponent){
super((JFrame) parentComponent);
this.setSize(800,650);
this.setLayout(null);

jl1.setSize(150,70);
jl1.setLocation(340,0);
jl1.setFont(new Font("黑体",Font.BOLD,24));
this.add(jl1);

pb.setLocation(100,70);
pb.setSize(600,15);
pb.setBorderPainted(true);
pb.setIndeterminate(true);
pb.setVisible(true);
this.add(pb);

lbLog.setLocation(47,75);
lbLog.setSize(120,70);
lbLog.setFont(new Font("黑体",Font.PLAIN,20));
this.add(lbLog);

taLog.setFont(new Font("微软雅黑",Font.PLAIN,18));
taLog.setEnabled(false);
spLog.setLocation(50,123);
spLog.setSize(680,450);
this.add(spLog);

this.setVisible(true);
GUIUtil.toCenter(this);

}

public void setCanRun(boolean canRun) {
this.canRun = canRun;
}

public void insertLog(String msg){
taLog.append(msg+"\n");
}


@Override
public void run() {
while(canRun) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
JOptionPane.showMessageDialog(this,"爬取完成");
this.dispose();
}

public static void main(String[] args) {
new Probar(null);
}

}


不知道为什么这个JDialog每次只有爬取完数据,也就是运行到JOptionPane.showMessageDialog(this,"爬取完成");才会显示出来,有什么办法可以让这个ProBar类可以立即显示出来?
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
te_ar
Java SE 62,620 成员 307,251 内容