📄 basicoptionpaneui.java
字号:
/** The minimum dimensions of the JOptionPane. */ protected Dimension minimumSize; /** The propertyChangeListener for the JOptionPane. */ protected PropertyChangeListener propertyChangeListener; /** The JOptionPane this UI delegate is used for. */ protected JOptionPane optionPane; /** The size of the icons. */ // FIXME: wrong name for a constant. private static final int iconSize = 36; /** The foreground color for the message area. */ private transient Color messageForeground; /** The border around the message area. */ private transient Border messageBorder; /** The border around the button area. */ private transient Border buttonBorder; /** The string used to describe OK buttons. */ private static final String OK_STRING = "OK"; /** The string used to describe Yes buttons. */ private static final String YES_STRING = "Yes"; /** The string used to describe No buttons. */ private static final String NO_STRING = "No"; /** The string used to describe Cancel buttons. */ private static final String CANCEL_STRING = "Cancel"; /** The container for the message area. * This is package-private to avoid an accessor method. */ transient Container messageAreaContainer; /** The container for the buttons. * This is package-private to avoid an accessor method. */ transient Container buttonContainer; /** * A helper class that implements Icon. This is used temporarily until * ImageIcons are fixed. */ private static class MessageIcon implements Icon { /** * This method returns the width of the icon. * * @return The width of the icon. */ public int getIconWidth() { return iconSize; } /** * This method returns the height of the icon. * * @return The height of the icon. */ public int getIconHeight() { return iconSize; } /** * This method paints the icon as a part of the given component using the * given graphics and the given x and y position. * * @param c The component that owns this icon. * @param g The Graphics object to paint with. * @param x The x coordinate. * @param y The y coordinate. */ public void paintIcon(Component c, Graphics g, int x, int y) { // Nothing to do here. } } /** The icon displayed for ERROR_MESSAGE. */ private static MessageIcon errorIcon = new MessageIcon() { public void paintIcon(Component c, Graphics g, int x, int y) { Polygon oct = new Polygon(new int[] { 0, 0, 9, 27, 36, 36, 27, 9 }, new int[] { 9, 27, 36, 36, 27, 9, 0, 0 }, 8); g.translate(x, y); Color saved = g.getColor(); g.setColor(Color.RED); g.fillPolygon(oct); g.setColor(Color.BLACK); g.drawRect(13, 16, 10, 4); g.setColor(saved); g.translate(-x, -y); } }; /** The icon displayed for INFORMATION_MESSAGE. */ private static MessageIcon infoIcon = new MessageIcon() { public void paintIcon(Component c, Graphics g, int x, int y) { g.translate(x, y); Color saved = g.getColor(); // Should be purple. g.setColor(Color.RED); g.fillOval(0, 0, iconSize, iconSize); g.setColor(Color.BLACK); g.drawOval(16, 6, 4, 4); Polygon bottomI = new Polygon(new int[] { 15, 15, 13, 13, 23, 23, 21, 21 }, new int[] { 12, 28, 28, 30, 30, 28, 28, 12 }, 8); g.drawPolygon(bottomI); g.setColor(saved); g.translate(-x, -y); } }; /** The icon displayed for WARNING_MESSAGE. */ private static MessageIcon warningIcon = new MessageIcon() { public void paintIcon(Component c, Graphics g, int x, int y) { g.translate(x, y); Color saved = g.getColor(); g.setColor(Color.YELLOW); Polygon triangle = new Polygon(new int[] { 0, 18, 36 }, new int[] { 36, 0, 36 }, 3); g.fillPolygon(triangle); g.setColor(Color.BLACK); Polygon excl = new Polygon(new int[] { 15, 16, 20, 21 }, new int[] { 8, 26, 26, 8 }, 4); g.drawPolygon(excl); g.drawOval(16, 30, 4, 4); g.setColor(saved); g.translate(-x, -y); } }; /** The icon displayed for MESSAGE_ICON. */ private static MessageIcon questionIcon = new MessageIcon() { public void paintIcon(Component c, Graphics g, int x, int y) { g.translate(x, y); Color saved = g.getColor(); g.setColor(Color.GREEN); g.fillRect(0, 0, iconSize, iconSize); g.setColor(Color.BLACK); g.drawOval(11, 2, 16, 16); g.drawOval(14, 5, 10, 10); g.setColor(Color.GREEN); g.fillRect(0, 10, iconSize, iconSize - 10); g.setColor(Color.BLACK); g.drawLine(11, 10, 14, 10); g.drawLine(24, 10, 17, 22); g.drawLine(27, 10, 20, 22); g.drawLine(17, 22, 20, 22); g.drawOval(17, 25, 3, 3); g.setColor(saved); g.translate(-x, -y); } }; // FIXME: Uncomment when the ImageIcons are fixed. /* IconUIResource warningIcon, questionIcon, infoIcon, errorIcon;*/ /** * Creates a new BasicOptionPaneUI object. */ public BasicOptionPaneUI() { // Nothing to do here. } /** * This method is messaged to add the buttons to the given container. * * @param container The container to add components to. * @param buttons The buttons to add. (If it is an instance of component, * the Object is added directly. If it is an instance of Icon, it is * packed into a label and added. For all other cases, the string * representation of the Object is retreived and packed into a * label.) * @param initialIndex The index of the component that is the initialValue. */ protected void addButtonComponents(Container container, Object[] buttons, int initialIndex) { if (buttons == null) return; for (int i = 0; i < buttons.length; i++) { if (buttons[i] != null) { Component toAdd; if (buttons[i] instanceof Component) toAdd = (Component) buttons[i]; else { if (buttons[i] instanceof Icon) toAdd = new JButton((Icon) buttons[i]); else toAdd = new JButton(buttons[i].toString()); hasCustomComponents = true; } if (toAdd instanceof JButton) ((JButton) toAdd).addActionListener(createButtonActionListener(i)); if (i == initialIndex) initialFocusComponent = toAdd; container.add(toAdd); } } selectInitialValue(optionPane); } /** * This method adds the appropriate icon the given container. * * @param top The container to add an icon to. */ protected void addIcon(Container top) { JLabel iconLabel = null; Icon icon = getIcon(); if (icon != null) { iconLabel = new JLabel(icon); top.add(iconLabel, BorderLayout.WEST); } } /** * A helper method that returns an instance of GridBagConstraints to be used * for creating the message area. * * @return An instance of GridBagConstraints. */ private static GridBagConstraints createConstraints() { GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = GridBagConstraints.REMAINDER; constraints.gridy = GridBagConstraints.REMAINDER; constraints.gridwidth = 0; constraints.anchor = GridBagConstraints.LINE_START; constraints.fill = GridBagConstraints.NONE; constraints.insets = new Insets(0, 0, 3, 0); return constraints; } /** * This method creates the proper object (if necessary) to represent msg. * (If msg is an instance of Component, it will add it directly. If it is * an icon, then it will pack it in a label and add it. Otherwise, it gets * treated as a string. If the string is longer than maxll, a box is * created and the burstStringInto is called with the box as the container. * The box is then added to the given container. Otherwise, the string is * packed in a label and placed in the given container.) This method is * also used for adding the inputComponent to the container. * * @param container The container to add to. * @param cons The constraints when adding. * @param msg The message to add. * @param maxll The max line length. * @param internallyCreated Whether the msg is internally created. */ protected void addMessageComponents(Container container, GridBagConstraints cons, Object msg, int maxll, boolean internallyCreated) { if (msg == null) return; hasCustomComponents = internallyCreated; if (msg instanceof Object[]) { Object[] arr = (Object[]) msg; for (int i = 0; i < arr.length; i++) addMessageComponents(container, cons, arr[i], maxll, internallyCreated); return; } else if (msg instanceof Component) { container.add((Component) msg, cons); cons.gridy++; } else if (msg instanceof Icon) { container.add(new JLabel((Icon) msg), cons); cons.gridy++; } else { // Undocumented behaviour. // if msg.toString().length greater than maxll // it will create a box and burst the string. // otherwise, it will just create a label and re-call // this method with the label o.O if (msg.toString().length() > maxll) { Box tmp = new Box(BoxLayout.Y_AXIS); burstStringInto(tmp, msg.toString(), maxll); addMessageComponents(container, cons, tmp, maxll, true); } else addMessageComponents(container, cons, new JLabel(msg.toString()), maxll, true); } } /** * This method creates instances of d (recursively if necessary based on * maxll) and adds to c. * * @param c The container to add to. * @param d The string to burst. * @param maxll The max line length. */ protected void burstStringInto(Container c, String d, int maxll) { // FIXME: Verify that this is the correct behaviour. // One interpretation of the spec is that this method // should recursively call itself to create (and add) // JLabels to the container if the length of the String d // is greater than maxll. // but in practice, even with a really long string, this is // all that happens. if (d == null || c == null) return; JLabel label = new JLabel(d); c.add(label); } /** * This method returns true if the given JOptionPane contains custom * components. * * @param op The JOptionPane to check. * * @return True if the JOptionPane contains custom components. */ public boolean containsCustomComponents(JOptionPane op) { return hasCustomComponents; } /** * This method creates a button action listener for the given button index. * * @param buttonIndex The index of the button in components. * * @return A new ButtonActionListener. */ protected ActionListener createButtonActionListener(int buttonIndex) { return new ButtonActionListener(buttonIndex); } /** * This method creates the button area. * * @return A new Button Area. */ protected Container createButtonArea() { JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(createLayoutManager()); addButtonComponents(buttonPanel, getButtons(), getInitialValueIndex()); return buttonPanel; } /** * This method creates a new LayoutManager for the button area. * * @return A new LayoutManager for the button area. */ protected LayoutManager createLayoutManager() { return new ButtonAreaLayout(getSizeButtonsToSameWidth(), 6); } /** * This method creates the message area. * * @return A new message area. */ protected Container createMessageArea() { JPanel messageArea = new JPanel(); messageArea.setLayout(new BorderLayout()); addIcon(messageArea); JPanel rightSide = new JPanel(); rightSide.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); rightSide.setLayout(new GridBagLayout()); GridBagConstraints con = createConstraints(); addMessageComponents(rightSide, con, getMessage(), getMaxCharactersPerLineCount(), false); if (optionPane.getWantsInput()) { Object[] selection = optionPane.getSelectionValues(); if (selection == null) inputComponent = new JTextField(15); else if (selection.length < 20) inputComponent = new JComboBox(selection); else inputComponent = new JList(selection); if (inputComponent != null) { addMessageComponents(rightSide, con, inputComponent, getMaxCharactersPerLineCount(), false); resetSelectedValue(); selectInitialValue(optionPane); } } messageArea.add(rightSide, BorderLayout.CENTER);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -