当前位置: 首页 > news >正文

swing快速入门(三十九)进度对话框

🎁注释很详细,直接上代码

🧧新增内容
🧨1.模拟耗时操作
🧨2.使用计时器更新进度对话框

🎀源码:

package swing31_40;import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class swing_test_37 {//定义一个计时器//为什么定义在这里而不是在使用时定义?//因为在new时定义会导致timer.stop();这句报错未初始化Timer timer;//初始化方法public void init(){//模拟耗时操作final SimulatedActivity simulatedActivity = new SimulatedActivity(100);//启动耗时操作线程final Thread targetThread= new Thread(simulatedActivity);//启动线程targetThread.start();//创建进度条对话框ProgressMonitor dialog = new ProgressMonitor(null, "等待任务完成", "已完成:", 0, simulatedActivity.getAmount());//设置定时器(300毫秒执行一次)timer = new Timer(300, new ActionListener() {//定时器执行事件@Overridepublic void actionPerformed(ActionEvent e) {//设置对话框进度条dialog.setProgress(simulatedActivity.getCurrent());//如果进度条已完成,则停止计时器if (dialog.isCanceled()){timer.stop();//停止计时器targetThread.interrupt();//中断线程System.exit(0);//退出程序}}});timer.start();//启动计时器}public static void main(String[] args) {//启动程序new swing_test_37().init();}//定义一个线程任务,模拟耗时操作private class SimulatedActivity implements Runnable{//设置内存可见private volatile int current = 0;private int amount;//总量//构造方法public SimulatedActivity(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;}//重写run方法@Overridepublic void run() {//通过循环,不断的修改current的值,模拟任务完成量while(current<amount){try {//睡眠50毫秒Thread.sleep(500);} catch (InterruptedException e) {//中断异常处理e.printStackTrace();}current++;//增加当前值}}}}

🎗️效果演示:

在这里插入图片描述

http://www.lryc.cn/news/274274.html

相关文章:

  • Oracle-存储过程
  • L1-085:试试手气
  • nginx+keepalived实现七层负载
  • 机器人制作开源方案 | 智能盲道除雪小车
  • Mypy: 把静态类型检查带给Python
  • 【心得杂记】简单聊聊限制高速面阵相机性能的因素
  • 金蝶Apusic应用服务器 loadTree JNDI注入漏洞
  • 计算机毕业设计 基于SpringBoot的公司资产网站的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 如何获取时间戳?
  • Vue页面传值:Props属性与$emit事件的应用介绍
  • 【mars3d】new mars3d.layer.GeoJsonLayer(实现环状面应该怎么传data
  • Websocket实时更新商品信息
  • 数据结构第六弹---带头双向循环链表
  • 洛谷——P1347 排序(图论-拓扑排序)
  • JVM内存管理
  • 将 Python 和 Rust 融合在一起,为 pyQuil® 4.0 带来和谐
  • Spring Boot应用程序中VO的理解及使用
  • 华为交换机ETH-TRUNK链路聚合lacp模式与手工模式
  • 函数图像化
  • gnu工程的编译 - 以libiconv为例
  • 在 CentOS 7.8 上安装 Node.js
  • 【数据分析实战】冰雪大世界携程景区评价信息情感分析采集词云
  • BIND-DNS配置介绍
  • Python技巧
  • 几种常见的CSS三栏布局?介绍下粘性布局(sticky)?自适应布局?左边宽度固定,右边自适应?两种以上方式实现已知或者未知宽度的垂直水平居中?
  • 箭头函数 - JavaScript的新宠儿
  • 操作系统期末复习知识点
  • [英语学习][23][Word Power Made Easy]的精读与翻译优化
  • 吉林大学19、21级计算机学院《计算机网络》期末真题试题
  • python练习3【题解///考点列出///错题改正】