basicoptionpaneui.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,408 行 · 第 1/4 页
JAVA
1,408 行
int padSize; padSize = (numButtons <= 2? 8 : 4); for(int counter = 0; counter < numButtons; counter++) { aButton = createdButtons[counter]; aButton.setMargin(new Insets(2, padSize, 2, padSize)); } } } } protected ActionListener createButtonActionListener(int buttonIndex) { return new ButtonActionListener(buttonIndex); } /** * Returns the buttons to display from the JOptionPane the receiver is * providing the look and feel for. If the JOptionPane has options * set, they will be provided, otherwise if the optionType is * YES_NO_OPTION, yesNoOptions is returned, if the type is * YES_NO_CANCEL_OPTION yesNoCancelOptions is returned, otherwise * defaultButtons are returned. */ protected Object[] getButtons() { if (optionPane != null) { Object[] suppliedOptions = optionPane.getOptions(); if (suppliedOptions == null) { Object[] defaultOptions; int type = optionPane.getOptionType(); Locale l = optionPane.getLocale(); if (type == JOptionPane.YES_NO_OPTION) { defaultOptions = new ButtonFactory[2]; defaultOptions[0] = new ButtonFactory( UIManager.getString("OptionPane.yesButtonText", l), getMnemonic("OptionPane.yesButtonMnemonic", l), (Icon)DefaultLookup.get(optionPane, this, "OptionPane.yesIcon")); defaultOptions[1] = new ButtonFactory( UIManager.getString("OptionPane.noButtonText", l), getMnemonic("OptionPane.noButtonMnemonic", l), (Icon)DefaultLookup.get(optionPane, this, "OptionPane.noIcon")); } else if (type == JOptionPane.YES_NO_CANCEL_OPTION) { defaultOptions = new ButtonFactory[3]; defaultOptions[0] = new ButtonFactory( UIManager.getString("OptionPane.yesButtonText", l), getMnemonic("OptionPane.yesButtonMnemonic", l), (Icon)DefaultLookup.get(optionPane, this, "OptionPane.yesIcon")); defaultOptions[1] = new ButtonFactory( UIManager.getString("OptionPane.noButtonText",l), getMnemonic("OptionPane.noButtonMnemonic", l), (Icon)DefaultLookup.get(optionPane, this, "OptionPane.noIcon")); defaultOptions[2] = new ButtonFactory( UIManager.getString("OptionPane.cancelButtonText",l), getMnemonic("OptionPane.cancelButtonMnemonic", l), (Icon)DefaultLookup.get(optionPane, this, "OptionPane.cancelIcon")); } else if (type == JOptionPane.OK_CANCEL_OPTION) { defaultOptions = new ButtonFactory[2]; defaultOptions[0] = new ButtonFactory( UIManager.getString("OptionPane.okButtonText",l), getMnemonic("OptionPane.okButtonMnemonic", l), (Icon)DefaultLookup.get(optionPane, this, "OptionPane.okIcon")); defaultOptions[1] = new ButtonFactory( UIManager.getString("OptionPane.cancelButtonText",l), getMnemonic("OptionPane.cancelButtonMnemonic", l), (Icon)DefaultLookup.get(optionPane, this, "OptionPane.cancelIcon")); } else { defaultOptions = new ButtonFactory[1]; defaultOptions[0] = new ButtonFactory( UIManager.getString("OptionPane.okButtonText",l), getMnemonic("OptionPane.okButtonMnemonic", l), (Icon)DefaultLookup.get(optionPane, this, "OptionPane.okIcon")); } return defaultOptions; } return suppliedOptions; } return null; } private int getMnemonic(String key, Locale l) { String value = (String)UIManager.get(key, l); if (value == null) { return 0; } try { return Integer.parseInt(value); } catch (NumberFormatException nfe) { } return 0; } /** * Returns true, basic L&F wants all the buttons to have the same * width. */ protected boolean getSizeButtonsToSameWidth() { return true; } /** * Returns the initial index into the buttons to select. The index * is calculated from the initial value from the JOptionPane and * options of the JOptionPane or 0. */ protected int getInitialValueIndex() { if (optionPane != null) { Object iv = optionPane.getInitialValue(); Object[] options = optionPane.getOptions(); if(options == null) { return 0; } else if(iv != null) { for(int counter = options.length - 1; counter >= 0; counter--){ if(options[counter].equals(iv)) return counter; } } } return -1; } /** * Sets the input value in the option pane the receiver is providing * the look and feel for based on the value in the inputComponent. */ protected void resetInputValue() { if(inputComponent != null && (inputComponent instanceof JTextField)) { optionPane.setInputValue(((JTextField)inputComponent).getText()); } else if(inputComponent != null && (inputComponent instanceof JComboBox)) { optionPane.setInputValue(((JComboBox)inputComponent) .getSelectedItem()); } else if(inputComponent != null) { optionPane.setInputValue(((JList)inputComponent) .getSelectedValue()); } } /** * If inputComponent is non-null, the focus is requested on that, * otherwise request focus on the default value */ public void selectInitialValue(JOptionPane op) { if (inputComponent != null) inputComponent.requestFocus(); else { if (initialFocusComponent != null) initialFocusComponent.requestFocus(); if (initialFocusComponent instanceof JButton) { JRootPane root = SwingUtilities.getRootPane(initialFocusComponent); if (root != null) { root.setDefaultButton((JButton)initialFocusComponent); } } } } /** * Returns true if in the last call to validateComponent the message * or buttons contained a subclass of Component. */ public boolean containsCustomComponents(JOptionPane op) { return hasCustomComponents; } /** * <code>ButtonAreaLayout</code> behaves in a similar manner to * <code>FlowLayout</code>. It lays out all components from left to * right. If <code>syncAllWidths</code> is true, the widths of each * component will be set to the largest preferred size width. * * This inner class is marked "public" due to a compiler bug. * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of BasicOptionPaneUI. */ public static class ButtonAreaLayout implements LayoutManager { protected boolean syncAllWidths; protected int padding; /** If true, children are lumped together in parent. */ protected boolean centersChildren; private int orientation; private boolean reverseButtons; /** * Indicates whether or not centersChildren should be used vs * the orientation. This is done for backward compatability * for subclassers. */ private boolean useOrientation; public ButtonAreaLayout(boolean syncAllWidths, int padding) { this.syncAllWidths = syncAllWidths; this.padding = padding; centersChildren = true; useOrientation = false; } ButtonAreaLayout(boolean syncAllSizes, int padding, int orientation, boolean reverseButtons) { this(syncAllSizes, padding); useOrientation = true; this.orientation = orientation; this.reverseButtons = reverseButtons; } public void setSyncAllWidths(boolean newValue) { syncAllWidths = newValue; } public boolean getSyncAllWidths() { return syncAllWidths; } public void setPadding(int newPadding) { this.padding = newPadding; } public int getPadding() { return padding; } public void setCentersChildren(boolean newValue) { centersChildren = newValue; useOrientation = false; } public boolean getCentersChildren() { return centersChildren; } private int getOrientation(Container container) { if (!useOrientation) { return SwingConstants.CENTER; } if (container.getComponentOrientation().isLeftToRight()) { return orientation; } switch (orientation) { case SwingConstants.LEFT: return SwingConstants.RIGHT; case SwingConstants.RIGHT: return SwingConstants.LEFT; case SwingConstants.CENTER: return SwingConstants.CENTER; } return SwingConstants.LEFT; } public void addLayoutComponent(String string, Component comp) { } public void layoutContainer(Container container) { Component[] children = container.getComponents(); if(children != null && children.length > 0) { int numChildren = children.length; Insets insets = container.getInsets(); int maxWidth = 0; int maxHeight = 0; int totalButtonWidth = 0; int x = 0; int xOffset = 0; boolean ltr = container.getComponentOrientation(). isLeftToRight(); boolean reverse = (ltr) ? reverseButtons : !reverseButtons; for(int counter = 0; counter < numChildren; counter++) { Dimension pref = children[counter].getPreferredSize(); maxWidth = Math.max(maxWidth, pref.width); maxHeight = Math.max(maxHeight, pref.height); totalButtonWidth += pref.width; } if (getSyncAllWidths()) { totalButtonWidth = maxWidth * numChildren; } totalButtonWidth += (numChildren - 1) * padding; switch (getOrientation(container)) { case SwingConstants.LEFT: x = insets.left; break; case SwingConstants.RIGHT: x = container.getWidth() - insets.right - totalButtonWidth; break; case SwingConstants.CENTER: if (getCentersChildren() || numChildren < 2) { x = (container.getWidth() - totalButtonWidth) / 2; } else { x = insets.left; if (getSyncAllWidths()) { xOffset = (container.getWidth() - insets.left - insets.right - totalButtonWidth) / (numChildren - 1) + maxWidth; } else { xOffset = (container.getWidth() - insets.left - insets.right - totalButtonWidth) / (numChildren - 1); } } break; } for (int counter = 0; counter < numChildren; counter++) { int index = (reverse) ? numChildren - counter - 1 : counter; Dimension pref = children[index].getPreferredSize(); if (getSyncAllWidths()) { children[index].setBounds(x, insets.top, maxWidth, maxHeight); } else { children[index].setBounds(x, insets.top, pref.width, pref.height); } if (xOffset != 0) { x += xOffset; } else { x += children[index].getWidth() + padding; } } } } public Dimension minimumLayoutSize(Container c) { if(c != null) { Component[] children = c.getComponents(); if(children != null && children.length > 0) { Dimension aSize; int numChildren = children.length; int height = 0; Insets cInsets = c.getInsets(); int extraHeight = cInsets.top + cInsets.bottom;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?