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

📄 optiondialog.java

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

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
///import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import jp.co.ntl.preference.PreferenceInfo;
import jp.co.ntl.Util;

public class OptionDialog extends JDialog implements ActionListener, MessageConstants {
    /**
	 * 
	 */
	private static final long serialVersionUID = -1770055462355953873L;
	/** 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 JButton[]      			buttons;
	
    private int						selectedOption;
	
	public OptionDialog(Frame parent, int msgID, String[] params, String title, int optionType) {
	    super(parent, title, true);
	    
    	Resource.load(Util.getCurrentLocale());
	    
	    int	clientKind = Util.getPreferenceInfo().getClientKind();
	    
	    if (title.equals("")) {
	    	setUndecorated(true);
	    }
	    
	    Container container = getContentPane();
	    container.setLayout(new BorderLayout());
    	setResizable(false);	    

        switch (optionType) {
        case DEFAULT_OPTION:
            buttons = new JButton[1];
            buttons[0] = new JButton(Resource.getString(Resource.OK));
            buttons[0].addActionListener(this);
            break;
        case YES_NO_OPTION:
            buttons = new JButton[2];
            if (clientKind == PreferenceInfo.CLIENT_MAC) {
            	buttons[0] = new JButton(Resource.getString(Resource.NO));
            	buttons[1] = new JButton(Resource.getString(Resource.YES));
            } else {
            	buttons[0] = new JButton(Resource.getString(Resource.YES));
            	buttons[1] = new JButton(Resource.getString(Resource.NO));
            }
            buttons[0].addActionListener(this);
            buttons[1].addActionListener(this);
            break;
        case YES_NO_CANCEL_OPTION:
            buttons = new JButton[3];
            if (clientKind == PreferenceInfo.CLIENT_MAC) {
            	buttons[0] = new JButton(Resource.getString(Resource.NO));
            	buttons[1] = new JButton(Resource.getString(Resource.CANCEL));
            	buttons[2] = new JButton(Resource.getString(Resource.YES));            	
            } else {
            	buttons[0] = new JButton(Resource.getString(Resource.YES));
            	buttons[1] = new JButton(Resource.getString(Resource.NO));
            	buttons[2] = new JButton(Resource.getString(Resource.CANCEL));
            }
            buttons[0].addActionListener(this);
            buttons[1].addActionListener(this);
            buttons[2].addActionListener(this);
            break;
        case OK_CANCEL_OPTION:
            buttons = new JButton[2];
            if (clientKind == PreferenceInfo.CLIENT_MAC) {
            	buttons[0] = new JButton(Resource.getString(Resource.CANCEL));
            	buttons[1] = new JButton(Resource.getString(Resource.OK));
            } else {
            	buttons[0] = new JButton(Resource.getString(Resource.OK));
            	buttons[1] = new JButton(Resource.getString(Resource.CANCEL));
            }
            buttons[0].addActionListener(this);
            buttons[1].addActionListener(this);
            break;
        }
        
        String	message = MsgUtil.getMessage(msgID, params);
        
        JPanel	msgPanel = new JPanel(new BorderLayout());
        MultiLineLabel	msgBoard = new MultiLineLabel(message, getFont(), getForeground(), getBackground(), MultiLineLabel.LEFT);
        msgPanel.add(msgBoard, BorderLayout.CENTER);
        container.add(msgPanel);

        JPanel	btnPanel = new JPanel();
        btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
        btnPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        btnPanel.add(Box.createHorizontalGlue());
        
        for (int i = 0; i < buttons.length; i++) {
        	btnPanel.add(buttons[i]);
        	if (i != buttons.length - 1) {
        		btnPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        	}
        }
        container.add(btnPanel, BorderLayout.SOUTH);
        pack();
        
        Dimension	minimumSize = getMinimumSize();
        Dimension	dim = new Dimension();
        
        if (minimumSize.width > getWidth()) {
        	dim.width = minimumSize.width;
        } else {
        	dim.width = getWidth();
        }
        if (minimumSize.height > getHeight()) {
        	dim.height = minimumSize.height;
        } else {
        	dim.height = getHeight();
        }
        
        Dimension screenSize = getToolkit().getScreenSize();
        setBounds((screenSize.width - dim.width) / 2, 
                    (screenSize.height - dim.height) / 2, 
                    dim.width, dim.height);

        addWindowListener(new WindowAdapter() {
    		public void windowClosing(WindowEvent we) {
                selectedOption = CLOSED_OPTION;                
    			dispose();
    		}
    	});
    }
	
	public static int showConfirmDialog(Component parent, int msgID, String[] params, String title, int optionType) {
	    return showOptionDialog(getFrameForComponent(parent), msgID, params, title, optionType);
	}

	public static int showMessageDialog(Component parent, int msgID, String[] params, String title) {
	    return showOptionDialog(getFrameForComponent(parent), msgID, params, title, DEFAULT_OPTION);
	}

	private static int showOptionDialog(Component parent, int msgID, String[] params, String title, int optionType) {
	    OptionDialog dialog = new OptionDialog(getFrameForComponent(parent), msgID, params, 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 Dimension getMinimumSize() {
    	return new Dimension(300, 50);
    }

    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();
    }
    
    public static Frame getFrameForComponent(Component comp) {
        Component parent = comp;
        while (parent != null) {
            if (parent instanceof Frame) {
                break;
            }
            parent = parent.getParent();
        }
        return (Frame)parent;
    }
    
    public void paint(Graphics g) {
    	super.paint(g);
    	
    	if (getTitle().equals("")) {
    		g.draw3DRect(0, 0, getWidth() - 1, getHeight() - 1, true);
    	}
    }
}

⌨️ 快捷键说明

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