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

📄 statusdialog.java

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

import java.awt.*;
import java.util.*;

public class StatusDialog extends Dialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private final int HORI_MARGIN = 30;
    private final int TOP_MARGIN = 20;
    private final int MIN_WIDTH = 200;
    private final int MIN_HEIGHT = TOP_MARGIN * 2;

    private MessageBoard msgBoard;

	public StatusDialog(Component parentComponent, String message, String title, boolean modal) {
	    super(getFrameForComponent(parentComponent), title, modal);
        setLayout(null);
    	setResizable(false);
    	//setEnabled(false);

        Panel panel = new Panel();
        panel.setLayout(null);
        msgBoard = new MessageBoard();
        //msgBoard.setBackground(Color.red);
        panel.add(msgBoard);
        add(panel);
        pack();
        msgBoard.setMessage(message);

        Dimension d = msgBoard.getSize();
        Insets insets = getInsets();
        int panelWidth, panelHeight;
        if (d.width > MIN_WIDTH - HORI_MARGIN * 2) {
            panelWidth = d.width + HORI_MARGIN * 2;
            msgBoard.setLocation(HORI_MARGIN, TOP_MARGIN);
        } else {
            panelWidth = MIN_WIDTH;
            msgBoard.setLocation((panelWidth - d.width) / 2, TOP_MARGIN);
        }
        panelHeight = MIN_HEIGHT + d.height;
        panel.setBounds(insets.left, insets.top, panelWidth, panelHeight);

        int width, height;
        width = panelWidth + insets.left + insets.right;
        height = panelHeight + insets.top + insets.bottom;
        Dimension screenSize = getToolkit().getScreenSize();
        setBounds((screenSize.width - width) / 2,
                    (screenSize.height - height) / 2,
                    width,
                    height);
	}

	public StatusDialog(Component parentComponent, String message, String title) {
		this(parentComponent, message, title, false);
    }

    private boolean fComponentsAdjusted = false;

	public void addNotify() {
	    // 恊偺addNotify傪屇傇慜偵丄敞菽蕹偺徊睫傪曐帩偟偰偍偔
	    Dimension d = getSize();

		super.addNotify();

		if (fComponentsAdjusted)
			return;

		// 草警膫蓮]偭偰狠芜叭菽埵抲傪挷愡偡傞
		setSize(getInsets().left + getInsets().right  + d.width,
				getInsets().top  + getInsets().bottom + d.height);

		Component components[] = getComponents();

		for (int i = 0; i < components.length; i++){
			Point p = components[i].getLocation();
			p.translate(getInsets().left, getInsets().top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
	}

    public void setMessage(String message) {
        msgBoard.setMessage(message);
    }
    
    private class MessageBoard extends Component {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
        private final int MAX_LINE = 10;//儊僢僙乕僕偺嵟戝峴悢

        private int stringHeight;
        private int stringAscent;

        private String[]    strings = new String[MAX_LINE];
        private int         lineNum;

        public MessageBoard() {}

        public void setMessage(String message) {
            // '\r'偲'\n'傪嬫愗傝暥帤偵偡傞
            StringTokenizer st = new StringTokenizer(message, "\r\n");
            for (lineNum = 0; st.hasMoreTokens() && lineNum < MAX_LINE; lineNum++) {
                strings[lineNum] = st.nextToken();
            }

            //pack(); //僐儞僥僉僗僩妋掕
            Graphics g = getGraphics();
            FontMetrics fm = g.getFontMetrics();
            stringHeight = fm.getHeight();
            stringAscent = fm.getAscent();
            //暘妱偟偨暥帤楍偺嵟戝挿傪maxStringLength偵愝掕
            int maxStringLength = 0;
            for (int i = 0; i < lineNum; i++) {
                 if (fm.stringWidth(strings[i]) > maxStringLength) {
                    maxStringLength = fm.stringWidth(strings[i]);
                 }
            }

            setSize(maxStringLength, stringHeight * lineNum);
        }

        public void paint(Graphics g) {
            g.setColor(Color.black);
            for (int i = 0; i < lineNum; i++) {
                g.drawString(strings[i], 0, stringAscent + stringHeight * i);
            }
        }
    }

    private static Frame getFrameForComponent(Component comp) {
        Component parent = comp;
        while (parent != null) {
            if (parent instanceof Frame) {
                break;
            }
            parent = parent.getParent();
        }
        return (Frame)parent;
    }
}

⌨️ 快捷键说明

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