Java製作進度條
阿新 • • 發佈:2018-11-10
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(); } }