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

📄 joptionpane.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**
     * Brings up a confirmation dialog with the specified title.  The
     * msgtype parameter is ignored (it is used in the javax.swing package to
     * specify an icon to display).
     */
    public static void showMessageDialog(Component parent_,
	    Object message_, String title_, int msgtype_) {

	JOptionPane pane = new JOptionPane(message_, msgtype_, DEFAULT_OPTION);
	JDialog dialog = pane.createDialog(parent_, title_);
	dialog.show();
    }

    /** Returns the option the user has selected.
     */
    public Object getValue() {
	return _value;
    }

    // INSTANCE VARIABLES
    protected Object _message;
    protected int _messagetype;

    /** Determines which option buttons to display (unless an array
     * of options is explicitly specified with <code>setOptions()</code>).
     */
    protected int _optiontype;

    /** If true, an TextField will be displayed for the user to provide
     * input.
     */
    protected boolean _wantsInput = false;

    protected String _inputValue = "";

    /** Array of options to display to the user in the bottom button-panel.
     * The objects in this array can be any combination of Strings or 
     * components which are subclasses of AbstractButton.
     * Buttons are just added to the bottom button-panel; Strings are
     * wrapped in a JButton which is then added to the button-panel.
     */
    protected Object[] _options;

    /** Option that should be initially selected in <code>_options</code>.
     */
    protected Object _initialValue;

    /** The currently selected option.
     */
    protected Object _value;

    // Message types
    public static final int ERROR_MESSAGE = 100;
    public static final int INFORMATION_MESSAGE = 101;
    public static final int WARNING_MESSAGE = 102;
    public static final int QUESTION_MESSAGE = 103;
    public static final int PLAIN_MESSAGE = 104;

    // Option types
    public static final int DEFAULT_OPTION = 200;
    public static final int YES_NO_OPTION = 201;
    public static final int YES_NO_CANCEL_OPTION = 202;
    public static final int OK_CANCEL_OPTION = 203;

    // Return values
    public static final int YES_OPTION = 300;
    public static final int NO_OPTION = 301;
    public static final int CANCEL_OPTION = 302;
    public static final int OK_OPTION = 303;
    public static final int CLOSED_OPTION = 304;

    // Label values - can be changed to customize appearance
    public static String YES_LABEL = "Yes";
    public static String NO_LABEL = "No";
    public static String CANCEL_LABEL = "Cancel";
    public static String OK_LABEL = "OK";

    // Accelerator keystrokes - can be customized.
    public static int YES_ACCELERATOR = -1;
    public static int NO_ACCELERATOR = -1;
    public static int CANCEL_ACCELERATOR = -1;
    public static int OK_ACCELERATOR = -1;

    //====================================================================

    /** This is a non-static inner class used for the popup dialog that 
     * JOptionPane creates.
     */
    private class Popup
	extends JDialog
	implements ActionListener, KeyListener
    {
	/** Constructor
	 */
	Popup(Component owner_, Object message_, String title_) {

	    super();

	    // The window inherits the colors of its parent if there is one,
	    // otherwise the default colors are used.
	    if (owner_ != null) {
		super.setForeground(owner_.getForeground());
		super.setBackground(owner_.getBackground());
	    }
	    else {
		super.setForeground(Toolkit.getDefaultForeground());
		super.setBackground(Toolkit.getDefaultBackground());
	    }
	    setTitle(title_);

	    _ownerComponent = owner_;
	    setLayout(new BorderLayout());

	    JPanel northpan = new JPanel();
	    northpan.setBorder(new EmptyBorder(2,2,1,2));

	    JPanel messagepanel = new JPanel();
	    if (message_ instanceof String) {
		messagepanel.add(new JLabel((String) message_));
	    }
	    else if (message_ instanceof Object[]) {
		messagepanel.setLayout(
			new BoxLayout(getOwner(), BoxLayout.Y_AXIS));

		Object[] objects = (Object[]) message_;
		for (int i=0; i<objects.length; i++) {
		    messagepanel.add(new JLabel(objects[i].toString()));
		}
	    }
	    else {
		throw new IllegalArgumentException("illegal message type " +
		       message_.getClass().getName());
	    }

	    northpan.add(messagepanel);
	    add(northpan, BorderLayout.NORTH);

	    if (_wantsInput) {
		JPanel centerpan = new JPanel();
		centerpan.setBorder(new EmptyBorder(1, 1, 1, 1));
		_inputField = new JTextField(_inputValue, 20);
		_inputField.addActionListener(this);
		centerpan.add(_inputField);
		add(centerpan, BorderLayout.CENTER);
	    }

	    JPanel southpan = new JPanel();
	    southpan.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 1));
	
	    if (_options != null) {
		// Option buttons were explicitly specified.
		for (int i=0; i<_options.length; i++) {
		    AbstractButton button = null;
		    if (_options[i] instanceof String)
			button = new JButton((String) _options[i]);
		    else if (_options[i] instanceof AbstractButton)
			button = (AbstractButton) _options[i];
		    button.addActionListener(this);
		    southpan.add(button);
		}
	    }
	    else {
		// Decide which option buttons to display, based on the
		// value of _optiontype.
		if (_optiontype == DEFAULT_OPTION || 
			_optiontype == OK_CANCEL_OPTION) {

		    _okButton = new JButton("OK");
		    _okButton.setText(OK_LABEL);
		    _okButton.addActionListener(this);
		    southpan.add(_okButton);
		}
		if (_optiontype == YES_NO_OPTION ||
			_optiontype == YES_NO_CANCEL_OPTION) {

		    _yesButton = new JButton("Yes");
		    _yesButton.setText(YES_LABEL);
		    _yesButton.addActionListener(this);
		    southpan.add(_yesButton);
		}
		if (_optiontype == YES_NO_OPTION ||
			_optiontype == YES_NO_CANCEL_OPTION) {

		    _noButton = new JButton("No");
		    _noButton.setText(NO_LABEL);
		    _noButton.addActionListener(this);
		    southpan.add(_noButton);
		}
		if (_optiontype == YES_NO_CANCEL_OPTION ||
			_optiontype == OK_CANCEL_OPTION) {

		    _cancelButton = new JButton("Cancel");
		    _cancelButton.setText(CANCEL_LABEL);
		    _cancelButton.addActionListener(this);
		    southpan.add(_cancelButton);
		}
	    }
	    add(southpan, BorderLayout.SOUTH);

	    setSize(Toolkit.getDefaultToolkit().getScreenSize());
	    pack();
	    Dimension msgsize = messagepanel.getSize();
	    setSize(msgsize.width+8, msgsize.height+9);
	    pack();

	    /* Center the dialog over its parent component.
	     */
	    Dimension ourSize = getSize();
	    if (_ownerComponent != null) {
		Point ownerOrigin = _ownerComponent.getLocationOnScreen();
		Dimension ownerSize = _ownerComponent.getSize();
		Point ownerCenter = ownerOrigin.addOffset(
		    ownerSize.width/2, ownerSize.height/2);
		setLocation(ownerCenter.addOffset(
		    -ourSize.width/2, -ourSize.height/2));
	    }
	    else {
		/* The parent component was not specified. Center this
		 * dialog box in the middle of the screen.
		 */
		Dimension screensize =
		    Toolkit.getDefaultToolkit().getScreenSize();
		Point screenCenter = new Point(screensize.width/2,
		    screensize.height/2);
		setLocation(screenCenter.addOffset(
		    -ourSize.width/2, -ourSize.height/2));
	    }

	    // Add a KeyListener in case one or more accelerators were set.
	    addKeyListener(this);
	}

	/** Gets called when the user presses an option-button (or
	 * if ENTER is pressed while focus is in the TextField).
	 */
	public void actionPerformed(ActionEvent e_) {
	    if (_wantsInput)
		_inputValue = _inputField.getText();

	    if (_options != null) {
		// Options were specified explicitly.
		// So ignore ENTER if pressed while focus is in textfield.
		if (e_.getSource() == _inputField)
		    return;

		AbstractButton source = (AbstractButton) e_.getSource();
		for (int i=0; i < _options.length; i++) {
		    if (_options[i] instanceof String && 
			    source.getText().equals(_options[i]) ) {

			_value = _options[i];
			break;
		    }
		    else if (source == _options[i]) {
			_value = source;
			break;
		    }
		}
	    }
	    else {
		// Options were not specified explicitly.
		Object source = e_.getSource();
		if ( source == _okButton || source == _inputField) {
		    _value = new Integer(JOptionPane.OK_OPTION);
		}
		else if (source == _yesButton) {
		    _value = new Integer(JOptionPane.YES_OPTION);
		}
		else if (source == _noButton) {
		    _value = new Integer(JOptionPane.NO_OPTION);
		}
		else if (source == _cancelButton) {
		    _value = new Integer(JOptionPane.CANCEL_OPTION);
		}
	    }

	    hide();
	}

	public void keyPressed(KeyEvent e_) {
	    int key = e_.getKeyCode();
	    if (key == OK_ACCELERATOR &&
		    (_optiontype == DEFAULT_OPTION || 
		    _optiontype == OK_CANCEL_OPTION)) {

		_value = new Integer(JOptionPane.OK_OPTION);
		_inputValue = _inputField.getText();
		hide();
	    }
	    else if (key == YES_ACCELERATOR &&
		    (_optiontype == YES_NO_OPTION ||
		    _optiontype == YES_NO_CANCEL_OPTION)) {
		_value = new Integer(JOptionPane.YES_OPTION);
		_inputValue = _inputField.getText();
		hide();
	    }
	    else if (key == NO_ACCELERATOR &&
		    (_optiontype == YES_NO_OPTION ||
		    _optiontype == YES_NO_CANCEL_OPTION)) {
		_value = new Integer(JOptionPane.NO_OPTION);
		hide();
	    }
	    else if (key == CANCEL_ACCELERATOR &&
		    (_optiontype == YES_NO_CANCEL_OPTION ||
		    _optiontype == OK_CANCEL_OPTION)) {
		_value = new Integer(JOptionPane.CANCEL_OPTION);
		hide();
	    }
	}

	public void keyTyped(KeyEvent e_) {
	}

	public void keyReleased(KeyEvent e_) { }

	private Component _ownerComponent;
	private JButton _okButton;
	private JButton _yesButton;
	private JButton _noButton;
	private JButton _cancelButton;
	private JTextField _inputField = new JTextField(20);
    }
}

⌨️ 快捷键说明

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