怎么将进度条从下到上慢慢填充颜色,用java编写,那个进度条我用的JProgressBar,求解
展开全部
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class Floor extends JFrame
{
private static final long serialVersionUID = 1L;
private final int WIDTH = 500;
private final int HEIGHT = 500;
private final int L = 100;
private final int H = 100;
JProgressBar progressBar;
public Floor ( String title )
{
setTitle (title);
setLayout (null);
setSize (WIDTH, HEIGHT);
setResizable (false);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
private void addComponents ( JFrame frame )
{
JPanel panel = new JPanel ();
panel.setSize (WIDTH, HEIGHT);
panel.setLayout (null);
progressBar = new JProgressBar (JProgressBar.VERTICAL);
progressBar.setLocation (( WIDTH - L ) / 2 + L / 2 - L / 8, H);
progressBar.setSize (L / 4, HEIGHT - 2 * H - 28);
progressBar.setValue (0);
progressBar.setStringPainted (true);
JButton down = new JButton ("下楼");
down.setLocation (( WIDTH - L ) / 2, 0);
down.setSize (L, H);
JButton up = new JButton ("上楼");
up.setLocation (( WIDTH - L ) / 2, HEIGHT - H - 28);
up.setSize (L, H);
up.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
Task task = new Task ();
task.addPropertyChangeListener (new PropertyChangeListener ()
{
@Override
public void propertyChange ( PropertyChangeEvent evt )
{
if ("progress" == evt.getPropertyName ())
{
int progress = (Integer) evt.getNewValue ();
progressBar.setValue (progress);
}
}
});
task.execute ();
}
});
panel.add (down);
panel.add (progressBar);
panel.add (up);
frame.add (panel);
}
class Task extends SwingWorker<Void, Void>
{
@Override
public Void doInBackground ()
{
progressBar.setValue (0);
while (progressBar.getValue () < progressBar.getMaximum ())
{
try
{
Thread.sleep (50);
}
catch (InterruptedException ignore)
{}
progressBar.setValue (progressBar.getValue () + 1);
}
return null;
}
}
public static void main ( String[] args )
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override
public void run ()
{
Floor floor = new Floor ("电梯管理");
floor.addComponents (floor);
floor.setVisible (true);
}
});
}
}
追问
虽然对于SwingWorker不是很熟悉,但很感谢你的代码,能留个qq或者其他联系方式,我的联系方式给你也行,希望能请教你更多问题,拜托了
追答
不用了,自己多看看api里面的demo就可以了,我也是从那里仿写的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询