Update JProgressBar

后端 未结 6 1500
生来不讨喜
生来不讨喜 2021-01-22 21:43

I can\'t update my progressbar... this is my code

Thread t=new Thread(new Runnable(){
        public void run(){
            int i=1;
            jProgBar.setMin         


        
6条回答
  •  生来不讨喜
    2021-01-22 21:44

    Use a model instead of the JProgressBar directly:

    DefaultBoundedRangeModel model = new DefaultBoundedRangeModel();
    JProgressBar bar = new JProgressBar(model);
    
    // Somewhere else, perhaps in another Thread
    model.setValue(i)
    

    The following example works fine:

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 100);
        frame.setVisible(true);
        final DefaultBoundedRangeModel model = new DefaultBoundedRangeModel();
        frame.add(new JProgressBar(model));
        Thread t = new Thread(new Runnable() {
            public void run() {
                int i = 1;
                model.setMinimum(0);
                model.setMaximum(100);
                try {
                    while (i <= 100 || true) {
                        model.setValue(i);
                        i++;
                        Thread.sleep(50);
                    }
                } catch (InterruptedException ex) {
                    model.setValue(model.getMaximum());
                }
            }
        });
        t.start();
    
        Thread.sleep(2000);
    
        t.interrupt();
    }
    

提交回复
热议问题