图形界面的进度条

本文介绍了如何在图形用户界面中创建和使用进度条,展示了其在应用程序中显示任务进度的功能。通过示例代码,读者可以理解进度条的工作原理及其在不同编程环境下的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.awt.*;//引入相应库类
import javax.swing.*;

public class L10_1 extends JWindow {
    //进度条组件,因为在线程类L10_01要引用它,所以需要定义为类成员变量
    JProgressBar bar = new JProgressBar(1, 100);
    //构造函数
    public L10_1() {
        //背景图片,注意图片的位置
        JLabel bsckLmg = new JLabel(new ImageIcon("#"));
        bar.setStringPainted(true);
        bar.setString("系统正在初始化...");
        add(bar, "South");
        toFront();// 界面最前
        setSize(600, 400);
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((size.width - this.getWidth()) / 2,(size.height - this.getHeight()) / 2);
        add(bsckLmg);
        this.setVisible(true);//设置进度条显示文本
        LX10_1f_FlashThread ft = new LX10_1f_FlashThread(this);
        ft.start();
    }

    public class LX10_1f_FlashThread extends Thread {
        private L10_1 fw;
        public LX10_1f_FlashThread(L10_1 fw) {
            this.fw = fw;
            this.setName("LX10_1");
        }

        public void run(){
            System.out.println("当前线程是:"+LX10_1f_FlashThread.currentThread().getName());
            while(fw.bar.getValue()<100){
                fw.bar.setValue(fw.bar.getValue()+1);
                fw.bar.setString("系统正在初始化("+(fw.bar.getValue()+1)+"%)...");
                try{
                    Thread.sleep(200);//当前线程休眠0.2秒
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
            fw.dispose();//关闭窗体
        }
    }

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        //当前线程名称
        new L10_1();
    }
}

运行结果如下:
这里写图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值