有谁知道进行ftp下载的时候如何显示进度条?我用javax.swing包里的ProgressMonitorInputStream类试了一下,但是显示不出进度条来,不知道是为什么也不知道还有没有其他的方法来显示进度条。
解决方案 »
- 关于swing的问题,单击按钮当前按钮换成另一个按钮
- -------------------大家在开发系统的时候,都是开发到什么阶段开始在数据库建立VIEW---------------------
- java执行cmd的问题
- java如何调用dll文件升级程序?
- EJB部署到WebLogic中为何老有错误
- 关于利用java将txt文件转换为pdf格式的问题
- 恳求大家帮忙做一下这个课程设计:如何用java做VOD?
- XP对JDK1.3是否支持?
- 怎样用java编写一个学籍管理系统?
- 没办法,还要问tomcat的问题,是不是和机器有关,总之没搞定!
- 请问如何调用JSP页面呀(高手请进,急着呢)
- ArrayList.toArray如何返回指定类型的数组
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;import javax.swing.*;public class ProgressList extends JPanel implements Scrollable
{
public ProgressList()
{
super(null);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBackground(Color.WHITE);
}public Dimension getPreferredScrollableViewportSize()
{
return getPreferredSize();
}public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
{
if (getComponentCount() > 0) {
Component c = getComponent(0);
return c.getHeight();
}
return 0;
}public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
{
return orientation == SwingConstants.HORIZONTAL ? visibleRect.width : visibleRect.height;
}public boolean getScrollableTracksViewportWidth()
{
return true;
}public boolean getScrollableTracksViewportHeight()
{
return true;
}private static class Progress extends JPanel {//private static final ImageIcon CANCEL_BTN_ICON = new ImageIcon(ProgressList.class.getResource("imgs/cancelIcon.png"));
//private static final ImageIcon TITLE_ICON = new ImageIcon(ProgressList.class.getResource("imgs/titleIcon.png"));private static final Color SELECTION_BG = UIManager.getColor("List.selectionBackground");
private static final Color SELECTION_FG = UIManager.getColor("List.selectionForeground");
private static final Color UNSELECTION_BG = new Color(245, 245, 245);
private static final Color UNSELECTION_FG = UIManager.getColor("List.foreground");
private JLabel titleIconLabel = new JLabel();
private JLabel titleLabel = new JLabel();
private JLabel progressInfoLabel = new JLabel();
private JButton cancelBtn = new JButton();
private JProgressBar progressBar = new JProgressBar();
private boolean selected;private Progress(String title, int progress, String processInfo) {
super(new BorderLayout(5, 0));titleLabel.setText(title);
progressBar.setValue(progress);
progressInfoLabel.setText(processInfo);//titleIconLabel.setIcon(TITLE_ICON);
//cancelBtn.setIcon(CANCEL_BTN_ICON);JToolBar cancelBtnPane = new JToolBar();
cancelBtnPane.setOpaque(false);
cancelBtnPane.setFloatable(false);
cancelBtnPane.setRollover(true);
cancelBtnPane.setLayout(new BoxLayout(cancelBtnPane, BoxLayout.X_AXIS));
cancelBtnPane.setBorder(null);
cancelBtnPane.add(cancelBtn);cancelBtn.setContentAreaFilled(false);
cancelBtn.setEnabled(isCancelable());
cancelBtn.setMargin(new Insets(0,0,0,0));
cancelBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cancel();
}
});JPanel centerPane = new JPanel(new BorderLayout(0, 2));
centerPane.setOpaque(false);centerPane.add(titleLabel, BorderLayout.NORTH);
centerPane.add(progressBar, BorderLayout.CENTER);
centerPane.add(progressInfoLabel, BorderLayout.SOUTH);
this.add(titleIconLabel, BorderLayout.WEST);
this.add(centerPane, BorderLayout.CENTER);
this.add(cancelBtnPane, BorderLayout.EAST);titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD));
titleIconLabel.setVerticalAlignment(JLabel.TOP);this.setOpaque(true);
this.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 5));this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
setSelected(!isSelected());
}
});setSelected(false);
}public Dimension getMaximumSize()
{
Dimension d = getPreferredSize();
d.width = Integer.MAX_VALUE;return d;
}public void setSelected(boolean b)
{
this.selected = b;
if (b) {
setBackground(SELECTION_BG);
titleLabel.setForeground(SELECTION_FG);
progressInfoLabel.setForeground(SELECTION_FG);
}
else {
setBackground(UNSELECTION_BG);
titleLabel.setForeground(UNSELECTION_FG);
progressInfoLabel.setForeground(UNSELECTION_FG);
}
}public boolean isSelected()
{
return selected;
}protected void paintComponent(Graphics g)
{
((Graphics2D)g).setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0, getBackground().brighter(), 0, getHeight(), getBackground());Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
}private void removeFromProgressList()
{
Container parent = getParent();
if (parent != null) {
parent.remove(this);
parent.validate();
parent.repaint();
}
}public int getProgress()
{
return progressBar.getValue();
}public void setProgress(final int progress)
{
if (SwingUtilities.isEventDispatchThread()) {
progressBar.setValue(progress);
if (progress >= 100) {
completed();
}
}
else {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
progressBar.setValue(progress);
if (progress >= 100) {
completed();
}
}
});
}
}public String getProgressInfo()
{
return progressInfoLabel.getText();
}public void setProcessInfo(final String progressInfo)
{
if (SwingUtilities.isEventDispatchThread()) {
progressInfoLabel.setText(progressInfo);
}
else {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
progressInfoLabel.setText(progressInfo);
}
});
}
}protected boolean isCancelable()
{
return true;
}protected void cancel()
{
removeFromProgressList();
}protected void completed()
{
removeFromProgressList();
}
}private static int n = 0;public static void main(String[] args)
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(new PlasticXPLookAndFeel());
} catch (Exception e) {
e.printStackTrace();
}final Random random = new Random();
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
final ProgressList progressList = new ProgressList();JButton btn = new JButton("Start");
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final Progress progress = new Progress("Test Progress - " + (n++), 0, " ");
progressList.add(progress);
progressList.revalidate();
progressList.getParent().repaint();new Thread() {
public void run()
{
while (true) {
try {
Thread.sleep(2000 + random.nextInt(200));
} catch (InterruptedException e) {
}
int t = progress.getProgress();
if (t >= 100) {
return;
}t += random.nextInt(20);
t = Math.min(t, 100);
progress.setProgress(t);
progress.setProcessInfo("Progress " + t + "% complete, current time is " + sdf.format(new Date()));
}
}
}.start();
}
});
JPanel btnPane = new JPanel();
btnPane.add(btn);JScrollPane sp = new JScrollPane(progressList);
JFrame f = new JFrame("Progress List Test");
f.getContentPane().add(sp, BorderLayout.CENTER);
f.getContentPane().add(btnPane, BorderLayout.SOUTH);
f.setSize(400, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.ProgressMonitorInputStream;import sun.net.*;
import sun.net.ftp.*;
public class ProcessBar { public static void main(String[] args) throws IOException {
final JFrame f = new JFrame("ProgressMonitor Sample");
f.getContentPane().setLayout(new FlowLayout());
JButton b = new JButton("Click me");
f.getContentPane().add(b);
f.pack();
File f1=new File("e://test1//a.rmvb");
final RandomAccessFile getFile =new RandomAccessFile(f1,"rw");
getFile.seek(0);
FtpClient client=new FtpClient("192.168.1.217");
client.login("ftpuser","123456");
client.binary();
TelnetInputStream ftpFile=client.get("/a.rmvb");
final InputStream in=new DataInputStream(ftpFile);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
try {
InputStream in = new FileInputStream("e://anywhere for you.rmvb"); ProgressMonitorInputStream pm = new ProgressMonitorInputStream (f,"Reading file: a.rmvb",in);
int c;
while((c=pm.read()) != -1) {
getFile.write(c);
}
System.out.println("下载完毕");
pm.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}.start();
}});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
client.closeServer();
}
}