14. 74. 7. 对话框大全 extends JDialog to create your own dialog

14. 74. 7. 对话框大全 extends JDialog to create your own dialog_第1张图片

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.Border;

/*
 * DialogDemo.java requires these files: CustomDialog.java 5.png
 */
public class DialogDemo extends JPanel {
  JLabel label;
  ImageIcon icon = createImageIcon("5.png");
  JFrame frame;
  String simpleDialogDesc = "一些简单消息对话框";
  String iconDesc = "一个JOptionPane有其选择的图标";
  String moreDialogDesc = "其它一些对话框";
  CustomDialog customDialog;

  /** Creates the GUI shown inside the frame's content pane. */
  public DialogDemo(JFrame frame) {
    super(new BorderLayout());
    this.frame = frame;
    customDialog = new CustomDialog(frame, "geisel", this);
    customDialog.pack();

    // 创建一些组件.
    JPanel frequentPanel = createSimpleDialogBox();
    JPanel featurePanel = createFeatureDialogBox();
    JPanel iconPanel = createIconDialogBox();
    label = new JLabel("单击 \"显示它!\" 按钮"
        + " 到选定的对话框弹出.", JLabel.CENTER);

    // Lay them out.
    Border padding = BorderFactory.createEmptyBorder(20, 20, 5, 20);
    frequentPanel.setBorder(padding);
    featurePanel.setBorder(padding);
    iconPanel.setBorder(padding);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("简单对话框模型", null, frequentPanel,
        simpleDialogDesc); // tooltip text
    tabbedPane.addTab("更多对话框", null, featurePanel, moreDialogDesc); // tooltip
                                                                            // text
    tabbedPane.addTab("带图标对话框", null, iconPanel, iconDesc); // tooltip
                                                                  // text

    add(tabbedPane, BorderLayout.CENTER);
    add(label, BorderLayout.PAGE_END);
    label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  }

  /** 设置在框架的底部显示的文字. */
  void setLabel(String newText) {
    label.setText(newText);
  }

  /** 返回一个ImageIcon如果路径是无效的,或者为null. */
  protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = DialogDemo.class.getResource(path);
    if (imgURL != null) {
      return new ImageIcon(imgURL);
    } else {
      System.err.println("不能找到文件: " + path);
      return null;
    }
  }

  /** 创建第一个标签所示的面板. */
  private JPanel createSimpleDialogBox() {
    final int numButtons = 4;
    JRadioButton[] radioButtons = new JRadioButton[numButtons];
    final ButtonGroup group = new ButtonGroup();

    JButton showItButton = null;

    final String defaultMessageCommand = "default";
    final String yesNoCommand = "yesno";
    final String yeahNahCommand = "yeahnah";
    final String yncCommand = "ync";

    radioButtons[0] = new JRadioButton("OK (在L&F的话)");
    radioButtons[0].setActionCommand(defaultMessageCommand);

    radioButtons[1] = new JRadioButton("Yes/No (在L&F的话)");
    radioButtons[1].setActionCommand(yesNoCommand);

    radioButtons[2] = new JRadioButton("Yes/No "
        + "(程序员的话)");
    radioButtons[2].setActionCommand(yeahNahCommand);

    radioButtons[3] = new JRadioButton("Yes/No/Cancel "
        + "(程序员的话)");
    radioButtons[3].setActionCommand(yncCommand);

    for (int i = 0; i < numButtons; i++) {
      group.add(radioButtons[i]);
    }
    radioButtons[0].setSelected(true);

    showItButton = new JButton("显示它!");
    showItButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String command = group.getSelection().getActionCommand();

        // ok dialog
        if (command == defaultMessageCommand) {
          JOptionPane.showMessageDialog(frame,
              "鸡蛋不应该是绿色的。");

          // yes/no dialog
        } else if (command == yesNoCommand) {
          int n = JOptionPane.showConfirmDialog(frame,
              "你想绿鸡蛋和火腿?", "一个空洞的问题",
              JOptionPane.YES_NO_OPTION);
          if (n == JOptionPane.YES_OPTION) {
            setLabel("哇!");
          } else if (n == JOptionPane.NO_OPTION) {
            setLabel("我也是!");
          } else {
            setLabel("来吧 -- 告诉我!");
          }

          // yes/no (not in those words)
        } else if (command == yeahNahCommand) {
          Object[] options = { "是的", "不可能!" };
          int n = JOptionPane.showOptionDialog(frame,
              "你想绿鸡蛋和火腿?", "一个愚蠢的问题",
              JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
              options, options[0]);
          if (n == JOptionPane.YES_OPTION) {
            setLabel("你在开玩笑!");
          } else if (n == JOptionPane.NO_OPTION) {
            setLabel("我也不喜欢他们.");
          } else {
            setLabel("来吧 -- '坦白点!");
          }

          // yes/no/cancel (not in those words)
        } else if (command == yncCommand) {
          Object[] options = { "是的, 来吧", "不, 谢谢", "不要鸡蛋, 不要火腿!" };
          int n = JOptionPane.showOptionDialog(frame,
              "你想一些绿色鸡蛋 " + "与火腿?",
              "一个愚蠢的问题", JOptionPane.YES_NO_CANCEL_OPTION,
              JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
          if (n == JOptionPane.YES_OPTION) {
            setLabel("在这里:绿鸡蛋和火腿!");
          } else if (n == JOptionPane.NO_OPTION) {
            setLabel("OK, 只要火腿");
          } else if (n == JOptionPane.CANCEL_OPTION) {
            setLabel("嗯, 我肯定不会吃!");
          } else {
            setLabel("请告诉我你到底要什么!");
          }
        }
        return;
      }
    });

    return createPane(simpleDialogDesc + ":", radioButtons, showItButton);
  }

  /**
   * createSimpleDialogBox和createFeatureDialogBox使用,以创建一个窗格
   * 包含一个描述,单个列的单选按钮,并显示它
   */
  private JPanel createPane(String description, JRadioButton[] radioButtons,
      JButton showButton) {

    int numChoices = radioButtons.length;
    JPanel box = new JPanel();
    JLabel label = new JLabel(description);

    box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS));
    box.add(label);

    for (int i = 0; i < numChoices; i++) {
      box.add(radioButtons[i]);
    }

    JPanel pane = new JPanel(new BorderLayout());
    pane.add(box, BorderLayout.PAGE_START);
    pane.add(showButton, BorderLayout.PAGE_END);
    return pane;
  }

  /**
   *通过按钮创建一个2列单选按钮      创建一个窗格
   */
  private JPanel create2ColPane(String description,
      JRadioButton[] radioButtons, JButton showButton) {
    JLabel label = new JLabel(description);
    int numPerColumn = radioButtons.length / 2;

    JPanel grid = new JPanel(new GridLayout(0, 2));
    for (int i = 0; i < numPerColumn; i++) {
      grid.add(radioButtons[i]);
      grid.add(radioButtons[i + numPerColumn]);
    }

    JPanel box = new JPanel();
    box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS));
    box.add(label);
    grid.setAlignmentX(0.0f);
    box.add(grid);

    JPanel pane = new JPanel(new BorderLayout());
    pane.add(box, BorderLayout.PAGE_START);
    pane.add(showButton, BorderLayout.PAGE_END);

    return pane;
  }

  /*
   * 创建三选项卡所示的面板。这些对话框实现
   * showMessageDialog,但你可以指定图标(使用类似的代码)任何其他对话的一种.
   */
  private JPanel createIconDialogBox() {
    JButton showItButton = null;

    final int numButtons = 6;
    JRadioButton[] radioButtons = new JRadioButton[numButtons];
    final ButtonGroup group = new ButtonGroup();

    final String plainCommand = "plain";
    final String infoCommand = "info";
    final String questionCommand = "question";
    final String errorCommand = "error";
    final String warningCommand = "warning";
    final String customCommand = "custom";

    radioButtons[0] = new JRadioButton("普通 (无图标)");
    radioButtons[0].setActionCommand(plainCommand);

    radioButtons[1] = new JRadioButton("信息图标");
    radioButtons[1].setActionCommand(infoCommand);

    radioButtons[2] = new JRadioButton("问题图标");
    radioButtons[2].setActionCommand(questionCommand);

    radioButtons[3] = new JRadioButton("错误图标");
    radioButtons[3].setActionCommand(errorCommand);

    radioButtons[4] = new JRadioButton("警告图标");
    radioButtons[4].setActionCommand(warningCommand);

    radioButtons[5] = new JRadioButton("定制图标");
    radioButtons[5].setActionCommand(customCommand);

    for (int i = 0; i < numButtons; i++) {
      group.add(radioButtons[i]);
    }
    radioButtons[0].setSelected(true);

    showItButton = new JButton("显示!");
    showItButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String command = group.getSelection().getActionCommand();

        // no icon
        if (command == plainCommand) {
          JOptionPane.showMessageDialog(frame,
              "鸡蛋不应该是绿色的.", "一个普通的信息",
              JOptionPane.PLAIN_MESSAGE);
          // information icon
        } else if (command == infoCommand) {
          JOptionPane.showMessageDialog(frame,
              "鸡蛋不应该是绿色的.",
              "空洞的信息的对话", JOptionPane.INFORMATION_MESSAGE);

          // XXX: It doesn't make sense to make a question with
          // XXX: only one button.
          // XXX: See "Yes/No (but not in those words)" for a better solution.
          // question icon
        } else if (command == questionCommand) {
          JOptionPane.showMessageDialog(frame,
              "你不应该使用一个消息对话框" + "(像这样)\n"
                  + "对于一个问题, OK?", "空洞的问题",
              JOptionPane.QUESTION_MESSAGE);
          // error icon
        } else if (command == errorCommand) {
          JOptionPane.showMessageDialog(frame,
              "Eggs aren't supposed to be green.", "Inane error",
              JOptionPane.ERROR_MESSAGE);
          // warning icon
        } else if (command == warningCommand) {
          JOptionPane.showMessageDialog(frame,
              "Eggs aren't supposed to be green.", "Inane warning",
              JOptionPane.WARNING_MESSAGE);
          // custom icon
        } else if (command == customCommand) {
          JOptionPane.showMessageDialog(frame,
              "Eggs aren't supposed to be green.", "Inane custom dialog",
              JOptionPane.INFORMATION_MESSAGE, icon);
        }
      }
    });

    return create2ColPane(iconDesc + ":", radioButtons, showItButton);
  }

  /** Creates the panel shown by the second tab. */
  private JPanel createFeatureDialogBox() {
    final int numButtons = 5;
    JRadioButton[] radioButtons = new JRadioButton[numButtons];
    final ButtonGroup group = new ButtonGroup();

    JButton showItButton = null;

    final String pickOneCommand = "pickone";
    final String textEnteredCommand = "textfield";
    final String nonAutoCommand = "nonautooption";
    final String customOptionCommand = "customoption";
    final String nonModalCommand = "nonmodal";

    radioButtons[0] = new JRadioButton("Pick one of several choices");
    radioButtons[0].setActionCommand(pickOneCommand);

    radioButtons[1] = new JRadioButton("Enter some text");
    radioButtons[1].setActionCommand(textEnteredCommand);

    radioButtons[2] = new JRadioButton("Non-auto-closing dialog");
    radioButtons[2].setActionCommand(nonAutoCommand);

    radioButtons[3] = new JRadioButton("Input-validating dialog "
        + "(with custom message area)");
    radioButtons[3].setActionCommand(customOptionCommand);

    radioButtons[4] = new JRadioButton("Non-modal dialog");
    radioButtons[4].setActionCommand(nonModalCommand);

    for (int i = 0; i < numButtons; i++) {
      group.add(radioButtons[i]);
    }
    radioButtons[0].setSelected(true);

    showItButton = new JButton("Show it!");
    showItButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String command = group.getSelection().getActionCommand();

        // pick one of many
        if (command == pickOneCommand) {
          Object[] possibilities = { "ham", "spam", "yam" };
          String s = (String) JOptionPane.showInputDialog(frame,
              "Complete the sentence:\n" + "\"Green eggs and...\"",
              "Customized Dialog", JOptionPane.PLAIN_MESSAGE, icon,
              possibilities, "ham");

          // If a string was returned, say so.
          if ((s != null) && (s.length() > 0)) {
            setLabel("Green eggs and... " + s + "!");
            return;
          }

          // If you're here, the return value was null/empty.
          setLabel("Come on, finish the sentence!");

          // text input
        } else if (command == textEnteredCommand) {
          String s = (String) JOptionPane
              .showInputDialog(frame, "Complete the sentence:\n"
                  + "\"Green eggs and...\"", "Customized Dialog",
                  JOptionPane.PLAIN_MESSAGE, icon, null, "ham");

          // If a string was returned, say so.
          if ((s != null) && (s.length() > 0)) {
            setLabel("Green eggs and... " + s + "!");
            return;
          }

          // If you're here, the return value was null/empty.
          setLabel("Come on, finish the sentence!");

          // non-auto-closing dialog
        } else if (command == nonAutoCommand) {
          final JOptionPane optionPane = new JOptionPane(
              "The only way to close this dialog is by\n"
                  + "pressing one of the following buttons.\n"
                  + "Do you understand?", JOptionPane.QUESTION_MESSAGE,
              JOptionPane.YES_NO_OPTION);

          // You can't use pane.createDialog() because that
          // method sets up the JDialog with a property change
          // listener that automatically closes the window
          // when a button is clicked.
          final JDialog dialog = new JDialog(frame, "Click a button", true);
          dialog.setContentPane(optionPane);
          dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
          dialog.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
              setLabel("Thwarted user attempt to close window.");
            }
          });
          optionPane.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
              String prop = e.getPropertyName();

              if (dialog.isVisible() && (e.getSource() == optionPane)
                  && (JOptionPane.VALUE_PROPERTY.equals(prop))) {
                // If you were going to check something
                // before closing the window, you'd do
                // it here.
                dialog.setVisible(false);
              }
            }
          });
          dialog.pack();
          dialog.setLocationRelativeTo(frame);
          dialog.setVisible(true);

          int value = ((Integer) optionPane.getValue()).intValue();
          if (value == JOptionPane.YES_OPTION) {
            setLabel("Good.");
          } else if (value == JOptionPane.NO_OPTION) {
            setLabel("Try using the window decorations "
                + "to close the non-auto-closing dialog. " + "You can't!");
          } else {
            setLabel("Window unavoidably closed (ESC?).");
          }

          // non-auto-closing dialog with custom message area
          // NOTE: if you don't intend to check the input,
          // then just use showInputDialog instead.
        } else if (command == customOptionCommand) {
          customDialog.setLocationRelativeTo(frame);
          customDialog.setVisible(true);

          String s = customDialog.getValidatedText();
          if (s != null) {
            // The text is valid.
            setLabel("Congratulations!  " + "You entered \"" + s + "\".");
          }

          // non-modal dialog
        } else if (command == nonModalCommand) {
          // Create the dialog.
          final JDialog dialog = new JDialog(frame, "A Non-Modal Dialog");

          // Add contents to it. It must have a close button,
          // since some L&Fs (notably Java/Metal) don't provide one
          // in the window decorations for dialogs.
          JLabel label = new JLabel("<html><p align=center>"
              + "This is a non-modal dialog.<br>"
              + "You can have one or more of these up<br>"
              + "and still use the main window.");
          label.setHorizontalAlignment(JLabel.CENTER);
          Font font = label.getFont();
          label.setFont(label.getFont().deriveFont(font.PLAIN, 14.0f));

          JButton closeButton = new JButton("Close");
          closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              dialog.setVisible(false);
              dialog.dispose();
            }
          });
          JPanel closePanel = new JPanel();
          closePanel.setLayout(new BoxLayout(closePanel, BoxLayout.LINE_AXIS));
          closePanel.add(Box.createHorizontalGlue());
          closePanel.add(closeButton);
          closePanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 5));

          JPanel contentPane = new JPanel(new BorderLayout());
          contentPane.add(label, BorderLayout.CENTER);
          contentPane.add(closePanel, BorderLayout.PAGE_END);
          contentPane.setOpaque(true);
          dialog.setContentPane(contentPane);

          // Show it.
          dialog.setSize(new Dimension(300, 150));
          dialog.setLocationRelativeTo(frame);
          dialog.setVisible(true);
        }
      }
    });

    return createPane(moreDialogDesc + ":", radioButtons, showItButton);
  }

  /**
   * Create the GUI and show it. For thread safety, this method should be
   * invoked from the event-dispatching thread.
   */
  private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("DialogDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    DialogDemo newContentPane = new DialogDemo(frame);
    newContentPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(newContentPane);

    // Display the window.
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }

}


/* 1.4 example used by DialogDemo.java. */
class CustomDialog extends JDialog implements ActionListener,
    PropertyChangeListener {
  private String typedText = null;
  private JTextField textField;
  private DialogDemo dd;

  private String magicWord;
  private JOptionPane optionPane;

  private String btnString1 = "Enter";
  private String btnString2 = "Cancel";

  /**
   * Returns null if the typed string was invalid; otherwise, returns the string
   * as the user entered it.
   */
  public String getValidatedText() {
    return typedText;
  }

  /** Creates the reusable dialog. */
  public CustomDialog(Frame aFrame, String aWord, DialogDemo parent) {
    super(aFrame, true);
    dd = parent;

    magicWord = aWord.toUpperCase();
    setTitle("Quiz");

    textField = new JTextField(10);

    // Create an array of the text and components to be displayed.
    String msgString1 = "What was Dr. SEUSS's real last name?";
    String msgString2 = "(The answer is \"" + magicWord + "\".)";
    Object[] array = { msgString1, msgString2, textField };

    // Create an array specifying the number of dialog buttons
    // and their text.
    Object[] options = { btnString1, btnString2 };

    // Create the JOptionPane.
    optionPane = new JOptionPane(array, JOptionPane.QUESTION_MESSAGE,
        JOptionPane.YES_NO_OPTION, null, options, options[0]);

    // Make this dialog display it.
    setContentPane(optionPane);

    // Handle window closing correctly.
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        /*
         * Instead of directly closing the window, we're going to change the
         * JOptionPane's value property.
         */
        optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
      }
    });

    // Ensure the text field always gets the first focus.
    addComponentListener(new ComponentAdapter() {
      public void componentShown(ComponentEvent ce) {
        textField.requestFocusInWindow();
      }
    });

    // Register an event handler that puts the text into the option pane.
    textField.addActionListener(this);

    // Register an event handler that reacts to option pane state changes.
    optionPane.addPropertyChangeListener(this);
  }

  /** This method handles events for the text field. */
  public void actionPerformed(ActionEvent e) {
    optionPane.setValue(btnString1);
  }

  /** This method reacts to state changes in the option pane. */
  public void propertyChange(PropertyChangeEvent e) {
    String prop = e.getPropertyName();

    if (isVisible()
        && (e.getSource() == optionPane)
        && (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY
            .equals(prop))) {
      Object value = optionPane.getValue();

      if (value == JOptionPane.UNINITIALIZED_VALUE) {
        // ignore reset
        return;
      }

      // Reset the JOptionPane's value.
      // If you don't do this, then if the user
      // presses the same button next time, no
      // property change event will be fired.
      optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);

      if (btnString1.equals(value)) {
        typedText = textField.getText();
        String ucText = typedText.toUpperCase();
        if (magicWord.equals(ucText)) {
          // we're done; clear and dismiss the dialog
          clearAndHide();
        } else {
          // text was invalid
          textField.selectAll();
          JOptionPane.showMessageDialog(CustomDialog.this, "Sorry, \""
              + typedText + "\" " + "isn't a valid response.\n"
              + "Please enter " + magicWord + ".", "Try again",
              JOptionPane.ERROR_MESSAGE);
          typedText = null;
          textField.requestFocusInWindow();
        }
      } else { // user closed dialog or clicked cancel
        dd.setLabel("It's OK.  " + "We won't force you to type " + magicWord
            + ".");
        typedText = null;
        clearAndHide();
      }
    }
  }

  /** This method clears the dialog and hides it. */
  public void clearAndHide() {
    textField.setText(null);
    setVisible(false);
  }
}



你可能感兴趣的:(14. 74. 7. 对话框大全 extends JDialog to create your own dialog)