Swing_JProgressBar

本文提供了一个使用Java Swing实现的示例程序,该程序展示了如何在Java应用程序中加载图片并显示加载进度。通过定时器更新进度条的值来模拟加载过程,并在加载完成后关闭窗口。

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

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Rectangle;


public class loadPicture extends JFrame {
    JLabel lbl_picture = new JLabel();
    JProgressBar pb_rute = new JProgressBar(1,200);
    Timer timer;
    int n = 200;
    public loadPicture() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        lbl_picture.setIcon(new ImageIcon(loadPicture.class.getResource(
                "pic.jpg")));
        lbl_picture.setBounds(new Rectangle(33, 9, 253, 226));
        pb_rute.setBounds(new Rectangle(7, 253, 309, 35));
        pb_rute.setStringPainted(true);
        pb_rute.setString("正在加载程序....");
        this.getContentPane().add(pb_rute);
        this.getContentPane().add(lbl_picture);
        this.setSize(340,340);
        this.toFront();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((screenSize.width-395)/2,(screenSize.height-302)/2 );
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
        timer = new Timer(100,new AbstractAction(){   
                public void actionPerformed(ActionEvent e) {
                    if(-- n >= 0)
                    {
                        pb_rute.setValue(200-n);
                        timer.restart();
                    }else
                    {
                        timer.stop();
                        closeFrame();
                    }
        }});
       
         
        timer.start();
    }
    private void closeFrame()
    {
        this.dispose();
    }

    public static void main(String[] args)
    {
        loadPicture lp = new loadPicture();
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值