📄 basicoptionpaneui.java
字号:
public int getPadding() { return padding; } public void setCentersChildren(boolean newValue) { centersChildren = newValue; } public boolean getCentersChildren() { return centersChildren; } 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; Dimension[] sizes = new Dimension[numChildren]; Insets insets = container.getInsets(); int counter; int yLocation = insets.top; boolean ltr = container.getComponentOrientation().isLeftToRight(); if(syncAllWidths) { int maxWidth = 0; for(counter = 0; counter < numChildren; counter++) { sizes[counter] = children[counter].getPreferredSize(); maxWidth = Math.max(maxWidth, sizes[counter].width); } int xLocation; int xOffset; if(getCentersChildren()) { xLocation = (container.getSize().width - insets.left - insets.right - (maxWidth * numChildren + (numChildren - 1) * padding)) / 2; xOffset = padding + maxWidth; } else { if(numChildren > 1) { xLocation = insets.left; xOffset = (container.getSize().width - insets.left - insets.right - (maxWidth * numChildren)) / (numChildren - 1) + maxWidth; } else { xLocation = insets.left + (container.getSize().width - insets.left - insets.right - maxWidth) / 2; xOffset = 0; } } // If right to left layout then adjust xLocation and // xOffset to start at the right side of the container // and move left. if( !ltr ) { xLocation = container.getSize().width - insets.right - (xLocation - insets.left) - maxWidth; xOffset = -xOffset; } for(counter = 0; counter < numChildren; counter++) { children[counter].setBounds(xLocation, yLocation, maxWidth, sizes[counter].height); xLocation += xOffset; } } else { int totalWidth = 0; for(counter = 0; counter < numChildren; counter++) { sizes[counter] = children[counter].getPreferredSize(); totalWidth += sizes[counter].width; } totalWidth += ((numChildren - 1) * padding); boolean cc = getCentersChildren(); int xOffset; int xLocation; if(cc) { xLocation = insets.left + (container.getSize().width - insets.left - insets.right - totalWidth) / 2; xOffset = padding; } else { if(numChildren > 1) { xOffset = (container.getSize().width - insets.left - insets.right - totalWidth) / (numChildren - 1); xLocation = insets.left; } else { xLocation = insets.left + (container.getSize().width - insets.left - insets.right - totalWidth) / 2; xOffset = 0; } } if( ltr ) { for(counter = 0; counter < numChildren; counter++) { children[counter].setBounds(xLocation, yLocation, sizes[counter].width, sizes[counter].height); xLocation += xOffset + sizes[counter].width; } } else { // If right to left layout then adjust xLocation to // start at the right side of the container. xLocation = container.getSize().width - insets.right - (xLocation - insets.left); for(counter = 0; counter < numChildren; counter++) { xLocation -= xOffset + sizes[counter].width; children[counter].setBounds(xLocation, yLocation, sizes[counter].width, sizes[counter].height); } } } } } 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; int extraWidth = cInsets.left + cInsets.right; if (syncAllWidths) { int maxWidth = 0; for(int counter = 0; counter < numChildren; counter++){ aSize = children[counter].getPreferredSize(); height = Math.max(height, aSize.height); maxWidth = Math.max(maxWidth, aSize.width); } return new Dimension(extraWidth + (maxWidth * numChildren) + (numChildren - 1) * padding, extraHeight + height); } else { int totalWidth = 0; for(int counter = 0; counter < numChildren; counter++){ aSize = children[counter].getPreferredSize(); height = Math.max(height, aSize.height); totalWidth += aSize.width; } totalWidth += ((numChildren - 1) * padding); return new Dimension(extraWidth + totalWidth, extraHeight + height); } } } return new Dimension(0, 0); } public Dimension preferredLayoutSize(Container c) { return minimumLayoutSize(c); } public void removeLayoutComponent(Component c) { } } /** * 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 class PropertyChangeHandler implements PropertyChangeListener { /** * If the source of the PropertyChangeEvent <code>e</code> equals the * optionPane and is one of the ICON_PROPERTY, MESSAGE_PROPERTY, * OPTIONS_PROPERTY or INITIAL_VALUE_PROPERTY, * validateComponent is invoked. */ public void propertyChange(PropertyChangeEvent e) { if(e.getSource() == optionPane) { // Option Pane Auditory Cue Activation // only respond to "ancestor" changes // the idea being that a JOptionPane gets a JDialog when it is // set to appear and loses it's JDialog when it is dismissed. if ("ancestor" == e.getPropertyName()) { JOptionPane op = (JOptionPane)e.getSource(); boolean isComingUp; // if the old value is null, then the JOptionPane is being // created since it didn't previously have an ancestor. if (e.getOldValue() == null) { isComingUp = true; } else { isComingUp = false; } // figure out what to do based on the message type switch (op.getMessageType()) { case JOptionPane.PLAIN_MESSAGE: if (isComingUp) { fireAudioAction("OptionPane.informationSound"); } break; case JOptionPane.QUESTION_MESSAGE: if (isComingUp) { fireAudioAction("OptionPane.questionSound"); } break; case JOptionPane.INFORMATION_MESSAGE: if (isComingUp) { fireAudioAction("OptionPane.informationSound"); } break; case JOptionPane.WARNING_MESSAGE: if (isComingUp) { fireAudioAction("OptionPane.warningSound"); } break; case JOptionPane.ERROR_MESSAGE: if (isComingUp) { fireAudioAction("OptionPane.errorSound"); } break; default: System.err.println("Undefined JOptionPane type: " + op.getMessageType()); break; } } // Visual activity String changeName = e.getPropertyName(); if(changeName.equals(JOptionPane.OPTIONS_PROPERTY) || changeName.equals(JOptionPane.INITIAL_VALUE_PROPERTY) || changeName.equals(JOptionPane.ICON_PROPERTY) || changeName.equals(JOptionPane.MESSAGE_TYPE_PROPERTY) || changeName.equals(JOptionPane.OPTION_TYPE_PROPERTY) || changeName.equals(JOptionPane.MESSAGE_PROPERTY) || changeName.equals(JOptionPane.SELECTION_VALUES_PROPERTY) || changeName.equals(JOptionPane.INITIAL_SELECTION_VALUE_PROPERTY) || changeName.equals(JOptionPane.WANTS_INPUT_PROPERTY)) { uninstallComponents(); installComponents(); optionPane.validate(); } else if (changeName.equals("componentOrientation")) { ComponentOrientation o = (ComponentOrientation)e.getNewValue(); JOptionPane op = (JOptionPane)e.getSource(); if (o != (ComponentOrientation)e.getOldValue()) { op.applyComponentOrientation(o); } } } } } /** * Utility method which contains code to fire the auditory feedback * Actions. * * @since 1.4 */ private void fireAudioAction (String actionName) { ActionMap map = optionPane.getActionMap(); if (map != null) { Action audioAction = map.get(actionName); if (audioAction != null) { // pass off firing the Action to a utility method BasicLookAndFeel lf = (BasicLookAndFeel) UIManager.getLookAndFeel(); lf.playSound(audioAction); } } } /** * Configures any necessary colors/fonts for the specified label * used representing the message. */ private void configureMessageLabel(JLabel label) { label.setForeground(UIManager.getColor( "OptionPane.messageForeground")); Font messageFont = UIManager.getFont("OptionPane.messageFont"); if (messageFont != null) { label.setFont(messageFont); } } /** * Configures any necessary colors/fonts for the specified button * used representing the button portion of the optionpane. */ private void configureButton(JButton button) { Font buttonFont = UIManager.getFont("OptionPane.buttonFont"); if (buttonFont != null) { button.setFont(buttonFont); } } /** * 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 class ButtonActionListener implements ActionListener { protected int buttonIndex; public ButtonActionListener(int buttonIndex) { this.buttonIndex = buttonIndex; } public void actionPerformed(ActionEvent e) { if (optionPane != null) { int optionType = optionPane.getOptionType(); Object[] options = optionPane.getOptions(); /* If the option pane takes input, then store the input value * if custom options were specified, if the option type is * DEFAULT_OPTION, OR if option type is set to a predefined * one and the user chose the affirmative answer. */ if (inputComponent != null) { if (options != null || optionType == JOptionPane.DEFAULT_OPTION || ((optionType == JOptionPane.YES_NO_OPTION || optionType == JOptionPane.YES_NO_CANCEL_OPTION || optionType == JOptionPane.OK_CANCEL_OPTION) && buttonIndex == 0)) { resetInputValue(); } } if (options == null) { if (optionType == JOptionPane.OK_CANCEL_OPTION && buttonIndex == 1) { optionPane.setValue(new Integer(2)); } else { optionPane.setValue(new Integer(buttonIndex)); } } else { optionPane.setValue(options[buttonIndex]); } } } } // // Classed used when optionPane.getWantsInput returns true. // /** * Listener when a JList is created to handle input from the user. */ private class ListSelectionListener extends MouseAdapter { public void mousePressed(MouseEvent e) { if (e.getClickCount() == 2) { JList list = (JList)e.getSource(); int index = list.locationToIndex(e.getPoint()); optionPane.setInputValue(list.getModel().getElementAt(index)); } } } /** * Listener when a JTextField is created to handle input from the user. */ private class TextFieldActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { optionPane.setInputValue(((JTextField)e.getSource()).getText()); } } /** * A JTextField that allows you to specify an array of KeyStrokes that * that will have their bindings processed regardless of whether or * not they are registered on the JTextField. This is used as we really * want the ActionListener to be notified so that we can push the * change to the JOptionPane, but we also want additional bindings * (those of the JRootPane) to be processed as well. */ private static class MultiplexingTextField extends JTextField { private KeyStroke[] strokes; MultiplexingTextField(int cols) { super(cols); } /** * Sets the KeyStrokes that will be additional processed for * ancestor bindings. */ void setKeyStrokes(KeyStroke[] strokes) { this.strokes = strokes; } protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) { boolean processed = super.processKeyBinding(ks, e, condition, pressed); if (processed && condition != JComponent.WHEN_IN_FOCUSED_WINDOW) { for (int counter = strokes.length - 1; counter >= 0; counter--) { if (strokes[counter].equals(ks)) { // Returning false will allow further processing // of the bindings, eg our parent Containers will get a // crack at them. return false; } } } return processed; } } // REMIND(aim,7/29/98): These actions should be broken // out into protected inner classes in the next release where // API changes are allowed /** * Registered in the ActionMap. Sets the value of the option pane * to <code>JOptionPane.CLOSED_OPTION</code>. */ private static class CloseAction extends AbstractAction { public void actionPerformed(ActionEvent e) { JOptionPane optionPane = (JOptionPane)e.getSource(); optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -