java进度条代码(java导入进度条)

本文目录
- java导入进度条
- 用 java 怎么做进度条
- java中如何在拷贝文件时加入进度条,进度条跟文件拷贝进度一致
- 怎么将进度条从下到上慢慢填充颜色,用java编写,那个进度条我用的JProgressBar,求解
java导入进度条
很简单,因为数据读到集合所用的时间远远不如数据库的存储,因此只要计算插入数据库的进度即可。做法是:在读入数据库的时候 根据集合的大小生成一个最大进度为集合长度的进度条,每成功写入数据库一条,进度条 +1。
用 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 javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class JprogressBar extends JFrame implements Runnable {
public JprogressBar() {
initUI();
}
private void initUI() {
btCopy = new JButton("open file....");
JButton btCancel = new JButton("cancel");
JButton btSavePath = new JButton("save Path...");
copyFileProgressBar = new JProgressBar(0, 100);
copyFileProgressBar.setPreferredSize(new Dimension(450, 15));
copyFileProgressBar.setBackground(Color.GREEN);
copyFileProgressBar.setForeground(Color.PINK);
copyFileProgressBar.setStringPainted(true);
copyFileProgressBar.setVisible(false);
openFileDialog = new JFileChooser(".");
this.setLayout(new BorderLayout());
JPanel tmpPanel1 = new JPanel();
JPanel tmpPanel2 = new JPanel();
btCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread t = new Thread(JprogressBar.this);
t.start();
}
});
btSavePath.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
savePath = setSavePath();
}
});
btCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stop = true;
btCopy.setEnabled(true);
}
});
tmpPanel1.add(btCopy);
tmpPanel1.add(btSavePath);
tmpPanel1.add(btCancel);
tmpPanel2.add(copyFileProgressBar);
this.add(tmpPanel1, BorderLayout.NORTH);
this.add(tmpPanel2, BorderLayout.SOUTH);
this.setTitle("read and copy file");
this.setPreferredSize(new Dimension(450, 95));
this.pack();
Dimension cd = centerIt(this);
this.setLocation(cd.width, cd.height);
final Dimension des = this.getPreferredSize();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
stop = true;
System.exit(0);
}
public void windowStateChanged(WindowEvent e) {
System.out.println("ss");
if (e.paramString().equals("WINDOW_STATE_CHANGED")) {
System.out.println("ss");
JprogressBar.this.setSize(des);
}
}
});
this.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
// System.out.println("ss");
// JprogressBar.this.setSize(des);
}
});
}
/*
* start read file..
*/
public void run() {
stop = false;
int c = openFileDialog.showOpenDialog(this);
if (c == JFileChooser.APPROVE_OPTION) {
try {
File selectFile = openFileDialog.getSelectedFile();
if (selectFile.equals(savePath)) {
JOptionPane.showMessageDialog(this, " \t target file and source file can’t as the same !");
return;
}
if (savePath == null) {
JOptionPane.showMessageDialog(this, " \t please select a path to save file !");
return;
}
btCopy.setEnabled(false);
copyFileProgressBar.setVisible(true);
long size = selectFile.length();
copyFileProgressBar.setMaximum((int) size);
FileInputStream fin = new FileInputStream(selectFile);
FileOutputStream fout = new FileOutputStream(savePath);
byte;
int s;
int count = 0;
long startTime = System.currentTimeMillis();
while ((s = fin.read(buff)) 》 0 && !stop) {
count += s;
fout.write(buff, 0, s);
String str = "" + 100 * (count / (size + 0.01));
str = forMatString(str);
long endTime = System.currentTimeMillis();
String speedStr = getSpeed(count, startTime, endTime);
String remailTime = getRemailTime(count, size, startTime, endTime);
copyFileProgressBar.setString(" precent: " + str + " %" + " speed: " + speedStr + " " + " remail time : " + remailTime);
copyFileProgressBar.setValue(count);
}
fin.close();
fout.close();
if (!stop) {
JOptionPane.showMessageDialog(this, " \t copy file complete !");
}
stop = true;
savePath = null;
btCopy.setEnabled(true);
copyFileProgressBar.setValue(0);
copyFileProgressBar.setString("");
copyFileProgressBar.setVisible(false);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "err:\n" + ex.getMessage());
}
}
}
/*
*select save file path
*/
private File setSavePath() {
File path = null;
int c = openFileDialog.showSaveDialog(this);
if (c == JFileChooser.APPROVE_OPTION) {
path = openFileDialog.getSelectedFile();
}
return path;
}
/*
* make frame center
*/
private Dimension centerIt(Component c) {
Dimension size = c.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int sH = screenSize.height;
int sW = screenSize.width;
int cW = size.width;
int cH = size.height;
return new Dimension((sW - cW) / 2, (sH - cH) / 2);
}
/*
* show copy file speed
*/
private String getSpeed(long readByte, long startTime, long endTime) {
long speed;
if (endTime - startTime != 0) {
speed = (readByte / (endTime - startTime)) * 1000;
if (speed 》 1024 * 1024) {
return forMatString(speed / (1024 * 1024 + 0.1) + "") + " m/s";
} else if (speed 》 1024) {
return forMatString(speed / (1024 + 0.1) + "") + " k/s";
} else {
return speed + " b/s";
}
} else {
return "0 b/s";
}
}
/*
* format string
*/
private String forMatString(String str) {
String values;
int index = str.indexOf(".");
values = str.substring(0, index + 3);
return values;
}
/*
* get remail time
*/
private String getRemailTime(long readByte, long totalByte, long startTime, long endTime) {
long hour;
long minute;
long second;
String h;
String m;
String s;
try {
long speed = readByte / (endTime - startTime);
long time = ((totalByte - readByte) / speed) / 1000;
hour = time / 3600;
minute = time % 3600 / 60;
second = time % 3600 % 60;
h = hour + "";
m = minute + "";
s = second + "";
if (hour 《 10) {
m = "0" + minute;
}
if (minute 《 10) {
m = "0" + minute;
}
if (second 《 10) {
s = "0" + second;
}
return h + ":" + m + ":" + s;
} catch (Exception ex) {
return "00:00:00";
}
}
/*
* show frm
*/
public static void main(String args) {
JprogressBar frm = new JprogressBar();
frm.setVisible(true);
}
/*
*
*/
private JButton btCopy;
private JFileChooser openFileDialog;
private JProgressBar copyFileProgressBar;
private File savePath = null;
private boolean stop = false;
}
怎么将进度条从下到上慢慢填充颜色,用java编写,那个进度条我用的JProgressBar,求解
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class Floor extends JFrame
{
private static final long serialVersionUID = 1L;
private final int WIDTH = 500;
private final int HEIGHT = 500;
private final int L = 100;
private final int H = 100;
JProgressBar progressBar;
public Floor ( String title )
{
setTitle (title);
setLayout (null);
setSize (WIDTH, HEIGHT);
setResizable (false);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
private void addComponents ( JFrame frame )
{
JPanel panel = new JPanel ();
panel.setSize (WIDTH, HEIGHT);
panel.setLayout (null);
progressBar = new JProgressBar (JProgressBar.VERTICAL);
progressBar.setLocation (( WIDTH - L ) / 2 + L / 2 - L / 8, H);
progressBar.setSize (L / 4, HEIGHT - 2 * H - 28);
progressBar.setValue (0);
progressBar.setStringPainted (true);
JButton down = new JButton ("下楼");
down.setLocation (( WIDTH - L ) / 2, 0);
down.setSize (L, H);
JButton up = new JButton ("上楼");
up.setLocation (( WIDTH - L ) / 2, HEIGHT - H - 28);
up.setSize (L, H);
up.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
Task task = new Task ();
task.addPropertyChangeListener (new PropertyChangeListener ()
{
@Override
public void propertyChange ( PropertyChangeEvent evt )
{
if ("progress" == evt.getPropertyName ())
{
int progress = (Integer) evt.getNewValue ();
progressBar.setValue (progress);
}
}
});
task.execute ();
}
});
panel.add (down);
panel.add (progressBar);
panel.add (up);
frame.add (panel);
}
class Task extends SwingWorker《Void, Void》
{
@Override
public Void doInBackground ()
{
progressBar.setValue (0);
while (progressBar.getValue () 《 progressBar.getMaximum ())
{
try
{
Thread.sleep (50);
}
catch (InterruptedException ignore)
{}
progressBar.setValue (progressBar.getValue () + 1);
}
return null;
}
}
public static void main ( String args )
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override
public void run ()
{
Floor floor = new Floor ("电梯管理");
floor.addComponents (floor);
floor.setVisible (true);
}
});
}
}

更多文章:
wordpress和自己写(开发企业网站 是用现成的开源框架(如wordpress等)还是自己独立写 哪种比较好一些)
2025年12月25日 14:00
java代码提示(用myeclipse,在jsp中写java代码,需要导包时不提示,必须手动导包,怎样才能让它提示并且自动导包)
2026年4月9日 13:15
怎样安装tomcat服务(如何安装Apache Tomcat并使用Tomcat发布网站)
2025年12月21日 23:30
谁有点卡回收寄售平台的源码最好免费的?虚幻源码卡在加载页面中
2026年7月20日 05:00
函数指针类型别名(typedef 函数指针 比如typedef char*(*callback)(char*, int);)
2025年6月5日 09:00
css下载文件(下载网页时,如何将css文件中内容全部下载下来)
2026年7月25日 07:00
css给按钮加箭头(CSS实现带箭头方框怎么控制箭头位置!)
2026年1月1日 04:30
sql怎么读取excel更新(SQL如何用EXCEL导入一个临时表的相关字段,然后用临时表更新原有的表数据的字段内容)
2026年4月25日 09:30
require词根(require acquire enquire 这三个单词怎么区别,有共同的词根么)
2025年7月27日 09:00
wallpaper怎么设置鼠标特效(wallpaper控制鼠标灯光)
2026年5月6日 09:45











