⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 progress.java

📁 本程序在Unix_Linux环境下用Java语言编写的文件下载程序
💻 JAVA
字号:
package loader.viewer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import loader.listener.*;
/**
	this is the reference implementation of Viewer.
	i extend it from JPanel, but you perhaps implement
	it more concretely.
*/
public class Progress extends JPanel implements Viewer {

	private JProgressBar progressBar;
        
        private JLabel url;
        private JLabel location;
        private JLabel percent;
        private JLabel time;
        private JLabel acceleration;

	private CompleteListener completeListener;

	private int len;
        private long curt;
	private int max_len, min_len;
	
	public Progress() {
		super();
		
		this.len = 0;
                
                setLayout( new GridLayout( 6, 1 ) );
                
                JPanel p = new JPanel( new FlowLayout() );                
                p.add( new JLabel( "URL: " ), FlowLayout.LEFT );
                p.add( (url=new JLabel("??##??")), FlowLayout.CENTER );
                url.setPreferredSize( new Dimension( 200, 15 ) );
                add( p );
                
                p = new JPanel( new FlowLayout() ); 
                p.add( new JLabel( "Location: " ), FlowLayout.LEFT );
                p.add( (location=new JLabel("??##??")), FlowLayout.CENTER );
                location.setPreferredSize( new Dimension( 200, 15 ) );
                add( p );
                
                p = new JPanel( new FlowLayout() ); 
                p.add( new JLabel( "Progress: " ), FlowLayout.LEFT );
		progressBar = new JProgressBar();
                progressBar.setPreferredSize( new Dimension( 200, 15 ) );
		progressBar.setStringPainted( true );
		progressBar.setIndeterminate( true );
                p.add( progressBar, FlowLayout.CENTER );
                add( p );
                
                p = new JPanel( new FlowLayout() ); 
                p.add( new JLabel( "Percent: " ), FlowLayout.LEFT );
                p.add( (percent=new JLabel("? %")), FlowLayout.CENTER );
                percent.setPreferredSize( new Dimension( 200, 15 ) );
                add( p );
                
                p = new JPanel( new FlowLayout() ); 
                p.add( new JLabel( "Time: " ), FlowLayout.LEFT );
                p.add( (time=new JLabel("???")), FlowLayout.CENTER );
                time.setPreferredSize( new Dimension( 200, 15 ) );
                add( p );
                
                p = new JPanel( new FlowLayout() ); 
                p.add( new JLabel( "Acceleration: " ), FlowLayout.LEFT );
                p.add( (acceleration=new JLabel("? b/s")), FlowLayout.CENTER );
                acceleration.setPreferredSize( new Dimension( 200, 15 ) );
                add( p );

		//percentLabel = new JLabel( "" );
		//setLayout( new FlowLayout() );
		//add( progressBar );
	}

	public void setMini_Max( int mini, int max ) {
		this.min_len = mini;
		this.max_len = max;
		progressBar.setMinimum( this.min_len );
		progressBar.setMaximum( this.max_len );
	}

        public void setLength( int len ) {
                progressBar.setValue( len );
		this.len = len;
	}

	synchronized public void registerCompleteListener( CompleteListener c) {
		this.completeListener = c;
	}

	synchronized public void show( int incr ) {
		len += incr;
                
                int cur = progressBar.getValue() + incr;
		progressBar.setValue( cur );
                
                percent.setText( len +"/"+ max_len );
                int c_p = (int)(System.currentTimeMillis() - curt)/1000;
                if( c_p == 0 ){
                    c_p = 1;
                }
                int a_p = (int)incr/c_p;
                if( a_p == 0 ){
                    a_p = 1;
                }
                acceleration.setText( a_p + " b/s" );
                time.setText( (int)max_len/a_p + " s" );
                curt = System.currentTimeMillis();
                
		if( progressBar.getValue() == progressBar.getMaximum() ) {
			//fireComplete();
                    completeListener.complete( new CompleteEvent( Progress.this ) );
		}
	}

	public void doEnd(){
		this.progressBar.setValue( 0 );
		this.progressBar.setIndeterminate( true );
	}
        
        public void setURL( String l ){
            url.setText( l );
        }
        public void setLocation( String l ){
            location.setText( l );
        }
        public void setCurt( long t ){
            this.curt = t;
        }
        public void doInit(){
            setCurt( System.currentTimeMillis() );
        }
	//private void  fireComplete() {
            //Progress.this.completeListener.complete( new CompleteEvent( Progress.this ) );
		/*new Thread( new Runnable() {
			public void run() {
				Progress.this.completeListener.complete( new CompleteEvent( Progress.this ) );
			}
		}).start();*/
	//}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -