java Swing进度条体现打开一个界面的进度

网上提到进度条,总是提到多线程,然后把多线程加到程序里面,更有甚者不管什么样的功能都把定时器加上,把程序写得那叫一个晦涩难懂。本来在swing里面的多线程就是不安全的,不太清楚进度条使用的人看着多线程在进度条上的使用,多少感觉很困难。其实实现一个进度条是极其的简单。下面来个例子你就会明白:
第一:一个进度条的代码:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;

public class ProgressPane extends JPanel {

public JLabel label;
public JProgressBar progressBar;

/**
* Create the panel
*/
public ProgressPane() {
super();
setLayout(new GridBagLayout());
setSize(394, 33);

progressBar = new JProgressBar(0,100);
progressBar.setStringPainted(true);
final GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.weightx = 1.0;
gridBagConstraints.ipady = 10;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.ipadx = 75;
gridBagConstraints.insets = new Insets(4, 0, 0, 0);
add(progressBar, gridBagConstraints);


label = new JLabel();
final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
gridBagConstraints_1.gridx = 1;
gridBagConstraints_1.gridy = 0;
gridBagConstraints_1.ipadx = 54;
gridBagConstraints_1.insets = new Insets(0, 12, 10, 0);
add(label, gridBagConstraints_1);
label.setText("正常状态");
//
}

}
第二:要打开的一个界面测试类
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JProgressBar;

import javax.swing.JDialog;
import javax.swing.JPanel;

public class TTTT extends JDialog {




/**
* Create the dialog
*/
public TTTT(JProgressBar progressBar) {
super();
setTitle("天津英诺");
setBounds(100, 100, 500, 375);
for(int i=0;i<100;i++){
progressBar.setValue(i);
progressBar.setString("正在打开--天津英诺界面。。。 "+i+"%");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);

final JCheckBox checkBox = new JCheckBox();
checkBox.setText("New JCheckBox");
panel.add(checkBox);

final JButton button = new JButton();
button.setText("New JButton");
panel.add(button);
//
}

}
可以看到上面,没有什么多线程的东西,就在打开的界面里面有一个对传过来的进度条对象进行设置它的值,为了显示需要,每次更新值都暂停200毫秒即:Thread.sleep(200);
第三:一个主程序测试程序:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

import java.lang.reflect.InvocationTargetException;
import java.util.Timer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

//import .ProgressBarTask;

public class ProgressFrame extends JFrame {

/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
ProgressFrame frame = new ProgressFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create the frame
*/
private ProgressPane progresspane;
public ProgressFrame() {
super();
setTitle("进度条事例");
setBounds(100, 100, 500, 375);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
progresspane = new ProgressPane();

final JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
getContentPane().add(panel, BorderLayout.CENTER);

final JButton button = new JButton();

button.setText("点击");
final GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridy = 0;
gridBagConstraints.gridx = 0;
panel.add(button, gridBagConstraints);
getContentPane().add(progresspane, BorderLayout.SOUTH);

//事件监听
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
Thread thread = new Thread(new Runnable(){//进度条的多线程就在这一点

public void run() {
// 点击按钮要打开的界面
TTTT ts = new TTTT(progresspane.progressBar);
ts.pack();
ts.setVisible(true);
progresspane.progressBar.setValue(0);//界面打开之后要把进度条归到0

}

});
thread.start();//启动这个线程 就足够了
}
});
}

}
进度条的使用其实很简单吧。主要是因为swing界面是单线程的,你只需另起一个线程,就可以使用进度条了,要不然的话,单线程上更新进度条的值,进度条是没反应的。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值