Java进度条的使用

Java进度条的使用importjava.awt.BorderLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener; importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JProgressBar;

Java进度条的使用

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JProgressBar;

 

publicclass ProgressBarTest extends JFrame{

       private JProgressBar jpb = new JProgressBar();

       private JButton btn = new JButton(“start”);

       public ProgressBarTest() {

              setTitle(进度条”);

              setSize(600, 500);

              setLocationRelativeTo(null);

              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              setVisible(true);

              this.getContentPane().setLayout(new BorderLayout());

              jpb.setString(准备就绪。”);

              jpb.setMinimum(0);

              jpb.setMaximum(100);

              jpb.setStringPainted(true);

              this.add(jpb,BorderLayout.SOUTH);

              this.add(btn,BorderLayout.NORTH);

              btn.addActionListener(new ActionListener() {

 

                     publicvoid actionPerformed(ActionEvent actionevent) {

                            new progressThread(jpb).start();

                     }

                    

              });

       }

       /**

        * @param args

        */

       publicstaticvoid main(String[] args) {

              new ProgressBarTest();

       }

 

}

 

class progressThread extends Thread {

       private JProgressBar jpb;

       public progressThread(JProgressBar jpb) {

              this.jpb = jpb;

       }

       publicvoid run() {

              int max_amount = 100;

              int currentIndex = 0;

              while (currentIndex < max_amount) {

                     currentIndex += 10;

                     System.out.println(currentIndex);

                     jpb.setValue(currentIndex);

                     jpb.setString(已完成进度的” + currentIndex + “%”);

                    

                     try {

                            Thread.sleep(500);

                     } catch (InterruptedException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                     }

              }

       }

}

 

读取大文件可以考虑使用进度条。今天的文章Java进度条的使用分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/27735.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注