Java graphical interface --- progress bar

Table of contents

1. Introduction of the progress bar

2. Create a progress bar

3. Simple case

 4. Sub-thread creation progress bar

(1) Why use sub-threads to create progress bars

 (2) The code for creating the progress bar by the child thread

5. Create a progress dialog

 (1) How to create a progress dialog

(2) Case


1. Introduction of the progress bar

The progress bar is a widely used GUI component in the graphical interface. When copying a large file, the operating system will display a progress bar to indicate the proportion of the copy operation completed; when starting programs such as Eclipse, because more files need to be loaded resources, so the startup speed is slow, and the program will also display a progress bar during the startup process to indicate the proportion of the software startup completion......

2. Create a progress bar

(1) Create a JProgressBar object
JProgressBar(int o, int mi, int ma)
o: direction
mi: minimum value
ma: maximum

(2) Set the attribute
setBorderPainted(boolean b) to set whether the progress bar has a border
setIndeterminate(boolean b) to set whether the current progress bar is a progress bar with indeterminate progress, if so, you will see a slider moving left and right in the progress bar
setStringPainted(boolean b) sets whether the progress bar displays the current completed percentage

(3) Get and set the progress status of the current progress bar
 setValue(int n) Set the current progress value
 double getPerecentComplete() Get the completion percentage String of the progress bar
 getString() Return the current value of the progress string

3. Simple case

Make the interface as shown in the figure:

public class exer{
    JFrame s=new JFrame("程序练习");
    JCheckBox b1=new JCheckBox("不确定进度");
    JCheckBox b2=new JCheckBox("不绘制边框");
    JProgressBar bar=new JProgressBar(JProgressBar.HORIZONTAL,0,100);
    public void init(){

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取是否点击了不确定进度
                boolean b=b1.isSelected();
                //设置当前进度条不确定进度

                bar.setIndeterminate(b);
                //设置是否显示当前百分比
                bar.setStringPainted(!b);
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取是否选中复选框
                boolean b=b2.isSelected();
                bar.setBorderPainted(!b);
            }
        });
        Box box1=Box.createVerticalBox();
        box1.add(b1);
        box1.add(b2);
        Box box2=Box.createHorizontalBox();
        box2.add(box1);
        box2.add(bar);
        s.add(box2);

        //开始设置默认属性
        bar.setBorderPainted(true);
        bar.setStringPainted(true);

        s.setDefaultCloseOperation(3);
        s.pack();
        s.setVisible(true);

        //通过循环模拟进度条
        for(int i=0;i<=100;i++){
            bar.setValue(i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 4. Sub-thread creation progress bar

(1) Why use sub-threads to create progress bars

If you use the main thread when creating a progress bar, then you can't do other things, you can only wait for the progress bar to end before you can do other things, which is extremely poor user experience. We usually use sub-threads to create all progress bars, so that the user experience is better.

 (2) The code for creating the progress bar by the child thread

public class exer{
    JFrame s=new JFrame("程序练习");
    JCheckBox b1=new JCheckBox("不确定进度");
    JCheckBox b2=new JCheckBox("不绘制边框");
    JProgressBar bar=new JProgressBar(JProgressBar.HORIZONTAL,0,100);
    public void init(){

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取是否点击了不确定进度
                boolean b=b1.isSelected();
                //设置当前进度条不确定进度

                bar.setIndeterminate(b);
                //设置是否显示当前百分比
                bar.setStringPainted(!b);
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取是否选中复选框
                boolean b=b2.isSelected();
                bar.setBorderPainted(!b);
            }
        });
        Box box1=Box.createVerticalBox();
        box1.add(b1);
        box1.add(b2);
        Box box2=Box.createHorizontalBox();
        box2.add(box1);
        box2.add(bar);
        s.add(box2);

        //开始设置默认属性
        bar.setBorderPainted(true);
        bar.setStringPainted(true);

        s.setDefaultCloseOperation(3);
        s.pack();
        s.setVisible(true);

        //开启子线程
        sim ss=new sim(bar.getMaximum());
        new Thread(ss).start();

        //设置一个定时任务
        Timer ti=new Timer(200, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //读取线程任务对象的当前完成量,设置给进度条
                int current=ss.getCurrent();
                bar.setValue(current);
            }
        });
        ti.start();
        bar.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if(bar.getValue()==ss.getAmount()){
                    ti.stop();
                }
            }
        });
    }
    private class sim implements Runnable{
        //任务总量
        private int amount;
        //记录当前完成的任务量
        private volatile int current;

        public sim(int amount){
            this.amount=amount;
        }
        public int getAmount(){
            return amount;
        }
        public void setAmount(int amount){
            this.amount=amount;
        }
        public void setCurrent(int current){
            this.current=current;
        }
        public int getCurrent(){
            return current;
        }
        @Override
        public void run() {
             while(current<100){
                 try {
                     Thread.currentThread().sleep(50);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 current++;
             }
        }
    }
}

5. Create a progress dialog

 (1) How to create a progress dialog

The usage of ProgressMonitor is basically similar to that of JProgressBar, except that ProgressMonitor can directly create a progress dialog box, which provides the following constructor to complete the dialog box creation

public ProgressMonitor(Component p,Object m,String n,int mi,int ma)
p: the parent component of the dialog box, this parent component may not exist.
m: the description information of the dialog box
n: the prompt information of the progress bar
mi: the minimum value of the progress bar
ma: the maximum value of the progress
 
 bar Whether to include a border (always includes a border), the progress cannot be set to be uncertain, and the direction of the progress bar cannot be changed (always horizontal)

(2) Case

 Make the interface as shown in the figure

 

public class exer{
    Timer t;
    public void init(){
        ProgressMonitor monitor=new ProgressMonitor(null,"等待任务完成","已完成",0,100);
        sim ss=new sim(100);
        new Thread(ss).start();
        t=new Timer(200, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //读取当前进度,设置进度条
                monitor.setProgress(ss.current);
                //判断用户是否点击了取消按钮
                if (monitor.isCanceled()==true){
                    t.stop();
                    monitor.close();
                    System.exit(0);
                }
            }
        });
        t.start();
    }
    private class sim implements Runnable{
        private volatile int current=0;
        private int amount;

        public sim(int amount){
            this.amount=amount;
        }
        public void setCurrent(int current){
            this.current=current;
        }
        public void setAmount(int amount){
            this.amount=amount;
        }
        public int getAmount(){
            return amount;
        }
        public int getCurrent(){
            return current;
        }
        @Override
        public void run() {
            while(current<100){
                try {
                    Thread.currentThread().sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                current++;
            }

        }
    }
}

Guess you like

Origin blog.csdn.net/gaoqiandr/article/details/128807518
Recommended