我在做一个JAVA软件,希望在点击一个按钮后执行一段程序,在执行过程中出现一个进度条,应该怎么添加
2个回答
展开全部
package socket.fox.msgdeal;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class JProgressBar extends Frame implements Runnable {
int x, y; // 进度倏起始位置
int rectWidth, rectHeight, width, height, num; // 进度格倏宽,进度格高,进度倏宽,进度倏高,格子数量
int x0, d, n; // 进度,进度格之间的距离,进度n%
Image bufImg;
Graphics gBuf;
public JProgressBar() {
x = 40;
y = 50;
x0 = x;
rectWidth = 6; // 每个进度格的宽
rectHeight = 10;
d = rectWidth / 3;
num = 50; // 显示五十个进度格
width = (rectWidth + d) * num;
height = rectHeight + 4; // 进度格的高
init();
bufImg = this.createImage(getWidth(), getHeight());
gBuf = bufImg.getGraphics();
paintBack();
repaint();
}
public void init() {
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setSize(this.x * 2 + width, this.y * 2 + height);
this.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = getSize().width;
int h = getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
setLocation(x, y);
}
public static void main(String[] args) {
new Thread(new JProgressBar()).start();
}
public void paintBack() {
gBuf.drawRoundRect(x, y, width, height, 6, 6);
}
public void paintProgressRect() {
Color c = gBuf.getColor();
gBuf.setColor(new Color(46, 211, 49));
x0 = x;
for (int i = 0; i < n; i++) {
gBuf.fillRect(x0, y + 2, rectWidth, rectHeight);
x0 += rectWidth + d;
}
gBuf.setColor(c);
}
public void paint(Graphics g) {
g.drawImage(bufImg, 0, 0, this);
}
/**
* 设置进度倏进度
*
* @param n
* 进度百分比
*/
public void setPercent(int n) {
this.n = n;
}
public void run() {
boolean flag = true;
while (flag) {
try {
repaint();
Thread.sleep(500);
n++;
System.out.println(n);
if (n > num) {
n = num;
flag = false;
}
paintProgressRect();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class JProgressBar extends Frame implements Runnable {
int x, y; // 进度倏起始位置
int rectWidth, rectHeight, width, height, num; // 进度格倏宽,进度格高,进度倏宽,进度倏高,格子数量
int x0, d, n; // 进度,进度格之间的距离,进度n%
Image bufImg;
Graphics gBuf;
public JProgressBar() {
x = 40;
y = 50;
x0 = x;
rectWidth = 6; // 每个进度格的宽
rectHeight = 10;
d = rectWidth / 3;
num = 50; // 显示五十个进度格
width = (rectWidth + d) * num;
height = rectHeight + 4; // 进度格的高
init();
bufImg = this.createImage(getWidth(), getHeight());
gBuf = bufImg.getGraphics();
paintBack();
repaint();
}
public void init() {
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setSize(this.x * 2 + width, this.y * 2 + height);
this.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = getSize().width;
int h = getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
setLocation(x, y);
}
public static void main(String[] args) {
new Thread(new JProgressBar()).start();
}
public void paintBack() {
gBuf.drawRoundRect(x, y, width, height, 6, 6);
}
public void paintProgressRect() {
Color c = gBuf.getColor();
gBuf.setColor(new Color(46, 211, 49));
x0 = x;
for (int i = 0; i < n; i++) {
gBuf.fillRect(x0, y + 2, rectWidth, rectHeight);
x0 += rectWidth + d;
}
gBuf.setColor(c);
}
public void paint(Graphics g) {
g.drawImage(bufImg, 0, 0, this);
}
/**
* 设置进度倏进度
*
* @param n
* 进度百分比
*/
public void setPercent(int n) {
this.n = n;
}
public void run() {
boolean flag = true;
while (flag) {
try {
repaint();
Thread.sleep(500);
n++;
System.out.println(n);
if (n > num) {
n = num;
flag = false;
}
paintProgressRect();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询