Ⅰ 用 java 怎麼做進度條
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();
}
}
}
Ⅱ 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(); } } 效果圖:
Ⅲ java導入進度條
很簡單,因為數據讀到集合所用的時間遠遠不如資料庫的存儲,因此只要計算插入資料庫的進度即可。做法是:在讀入資料庫的時候 根據集合的大小生成一個最大進度為集合長度的進度條,每成功寫入資料庫一條,進度條 +1。
Ⅳ java實現進度條
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class Test extends JFrame {
public Test(){
super();
setSize(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
JProgressBar progressBar = new JProgressBar();
getContentPane().add(progressBar,BorderLayout.NORTH);
progressBar.setStringPainted(true);
for (int i = 0;i < 50;i++){
progressBar.setValue(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Test();
}
}
for循環裡面加了個延時函數。