1. 程式人生 > >Java製作進度條

Java製作進度條

import java.awt.Color;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JWindow;


public class Demo extends JWindow implements Runnable {

    // 視窗大小
    public static final int LOAD_WIDTH 
= 200; public static final int LOAD_HEIGHT = 40; // 幕視窗大小 public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; // 義進條組 public JProgressBar progressbar; // 義標籤組
public JLabel label; // 造函 public Demo() { // 標籤 label = new JLabel("據加中,稍後!!!!"); label.setBounds(0, 0,LOAD_WIDTH , LOAD_HEIGHT - 15); // progressbar = new JProgressBar(); // 資訊 progressbar.setStringPainted(true); // 條邊框不 progressbar
.setBorderPainted(false); // 的前景色 progressbar.setForeground(new Color(0, 200, 30)); // 的背景色 progressbar.setBackground(new Color(168, 200, 198)); progressbar.setBounds(0, LOAD_HEIGHT - 15, LOAD_WIDTH, 15); // 新增 this.add(label); this.add(progressbar); // 置佈局 this.setLayout(null); // 置視窗初始位置 this.setLocation((WIDTH - LOAD_WIDTH) / 2, (HEIGHT - LOAD_HEIGHT) / 2); // 置視窗大小 this.setSize(LOAD_WIDTH, LOAD_HEIGHT); // 置視窗 this.setVisible(true); } public static void main(String[] args) { Demo t = new Demo(); new Thread(t).start(); } public void run() { for (int i = 0; i < 100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } progressbar.setValue(i); } JOptionPane.showMessageDialog(this, "完成!!!"); this.dispose(); } }