Java中如何实现进度条效果
2个回答
展开全部
代码如下:import java.awt.Color; import java.awt.Toolkit; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.JWindow; @SuppressWarnings("serial") public class Demo extends JWindow implements Runnable { // 定义加载窗口大小 public static final int LOAD_WIDTH = 455; public static final int LOAD_HEIGHT = 295; // 获取屏幕窗口大小 public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; // 定义进度条组件 public JProgressBar progressbar; // 定义标签组件 public JLabel label; // 构造函数 public Demo() { // 创建标签,并在标签上放置一张图片 label = new JLabel(new ImageIcon("images/background.jpg")); label.setBounds(0, 0, LOAD_WIDTH, LOAD_HEIGHT - 15); // 创建进度条 progressbar = new JProgressBar(); // 显示当前进度值信息 progressbar.setStringPainted(true); // 设置进度条边框不显示 progressbar.setBorderPainted(false); // 设置进度条的前景色 progressbar.setForeground(new Color(0, 210, 40)); // 设置进度条的背景色 progressbar.setBackground(new Color(188, 190, 194)); progressbar.setBounds(0, LOAD_HEIGHT - 15, LOAD_WIDTH, 15); // 添加组件 this.add(label); this.add(progressbar); // 设置布局为空 this.setLayout(null); // 设置窗口初始位置 this.setLocation((WIDTH - LOAD_WIDTH) / 2, (HEIGHT - LOAD_HEIGHT) / 2); // 设置窗口大小 this.setSize(LOAD_WIDTH, LOAD_HEIGHT); // 设置窗口显示 this.setVisible(true); } public static void main(String[] args) { Demo t = new Demo(); new Thread(t).start(); } @Override public void run() { for (int i = 0; i < 100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } progressbar.setValue(i); } JOptionPane.showMessageDialog(this, "加载完成"); this.dispose(); } } 效果图:
展开全部
怎样做进度条,下面给个简单的做法:
主要是使用JProgressBar(Swing内置javax.swing.JProgressBar)和SwingWorker(Swing内置javax.swing.SwingWorker)
package org.ubird;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class TestSwingWorker extends SwingWorker<List<Image>,String>{
private JLabel status;
private JPanel viewer;
private String[] imagesName;
private JProgressBar jpb;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
TestFrame frame = new TestFrame();
final JPanel panel = new JPanel();
final JLabel label = new JLabel();
final String[] imagesName = new String[38];
for(int i=0; i<imagesName.length; i++){
imagesName[i] = "res/1 ("+(i+1)+").jpg";
}
JScrollPane sp = new JScrollPane(panel);
sp.setSize(new Dimension(700,500));
frame.add(sp,BorderLayout.CENTER);
JPanel stp = new JPanel();
final JProgressBar jpb = new JProgressBar();
jpb.setMinimum(1);
jpb.setMaximum(imagesName.length);
stp.add(jpb);
stp.add(label);
frame.add(stp,BorderLayout.SOUTH);
JButton button = new JButton("load image");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
TestSwingWorker sw = new TestSwingWorker(label, panel, imagesName, jpb);
sw.execute();
}
});
frame.add(button,BorderLayout.NORTH);
frame.setVisible(true);
}
});
}
public TestSwingWorker(JLabel status, JPanel viewer, String[] imagesName, JProgressBar jpb){
this.status = status;
this.viewer = viewer;
this.imagesName = imagesName;
this.jpb = jpb;
}
@Override
protected List<Image> doInBackground() throws Exception {
List<Image> image = new ArrayList<Image>();
for(int i=0; i<imagesName.length; i++){
image.add(ImageIO.read(getClass().getClassLoader().getResource(imagesName[i])));
publish("已经加载了 "+(i+1)+"/"+imagesName.length+" : "+imagesName[i]);
}
return image;
}
@Override
protected void process(List<String> chunks) {
status.setText(chunks.get(chunks.size()-1));
int x = Integer.parseInt(chunks.get(chunks.size()-1).substring(chunks.get(chunks.size()-1).indexOf("(")+1,chunks.get(chunks.size()-1).indexOf(")")).trim());
jpb.setValue(x);
for(String str : chunks){
System.out.println(str);
}
}
@Override
protected void done() {
try {
for(Image image : get()){
JLabel label = new JLabel(new ImageIcon(image));
label.setSize(160, 120);
label.setPreferredSize(new Dimension(160,120));
viewer.add(label);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
package org.ubird;
import javax.swing.JFrame;
public class TestFrame extends JFrame {
public TestFrame(){
setTitle("Test Frame");
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询