您的位置:首页 > 其它

swing进度条

2013-01-24 16:02 197 查看
package com.hooypay.view;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;

public class LoadingFrame2 extends JFrame {
private JLabel statusLabel = new JLabel("");

public LoadingFrame2() {

this.setUndecorated(true); // 去掉窗口的装饰
this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);// 采用指定的窗口装饰风格
statusLabel.setIcon(new ImageIcon(ClassLoader
.getSystemResource("image/loding1.gif")));
this.getContentPane().add(statusLabel);

statusLabel.setHorizontalAlignment(JLabel.CENTER);
this.setBounds(700, 500, 30, 30);
// 任务栏图标
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.createImage(ClassLoader.getSystemResource("image/01.png"));
this.setIconImage(image);
//设置标题
this.setTitle("正在请求...");
}
public static void main(String[] args) {
new LoadingFrame2().setVisible(true);
}
}


1.上面是图片进度条

2.绘图进度条

package com.hooypay.test;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SwingThreadTest3 extends JFrame {
private static final long serialVersionUID = 1L;
private static final String STR = "Completed : ";
private JProgressBar progressBar = new JProgressBar();
private JTextField text = new JTextField(10);
private JButton start = new JButton("Start");
private JButton end = new JButton("End");
private boolean flag = false;
private int count = 0;

private GoThread t = null;

private Runnable run = null;//更新组件的线程
public SwingThreadTest3() {
this.setLayout(new FlowLayout());
add(progressBar);
text.setEditable(false);
add(text);
add(start);
add(end);
start.addActionListener(new Start());
end.addActionListener(new End());

run = new Runnable(){//实例化更新组件的线程
public void run() {
progressBar.setValue(count);
text.setText(STR + String.valueOf(count) + "%");
}
};
}
private void go() {
while (count < 100) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (flag) {
count++;
SwingUtilities.invokeLater(run);//将对象排到事件派发线程的队列中
}
}
}
private class Start implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = true;
if(t == null){
t = new GoThread();
t.start();
}
}
}

class GoThread extends Thread{
public void run() {
//do something
go();
}
}
private class End implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = false;
}
}
public static void main(String[] args) {
SwingThreadTest3 fg = new SwingThreadTest3();
fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fg.setSize(300, 100);
fg.setVisible(true);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: