java Swing 进度条 多线程问题 附代码

ilovesnow 2009-10-19 10:12:04
目标:
当我点击主界面上的开始按钮,然后就会用大约1分钟来进行数据处理。但是在数据处理的时候,整个软件就像死了一样(其实没有),刷新还在继续,所以我想达到这样的效果:点击开始按钮,就弹出一个进度条在不停的动,表示程序正在运行,并没有死,然后进度条在持续更新,从1% 到 100%,当数据处理结束时,进度条消失,显示主界面,怎么实现呀!
望大家处处注意!谢谢!
问题:程序无法实现 【处理中】对话框进度条与 数据处理同步开始的功能。如何使他们同步进行?
即:数据处理 一开始,就让进度条对话框弹出,且 从0% 开始 向 100% 依次滚动?
附代码。(数据处理用 hread.sleep(new Random().nextInt(60000));来模拟)


...全文
2218 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ilovesnow 2009-10-19
  • 打赏
  • 举报
回复
//进度条 对话框,功能:弹出进度条,并且显示 TestExecutingDlg.java
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class TestExecutingDlg extends JDialog {

private static final long serialVersionUID = 1395849122103612654L;
private JPanel panel_top = null;
private JPanel panel_mid = null;
private JProgressBar progressBar = null;
private JTextField textField;
private Timer timer;
private int count;

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestExecutingDlg dialog = new TestExecutingDlg();
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
System.out.println("dialog.count*******00*: " + dialog.count);
dialog.timer.start();
dialog.setVisible(true);
Random random = new Random();
//Sleep for up to one second.
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ignore) {}
System.out.println("dialog.count*******02*: ");
dialog.setProgressBar(99);

} catch (Exception e) {
e.printStackTrace();
}

}
});
}
public TestExecutingDlg() {
super();
System.out.println("TestExecutingDlg() Start0: " );
setTitle("処理中。。。");
setBounds(100, 100, 400, 150);
setResizable(false);
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e) {
e.printStackTrace();
}

final JPanel panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel);

panel.add(getTopPanel());

panel.add(getMidPanel());
System.out.println("TestExecutingDlg() Start01: " );
timer = new Timer(100,new ActionListener(){
public void actionPerformed(final ActionEvent e) {
if (count >= 100-2){
timer.stop();
progressBar.setValue(count);
count = 0;
}else{
count++;
progressBar.setValue(count);
}
System.out.println(" actionPerformed(): count: " + count);
}
} );
timer.start();
System.out.println("TestExecutingDlg() Start02: " );
progressBar.setValue(10);
this.repaint();
}
public int getCount(){
return count;
}
protected JPanel getTopPanel(){
if(panel_top == null){
panel_top = new JPanel();
panel_top.setLayout(null);
panel_top.setBounds(10, 4, 376, 52);

textField = new JTextField();
textField.setBounds(10, 7, 356, 37);
textField.setText("程序运行中,请稍后...... ");
panel_top.add(textField);
}
return panel_top;
}

protected JPanel getMidPanel(){
if(panel_mid == null){
panel_mid = new JPanel();
panel_mid.setLayout(null);
panel_mid.setBounds(10, 60, 376, 52);
panel_mid.add(getProgressBar());
}
return panel_mid;
}
protected JProgressBar getProgressBar(){
if(progressBar == null){
progressBar = new JProgressBar(1,100);
progressBar.setBounds(10, 5, 354, 37);
progressBar.setStringPainted(true);
progressBar.setValue(0);
progressBar.setStringPainted(true);
}
return progressBar;
}
public int setProgressBar( int nValue ){
System.out.println("setProgressBar()nValue: " + nValue +"count: " + count);
if( nValue < 0 || nValue > 100) return -1;
Random random = new Random();
try {
Thread.sleep(random.nextInt(5000));
} catch (InterruptedException ignore) {}

progressBar.setValue(nValue);

if( nValue == 100 ) {
timer.start();
count = nValue-1;
//Sleep for up to one second.
try {
Thread.sleep(random.nextInt(3000));
} catch (InterruptedException ignore) {}
if(this.count >= 100){
this.setVisible(false);
this.dispose();
}
}
return 0;
}
}
ilovesnow 2009-10-19
  • 打赏
  • 举报
回复
//主对话框: TestMainDlg.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class TestMainDlg extends JDialog {
private static final long serialVersionUID = 6496389157619836424L;
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestMainDlg dialog = new TestMainDlg();
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestMainDlg() {
super();
setBounds(100, 100, 400, 300);
JButton btStart = new JButton("Start");
btStart.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
doStartButtonAction();
}
});
btStart.setBounds(112, 159, 82, 20);
final JPanel panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel);
panel.add(btStart);
}
protected void doStartButtonAction(){
TestExecutingDlg dlg = new TestExecutingDlg();
dlg.setProgressBar(5);
dlg.setVisible(true);
System.out.println("-----------Main--00: " );
// this is a Test like 1min SQL Operation
try {
Thread.sleep(new Random().nextInt(60000));
} catch (InterruptedException ignore) {}

System.out.println("----------Main--01: " );
}
}
ilovesnow 2009-10-19
  • 打赏
  • 举报
回复
up一下。

新问题:
有没有办法在主对话框(TestMainDlg.java )里,控制 子对话框的ActionPerform 事件?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧