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

📄 progressbar.java

📁 java2 primer plus一书源程序
💻 JAVA
字号:
/*
 * ProgressBar.java
 *
 * Created on August 27, 2002, 11:18 AM
 */

package com.samspublishing.jpp.ch15;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.Serializable;

/**
 *
 * @author  Stephen Potts
 */
public class ProgressBar extends Canvas implements Serializable
{
    private float scaleSize;
    private float currentValue;
    
    //Default constructor
    public ProgressBar()
    {
        this(100, 50);
    }
    
    
    //Constructor
    public ProgressBar(float scaleSize, float currentValue)
    {
        super();
        
        this.scaleSize = scaleSize;
        this.currentValue = currentValue;
        
        setBackground(Color.lightGray);
        setForeground(Color.magenta);
        
        //sets the initial size of the bean on a canvas
        setSize(100, 25);
    }
    
    public float getScaleSize()
    {
        return scaleSize;
    }
    
    public void setScaleSize(float sSize)
    {
        //The scale size can never set to a value lower than
        //the current value
        this.scaleSize = Math.max(0.0f, sSize);
        if (this.scaleSize < this.currentValue)
        {
            this.scaleSize = this.currentValue;
        }
    }
    
    public float getCurrentValue()
    {
        return currentValue;
    }
    
    public void setCurrentValue(float cVal)
    {
        //The current value can not be set negative
        //nor can it be set greater than the scale size
        this.currentValue = Math.max(0.0f, cVal);
        if (this.currentValue > this.scaleSize)
        {
            this.currentValue = this.scaleSize;
        }
    }
    
    //The paint method is called by the container
    public synchronized void paint(Graphics g)
    {
        int width = getSize().width;
        int height = getSize().height;
        
        g.setColor(getBackground());
        g.fillRect(1, 1, width-2, height-2);
        g.draw3DRect(0,0, width-1, height-1, true);
        
        g.setColor(getForeground());
        g.fillRect(3,3,(int)((currentValue * (width-6))/scaleSize),
        height-6);
    }
    
    //The grow method makes the current value larger
    public void grow()
    {
        setCurrentValue( this.currentValue + 1.0f);
    }
    
    
    //The shrink method makes the current value smaller
    public void shrink()
    {
        setCurrentValue( this.currentValue - 1.0f);
    }
}//class

⌨️ 快捷键说明

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