大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
在java里面有JProgressBar 这个类,你查下文档看看如何实现,我也没有使用过
专注于为中小企业提供成都做网站、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业东海免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
public class MyProgressBar extends Canvas {
private float scaleSize;
private float currentValue;
public MyProgressBar() {
this(100, 50);
}
public MyProgressBar(float scaleSize, float currentValue) {
this.scaleSize = scaleSize;
this.currentValue = currentValue;
this.setBackground(Color.lightGray);
this.setForeground(Color.magenta);
setSize(150, 25);
}
public float getCurrentValue() {
return currentValue;
}
public void setCurrentValue(float currentValue) {
this.currentValue = Math.max(0, currentValue);
if (this.scaleSize this.currentValue) {
this.currentValue = this.scaleSize;
}
}
public float getScaleSize() {
return scaleSize;
}
public void setScaleSize(float scaleSize) {
this.scaleSize = Math.max(1.0f, scaleSize);
if (this.scaleSize this.currentValue) {
this.scaleSize = this.currentValue;
}
}
public synchronized void paint(Graphics g) {
int w = getSize().width;
int h = getSize().height;
g.setColor(getBackground());
g.fillRect(1, 1, w - 2, h - 2);
g.fill3DRect(0, 0, w - 1, h - 1, true);
g.setColor(getForeground());
g.fillRect(3, 3, (int) (currentValue * (w - 6) / scaleSize), h - 6);
}
}
下面是程序执行入口点:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestMyProgressBar extends JFrame implements Runnable,
ActionListener {
private MyProgressBar bar;
private JButton btnStart;
static TestMyProgressBar tmpb;
public TestMyProgressBar() {
setSize(400, 300);
setLocation(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("演示自定义进度条");
setLayout(new FlowLayout());
btnStart = new JButton("开始");
this.add(btnStart);
btnStart.addActionListener(this);
bar = new MyProgressBar();
setVisible(true);
}
public static void main(String[] args) {
tmpb = new TestMyProgressBar();
}
@Override
public void run() {
for (int i = 1; i = 20; i++) {
int x = i * 5;
bar.setCurrentValue(x);
if (x 0 x 100) {
btnStart.setEnabled(false);
}
if (x == 100) {
btnStart.setEnabled(true);
}
try {
Thread.sleep(200);
add(bar);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("开始")) {
Thread t = new Thread(tmpb);
t.start();
}
}
}
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(); } }