java Swing 进度条
2022/6/11 1:22:48
本文主要是介绍java Swing 进度条,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
java Swing 进度条
1 package io.guanghe; 2 3 import javax.swing.*; 4 5 /* 6 ProgressMonitor的用法与ProgressBar相似 , ProgressMonitor可以直接创建一个进度对话框 7 提供的构造方法: 8 public ProgressMonitor(Component parentComponent,Object message,String note, 9 int min,int max): 10 parentComponent: 对话框的父组件 11 message: 对话框的描述信息 12 note: 进度的提示信息 13 min: 进度条的最小值 14 max: 进度条的最大值 15 */ 16 public class Progress { 17 18 //定时器 19 Timer timer; 20 //任务线程 21 private static class SimulatorActivity implements Runnable { 22 //内存可见 23 private volatile int current = 0; 24 private int amount; 25 public SimulatorActivity(int amount) { 26 this.amount = amount; 27 } 28 public int getCurrent() { 29 return current; 30 } 31 public void setCurrent(int current) { 32 this.current = current; 33 } 34 public int getAmount() { 35 return amount; 36 } 37 public void setAmount(int amount) { 38 this.amount = amount; 39 } 40 @Override 41 public void run() { 42 //通过循环,不断的修改current的值,模拟任务我完成量 43 while (current < amount) { 44 try { 45 Thread.sleep(50); 46 } catch (InterruptedException e) { 47 e.printStackTrace(); 48 } 49 current++; 50 } 51 } 52 } 53 54 private Timer initTimer(ProgressMonitor monitor, SimulatorActivity simulatorActivity) { 55 //设置定时任务 56 return new Timer(100, e -> { 57 //读取当前任务量,修改进度 58 int current = simulatorActivity.getCurrent(); 59 monitor.setProgress(current); 60 //判断用户是否点击了取消按钮,停止定时任务,关闭对话框,退出程序 61 if (monitor.isCanceled()) { 62 timer.stop(); 63 monitor.close(); 64 System.exit(0); 65 } 66 if (current == 100) { 67 System.exit(0); 68 } 69 }); 70 } 71 72 public void init() { 73 //创建view 74 ProgressMonitor monitor = new ProgressMonitor(null, "等待任务完成", "已完成", 0, 100); 75 //创建任务model 76 SimulatorActivity simulaterActivity = new SimulatorActivity(100); 77 //执行任务model 78 new Thread(simulaterActivity).start(); 79 //开启监听controller 80 initTimer(monitor, simulaterActivity).start(); 81 } 82 83 public static void main(String[] args) { 84 new Progress().init(); 85 } 86 }
这篇关于java Swing 进度条的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南