java进度条显示问题
publicclassbarextendsJPanel{privateintintervel=10;staticJProgressBarjBar=newJProgress...
public class bar extends JPanel{
private int intervel = 10;
static JProgressBar jBar = new JProgressBar(0,200);
public void action() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
setJBar();
repaint();
}
}, intervel, intervel);
}
public void setJBar(){
jBar.setValue(123);
}
public void paint(Graphics g){//为什么重写paint方法后 进度条jBar就不显示了 ?
}
public static void main(String[] args){
JFrame jf = new JFrame();
bar panel = new bar();
jBar.setValue(100);
panel.add(jBar);
jf.add(panel);
jf.setSize(500,700);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
panel.action();
}
}
为什么不重写paint方法时 在窗口中可以显示进度条 重写paint方法后就不显示进度条了呢?
在重写paint方法的情况下 怎样才能显示进度条? 可不可以使用画笔Graphics画进度条?
谢谢各位了 展开
private int intervel = 10;
static JProgressBar jBar = new JProgressBar(0,200);
public void action() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
setJBar();
repaint();
}
}, intervel, intervel);
}
public void setJBar(){
jBar.setValue(123);
}
public void paint(Graphics g){//为什么重写paint方法后 进度条jBar就不显示了 ?
}
public static void main(String[] args){
JFrame jf = new JFrame();
bar panel = new bar();
jBar.setValue(100);
panel.add(jBar);
jf.add(panel);
jf.setSize(500,700);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
panel.action();
}
}
为什么不重写paint方法时 在窗口中可以显示进度条 重写paint方法后就不显示进度条了呢?
在重写paint方法的情况下 怎样才能显示进度条? 可不可以使用画笔Graphics画进度条?
谢谢各位了 展开
展开全部
// 回答完毕,采纳即可
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<Void, Void>
{
@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();
}
});
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询