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

📄 statusbar.java

📁 打印管理程序,测试完全通过.windows开发环境.
💻 JAVA
字号:
/* 
    $Author: $
    $Date: $
    $Revision: $
    $NoKeywords: $
 */

package jp.co.ntl.awt;

import java.awt.*;

import jp.co.ntl.Util;

public class StatusBar extends Component {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private volatile Color clrFrNormal = null;
    private volatile Color clrBkNormal = null;
    private volatile Color clrForeground = null;
    private volatile Color clrBackground = null;

    private Image imageBuffer = null;
	private Graphics graphicsImage = null;

    public final static int CENTER 	= 1;
    public final static int LEFT 	= 2;
    public final static int RIGHT 	= 3;

	private String text = "";     //昞帵偡傞暥帤楍
    private int alignment; //僥僉僗僩偺昞帵埵抲
    private boolean raised = false;
   
    // 僐儞僗僩儔僋僞
	public StatusBar() {
		this(100, 50);
	}

    // 僐儞僗僩儔僋僞
	public StatusBar(int width, int height) {
        this(width, height, LEFT);
    }

    // 僐儞僗僩儔僋僞
	public StatusBar(int width, int height, int alignment) {
		setSize(width, height);
		setBackground(Color.lightGray);
		this.alignment = alignment;
    }

    /**
     * 僥僉僗僩偺昤夋埵抲偺愝掕乮CENTER or LEFT or RIGHT乯
     */
    public void setAlignment(int alignment) {
        this.alignment = alignment;
    }

    /**
     * 僥僉僗僩偺昤夋埵抲偺庢摼乮CENTER or LEFT or RIGHT乯
     */
    public int getAlignment() {
        return alignment;
    }
    
    /**
     *
     */
    public void setText(String text) {
        this.text = text;
        repaint();
    }

    /**
     *
     */
    public String getText() {
        return text;
    }
    
    /**
     *
     */
    public void setRaised(boolean raised) {
        this.raised = raised;
        repaint();
    }
    
    public void setForeground(Color c) {
        clrFrNormal = c;
        clrForeground = c;
    }
    
    public void setBackground(Color c) {
        clrBkNormal = c;
        clrBackground = c;
    }
     
    // update
	public void update(Graphics g) {
		paint(g);
	}

	public void invalidate() {
		super.invalidate();
		imageBuffer = null;
		if (graphicsImage != null) {
		    graphicsImage.dispose();
		}
	}

    public void paintComponent(Graphics g) {
        int bx, by;
        Color colors[] = new Color[2];
        
        if (raised) {
            colors[0] = clrBackground.brighter();
            colors[1] = clrBackground.darker();
        } else {
            colors[0] = clrBackground.darker();
            colors[1] = clrBackground.brighter();
        }
        
		Dimension d = getSize();
		g.setColor(clrBackground);
		g.fill3DRect(0, 0, d.width, d.height, true);
        //偔傏傒
		g.setColor(colors[0]);
		g.drawLine(2, 2, d.width - 3, 2);
		g.drawLine(2, 2, 2, d.height - 3);
		g.setColor(colors[1]);
		g.drawLine(2, d.height - 3, d.width - 3, d.height -3);
		g.drawLine(d.width - 3, 3, d.width - 3, d.height - 3);

        FontMetrics fm = g.getFontMetrics();
        //僥僉僗僩偺昞帵埵抲偼5傛傝彫偝偔側傜側偄
        switch (alignment) {
        case CENTER:
            bx = Util.basePointX(fm, text, getSize().width);
            by = Util.basePointY(fm, getSize().height);
            if (bx < 5) {
                bx = 5;
            }
            break;
        case LEFT:
            bx = 5;
            by = Util.basePointY(fm, getSize().height);
            break;
        case RIGHT:
        default:
            if (fm.stringWidth(text) > getSize().width - 5) {
                bx = 5;
            } else {
                bx = getSize().width - fm.stringWidth(text) - 5;
            }
            by = Util.basePointY(fm, getSize().height);
            break;
        }

		g.setColor(clrForeground);
        if (text != null) {
            g.drawString(text, bx, by);
	    }
    }

    // paint
	public void paint(Graphics g) {
	    if (clrForeground == null) {
	        clrForeground = getForeground();
	    }
	    if (clrBackground == null) {
	        clrBackground = getBackground();
	    }
	    
        if (imageBuffer == null) {
			imageBuffer = createImage(getSize().width, getSize().height);
			graphicsImage = imageBuffer.getGraphics();
		}
	    
	    paintComponent(graphicsImage);
	    
   		g.drawImage(imageBuffer, 0, 0, null);
	}

    private BlinkThread blink = null;
    public synchronized void startBlink() {
        if (blink == null) {
            blink = new BlinkThread();
            blink.setDaemon(true);
            blink.start();
        }
    }
    
    public synchronized void stopBlink() {
        if (blink != null) {
            blink.setStop();
            blink = null;
        }
    }
    
    private static final Color[] clrBlkFr = new Color[] {Color.red, Color.white};
    private static final Color[] clrBlkBk = new Color[] {Color.white, Color.red};
    class BlinkThread extends Thread {
        private boolean stop = false;
        private boolean blkFlag = false;
        
        void setStop() {
            stop = true;
            interrupt();
        }
        
        public void run() {
            while (true) {
                if (!stop) {
                    if (blkFlag) {
                        clrForeground = clrBlkFr[0];
                        clrBackground = clrBlkBk[0];
                        blkFlag = false;
                    } else {
                        clrForeground = clrBlkFr[1];
                        clrBackground = clrBlkBk[1];
                        blkFlag = true;
                    }
                    repaint();
                    
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        break;
                    }
                } else {
                    break;
                }
            }
            clrForeground = clrFrNormal;
            clrBackground = clrBkNormal;
            repaint();
        }        
    }
}

⌨️ 快捷键说明

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