java代码写个进程条
总结:运用JProgressBar类。还有线程相关
package com.v; import java.awt.image.*; import java.awt.Color; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.FlowLayout; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.JWindow; import java.awt.event.*; public class Demo extends JWindow implements Runnable { // 定义加载窗口大小 public static final int load_width = 455; public static final int load_high = 295; public static final int width = Toolkit.getDefaultToolkit().getScreenSize().width; public static final int high = Toolkit.getDefaultToolkit().getScreenSize().height; // 定义进度条组件@Override public JProgressBar progressbar; // 定义标签组件 public JLabel label; // 构造方法 public Demo() { // /创建标签并在表标签上放一个图片 label = new JLabel(new ImageIcon("images/background.jpg"));// // label=new JLabel();//这里理解关键 label.setBounds(0, 0, load_width, load_high - 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_high - 15, load_width, 15);// 这一步我漏掉了导致进程条不显示 // 添加标签 this.add(label); // 添加进程条 this.add(progressbar); // 设置窗口的大小 this.setSize(load_width, load_high); // 设置窗口的初始位置 this.setLocation((high - load_high) / 2, (width - load_width) / 2); // 设置窗体的布局 this.setLayout(null); // this.setLayout(new FlowLayout()); // 设置窗口在屏幕中央显示 this.setLocationRelativeTo(null); // 设置窗口显示 this.setVisible(true); } public static void main(String[] args) { Demo demo = new Demo();// 创建对象 new Thread(demo).start(); } public void run() { for (int i = 0; i <= 100; i++) { try { Thread.sleep(100);// 线程 } catch (InterruptedException I) { I.printStackTrace(); } progressbar.setValue(i); } JOptionPane.showMessageDialog(this, "OK任务已完成!"); this.dispose(); } }