confirmdialog.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 145 行

SVN-BASE
145
字号
/** * $Revision: $ * $Date: $ * * Copyright (C) 2006 Jive Software. All rights reserved. * * This software is published under the terms of the GNU Lesser Public License (LGPL), * a copy of which is included in this distribution. */package org.jivesoftware.spark.component;import org.jivesoftware.spark.util.ResourceUtils;import javax.swing.Icon;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/** * Implementation of a Confirm Dialog to replace the modal JOptionPane.confirm.  This is intended * for use as a yes - no dialog. * * @author Derek DeMoro */public class ConfirmDialog extends BackgroundPanel {    private JLabel message;    private JLabel iconLabel;    private JButton yesButton;    private JButton noButton;    private ConfirmListener listener = null;    private JDialog dialog;    /**     * Creates the base confirm Dialog.     */    public ConfirmDialog() {        setLayout(new GridBagLayout());        message = new JLabel();        iconLabel = new JLabel();        yesButton = new JButton();        noButton = new JButton();        add(iconLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));        add(message, new GridBagConstraints(1, 0, 4, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));        add(yesButton, new GridBagConstraints(3, 1, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));        add(noButton, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));        yesButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent actionEvent) {                if (listener != null) {                    listener.yesOption();                    listener = null;                }                dialog.dispose();            }        });        noButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent actionEvent) {                dialog.dispose();            }        });    }    /**     * Creates and displays the new confirm dialog.     *     * @param parent  the parent dialog.     * @param title   the title of this dialog.     * @param text    the main text to display.     * @param yesText the text to use on the OK or Yes button.     * @param noText  the text to use on the No button.     * @param icon    the icon to use for graphical represenation.     */    public void showConfirmDialog(JFrame parent, String title, String text, String yesText, String noText, Icon icon) {        message.setText("<html><body>" + text + "</body></html>");        iconLabel.setIcon(icon);        ResourceUtils.resButton(yesButton, yesText);        ResourceUtils.resButton(noButton, noText);        dialog = new JDialog(parent, title, false);        dialog.getContentPane().setLayout(new BorderLayout());        dialog.getContentPane().add(this);        dialog.pack();        dialog.setLocationRelativeTo(parent);        dialog.setVisible(true);        dialog.addWindowListener(new WindowAdapter() {            public void windowClosed(WindowEvent windowEvent) {                if (listener != null) {                    listener.noOption();                }            }        });    }    public void setDialogSize(int width, int height) {        dialog.setSize(width, height);        dialog.pack();        dialog.validate();    }    /**     * Sets the ConfirmListener to use with this dialog instance.     *     * @param listener the <code>ConfirmListener</code> to use with this instance.     */    public void setConfirmListener(ConfirmListener listener) {        this.listener = listener;    }    /**     * Used to handle yes/no selection in dialog. You would use this simply to     * be notified when a user has either clicked on the yes or no dialog.     */    public interface ConfirmListener {        /**         * Fired when the Yes button has been clicked.         */        void yesOption();        /**         * Fired when the No button has been clicked.         */        void noOption();    }}

⌨️ 快捷键说明

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