package test.object;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class TextMain {
public void init() {
JFrame jFrame = new JFrame("test JProgress");
JCheckBox jCheckBox = new JCheckBox("不确定进度");
JCheckBox jCheckBox2 = new JCheckBox("不绘制边框");
JProgressBar jProgressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
// 处理复选框的点击行为
jCheckBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取一下复选框有没有选中
boolean selected = jCheckBox.isSelected();
// 设置当前进度条不确定进度
jProgressBar.setIndeterminate(selected);
// 进度百分比是否展示
jProgressBar.setStringPainted(!selected);
}
});
jCheckBox2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
boolean selected = jCheckBox2.isSelected();
jProgressBar.setBorderPainted(selected);
}
});
jProgressBar.setStringPainted(true);
jProgressBar.setBorderPainted(true);
Box createVerticalBox = Box.createVerticalBox();
createVerticalBox.add(jCheckBox);
createVerticalBox.add(jCheckBox2);
// jFrame.add(createVerticalBox, BorderLayout.CENTER);
// jFrame.add(jProgressBar, BorderLayout.EAST);
jFrame.setLayout(new FlowLayout());
jFrame.add(createVerticalBox);
jFrame.add(jProgressBar);
jFrame.pack();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
// // 循环修改进度条的进度
for (int i = 0; i < 100; i++) {
jProgressBar.setValue(i);
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public static void main(String[] age) {
new TextMain().init();
}
}
image.png
在子线程进行完成进度
package test.object;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TextMain {
public void init() {
JFrame jFrame = new JFrame("test JProgress");
JCheckBox jCheckBox = new JCheckBox("不确定进度");
JCheckBox jCheckBox2 = new JCheckBox("不绘制边框");
JProgressBar jProgressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
// 处理复选框的点击行为
jCheckBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取一下复选框有没有选中
boolean selected = jCheckBox.isSelected();
// 设置当前进度条不确定进度
jProgressBar.setIndeterminate(selected);
// 进度百分比是否展示
jProgressBar.setStringPainted(!selected);
}
});
jCheckBox2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
boolean selected = jCheckBox2.isSelected();
jProgressBar.setBorderPainted(selected);
}
});
jProgressBar.setStringPainted(true);
jProgressBar.setBorderPainted(true);
Box createVerticalBox = Box.createVerticalBox();
createVerticalBox.add(jCheckBox);
createVerticalBox.add(jCheckBox2);
// jFrame.add(createVerticalBox, BorderLayout.CENTER);
// jFrame.add(jProgressBar, BorderLayout.EAST);
jFrame.setLayout(new FlowLayout());
jFrame.add(createVerticalBox);
jFrame.add(jProgressBar);
jFrame.pack();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
// // 开启子线程模拟耗时操作
SimLaterActivity simLaterActivity = new SimLaterActivity(jProgressBar.getMaximum());
new Thread(simLaterActivity).start();
// 设置一个定时任务
Timer timer = new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 当前完成量设置给进度条
int current = simLaterActivity.getCurrent();
jProgressBar.setValue(current);
}
});
timer.start();
// 给进度条监听任务的变化
jProgressBar.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int value = jProgressBar.getValue();
if (value == simLaterActivity.getAmout()) {
timer.stop();
}
}
});
}
private class SimLaterActivity implements Runnable {
// 当前任务的完成量
// 任务总量
private int amout;
// volatile设置后该线程修改后其他线程可以立即查看到该结果
private volatile int current;
public SimLaterActivity(int amout) {
this.amout = amout;
}
public int getAmout() {
return amout;
}
public void setAmout(int amout) {
this.amout = amout;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
@Override
public void run() {
// 模拟耗时操作
// 子线程的文件
while (current < amout) {
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
current++;
}
}
}
public static void main(String[] age) {
new TextMain().init();
}
}
使用BoundedRangeModel进行实现
package test.object;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoundedRangeModel;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TextMain {
public void init() {
JFrame jFrame = new JFrame("test JProgress");
JCheckBox jCheckBox = new JCheckBox("不确定进度");
JCheckBox jCheckBox2 = new JCheckBox("不绘制边框");
JProgressBar jProgressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
// 获取进度条数据的模型对象
BoundedRangeModel model = jProgressBar.getModel();
// 处理复选框的点击行为
jCheckBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取一下复选框有没有选中
boolean selected = jCheckBox.isSelected();
// 设置当前进度条不确定进度
jProgressBar.setIndeterminate(selected);
// 进度百分比是否展示
jProgressBar.setStringPainted(!selected);
}
});
jCheckBox2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
boolean selected = jCheckBox2.isSelected();
jProgressBar.setBorderPainted(selected);
}
});
jProgressBar.setStringPainted(true);
jProgressBar.setBorderPainted(true);
Box createVerticalBox = Box.createVerticalBox();
createVerticalBox.add(jCheckBox);
createVerticalBox.add(jCheckBox2);
// jFrame.add(createVerticalBox, BorderLayout.CENTER);
// jFrame.add(jProgressBar, BorderLayout.EAST);
jFrame.setLayout(new FlowLayout());
jFrame.add(createVerticalBox);
jFrame.add(jProgressBar);
jFrame.pack();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
// // 开启子线程模拟耗时操作
SimLaterActivity simLaterActivity = new SimLaterActivity(jProgressBar.getMaximum());
new Thread(simLaterActivity).start();
// 设置一个定时任务
Timer timer = new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 当前完成量设置给进度条
int current = simLaterActivity.getCurrent();
// jProgressBar.setValue(current);
model.setValue(current);
}
});
timer.start();
// 给进度条监听任务的变化
model.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int value = model.getValue();
if (value == simLaterActivity.getAmout()) {
timer.stop();
}
}
});
}
private class SimLaterActivity implements Runnable {
// 当前任务的完成量
// 任务总量
private int amout;
// volatile设置后该线程修改后其他线程可以立即查看到该结果
private volatile int current;
public SimLaterActivity(int amout) {
this.amout = amout;
}
public int getAmout() {
return amout;
}
public void setAmout(int amout) {
this.amout = amout;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
@Override
public void run() {
// 模拟耗时操作
// 子线程的文件
while (current < amout) {
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
current++;
}
}
}
public static void main(String[] age) {
new TextMain().init();
}
}
ProgressMonitor进度条对话框
package com.main;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JToolBarDemo {
public static void main(String[] age){
new JToolBarDemo().init();
}
Timer timer;
public void init(){
//创建进度对话框,弹窗类的组件可以不用父组件
ProgressMonitor monitor=new ProgressMonitor(null,"等待进度完成","已完成",0,100);
SimulaterActivity simulaterActivity=new SimulaterActivity(100);
new Thread(simulaterActivity).start();
//设置定时任务
timer= new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//读取当前任务量,修改进度
int current = simulaterActivity.getCurrent();
monitor.setProgress(current);
//判断是否点击了取消按钮,如果点击了需要停止定时任务并关闭对话框,推出程序
if(monitor.isCanceled()){
timer.stop();
monitor.close();
System.exit(0);
}
}
});
timer.start();
}
private class SimulaterActivity implements Runnable{
private volatile int current=0;
private int amount=0;
public SimulaterActivity(int amount){
this.amount=amount;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Override
public void run() {
while(current<amount){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
current++;
}
}
}
}
网友评论