① 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; @SuppressWarnings("serial") public class Demo extends JWindow implements Runnable { // 定義載入窗口大小 public static final int LOAD_WIDTH = 455; public static final int LOAD_HEIGHT = 295; // 獲乎虧含取屏幕窗口大小 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(new ImageIcon("images/background.jpg")); label.setBounds(0, 0, LOAD_WIDTH, LOAD_HEIGHT - 15); // 創建進度空唯條 progressbar = new JProgressBar(); // 顯示當前進度值信息 progressbar.setStringPainted(true); // 設置進度條邊框不顯示 progressbar.setBorderPainted(false); // 設置進度條的前景色 progressbar.setForeground(new Color(0, 210, 40)); // 設置進度條的背景色 progressbar.setBackground(new Color(188, 190, 194)); 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(); } @Override 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(); } }