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

📄 optiondialog.java

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

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

import jp.co.ntl.Resource;

public class OptionDialog extends Dialog implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    /** Dialog Type */
    public static final int         DEFAULT_OPTION = -1;
    public static final int         YES_NO_OPTION = 0;
    public static final int         YES_NO_CANCEL_OPTION = 1;
    public static final int         OK_CANCEL_OPTION = 2;

    /** Return value */
    public static final int         YES_OPTION = 0;
    public static final int         NO_OPTION = 1;
    public static final int         CANCEL_OPTION = 2;
    public static final int         OK_OPTION = 0;
    public static final int         CLOSED_OPTION = -1;

    private final int HORI_MARGIN = 30;
    private final int TOP_MARGIN = 20;
    private final int BOTTOM_MARGIN = 10;
    private final int MSG_BUTTON_INTERVAL = 15;
    private final int BUTTON_WIDTH = 80;
    private final int BUTTON_HEIGHT = 25;
    private final int BUTTON_INTERVAL = 10;
    private final int MIN_WIDTH = 200;
    private final int MIN_HEIGHT = TOP_MARGIN + BOTTOM_MARGIN + MSG_BUTTON_INTERVAL + BUTTON_HEIGHT;

	private SimpleButton[]        buttons;
	
    int selectedOption;

    private MessageBoard msgBoard;
	
	public OptionDialog(Frame parent, String message, String title, int optionType) {
	    super(parent, title, true);
        setLayout(null);
    	setResizable(false);	    

        switch (optionType) {
        case DEFAULT_OPTION:
            buttons = new SimpleButton[1];
            buttons[0] = new SimpleButton(Resource.getString(Resource.OK));
            buttons[0].addActionListener(this);
            break;
        case YES_NO_OPTION:
            buttons = new SimpleButton[2];
            buttons[0] = new SimpleButton(Resource.getString(Resource.YES));
            buttons[1] = new SimpleButton(Resource.getString(Resource.NO));
            buttons[0].addActionListener(this);
            buttons[1].addActionListener(this);
            break;
        case YES_NO_CANCEL_OPTION:
            buttons = new SimpleButton[3];
            buttons[0] = new SimpleButton(Resource.getString(Resource.YES));
            buttons[1] = new SimpleButton(Resource.getString(Resource.NO));
            buttons[2] = new SimpleButton(Resource.getString(Resource.CANCEL));
            buttons[0].addActionListener(this);
            buttons[1].addActionListener(this);
            buttons[2].addActionListener(this);
            break;
        case OK_CANCEL_OPTION:
            buttons = new SimpleButton[2];
            buttons[0] = new SimpleButton(Resource.getString(Resource.OK));
            buttons[1] = new SimpleButton(Resource.getString(Resource.CANCEL));
            buttons[0].addActionListener(this);
            buttons[1].addActionListener(this);
            break;
        }

        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);

        int x = (panelWidth - (BUTTON_WIDTH * buttons.length + BUTTON_INTERVAL * (buttons.length - 1))) / 2;        
        for (int i = 0; i < buttons.length; i++) {
            buttons[i].setBounds(x, panelHeight - BOTTOM_MARGIN - BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
            panel.add(buttons[i]);
            x += BUTTON_WIDTH + BUTTON_INTERVAL;
        }

        addWindowListener(new WindowAdapter() {
    		public void windowClosing(WindowEvent we) {
                selectedOption = CLOSED_OPTION;                
    			dispose();
    		}
    	});

    }

	public static int showConfirmDialog(Component parent, String message, String title, int optionType) {
	    return showOptionDialog(getFrameForComponent(parent), message, title, optionType);
	}

	public static int showMessageDialog(Component parent, String message, String title) {
	    return showOptionDialog(getFrameForComponent(parent), message, title, DEFAULT_OPTION);
	}

	private static int showOptionDialog(Component parent, String message, String title, int optionType) {
	    OptionDialog dialog = new OptionDialog(getFrameForComponent(parent), message, title, optionType);
	    
	    dialog.setVisible(true);
	   
	    return dialog.getSelectedOption();
	}

    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 int getSelectedOption() {
        return selectedOption;
    }

    public void actionPerformed(ActionEvent ae) {
        String command = ae.getActionCommand();
        if (command.equals(Resource.getString(Resource.YES))) {
             selectedOption = YES_OPTION;
        } else if (command.equals(Resource.getString(Resource.NO))) {
             selectedOption = NO_OPTION;
        } else if (command.equals(Resource.getString(Resource.CANCEL))) {
             selectedOption = CANCEL_OPTION;
        } else if (command.equals(Resource.getString(Resource.OK))) {
             selectedOption = OK_OPTION;
        }
        dispose();
    }
    
    private static Frame getFrameForComponent(Component comp) {
        Component parent = comp;
        while (parent != null) {
            if (parent instanceof Frame) {
                break;
            }
            parent = parent.getParent();
        }
        return (Frame)parent;
    }

    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);
            }
        }
    }
}

⌨️ 快捷键说明

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