您的位置首页生活快答

java进度条显示问题

java进度条显示问题

的有关信息介绍如下:

java进度条显示问题

要在重载的paint方法里调用父类的paint方法(super.paint(g);),你上面代码paint里什么都没做当然就不画任何东西了。

// 回答完毕,采纳即可

import java.awt.BorderLayout;

import java.awt.Toolkit;

import java.beans.PropertyChangeEvent;

import java.beans.PropertyChangeListener;

import java.util.Random;

import javax.swing.BorderFactory;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JProgressBar;

import javax.swing.SwingWorker;

public class Bar extends JPanel implements PropertyChangeListener

{

private static final long serialVersionUID = 1L;

private JProgressBar progressBar;

private Task task;

public Bar()

{

super(new BorderLayout());

progressBar = new JProgressBar(0, 100);

progressBar.setValue(0);

progressBar.setStringPainted(true);

JPanel panel = new JPanel();

panel.add(progressBar);

add(panel, BorderLayout.PAGE_START);

setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

}

class Task extends SwingWorker

{

@Override

public Void doInBackground()

{

Random random = new Random();

int progress = 0;

setProgress(0);

while(progress < 100)

{

try

{

Thread.sleep(random.nextInt(1000));

}

catch(InterruptedException ignore)

{}

progress += random.nextInt(10);

setProgress(Math.min(progress, 100));

}

return null;

}

@Override

public void done()

{

Toolkit.getDefaultToolkit().beep();

System.exit(0);

}

}

public void propertyChange(PropertyChangeEvent evt)

{

if("progress" == evt.getPropertyName())

{

int progress = (Integer) evt.getNewValue();

progressBar.setValue(progress);

}

}

private void start()

{

task = new Task();

task.addPropertyChangeListener(this);

task.execute();

}

private static void createAndShowGUI()

{

JFrame frame = new JFrame("bar");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Bar b = new Bar();

b.setOpaque(true);

b.start();

frame.setContentPane(b);

frame.pack();

frame.setResizable(false);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args)

{

javax.swing.SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

createAndShowGUI();

}

});

}

}