synthoptionpaneui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,377 行 · 第 1/4 页
JAVA
1,377 行
for (int counter = 0; counter < numChildren; counter++) { int index = (reverseButtons) ? numChildren - counter - 1 : counter; Dimension pref = children[index].getPreferredSize(); if (getSyncAllSizes()) { children[index].setBounds(x, insets.top, maxWidth, maxHeight); } else { children[index].setBounds(x, insets.top, pref.width, pref.height); } 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; int extraWidth = cInsets.left + cInsets.right; if (syncAllSizes) { 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. */ 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) { if (SynthLookAndFeel.shouldUpdateStyle(e)) { fetchStyle(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) { SynthLookAndFeel.playSound(optionPane, "OptionPane.informationSound"); } break; case JOptionPane.QUESTION_MESSAGE: if (isComingUp) { SynthLookAndFeel.playSound(optionPane, "OptionPane.questionSound"); } break; case JOptionPane.INFORMATION_MESSAGE: if (isComingUp) { SynthLookAndFeel.playSound(optionPane, "OptionPane.informationSound"); } break; case JOptionPane.WARNING_MESSAGE: if (isComingUp) { SynthLookAndFeel.playSound(optionPane, "OptionPane.warningSound"); } break; case JOptionPane.ERROR_MESSAGE: if (isComingUp) { SynthLookAndFeel.playSound(optionPane, "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); } } } } } /** * Configures any necessary colors/fonts for the specified label * used representing the message. */ private void configureMessageLabel(JLabel label) { } /** * Configures any necessary colors/fonts for the specified button * used representing the button portion of the optionpane. */ private void configureButton(JButton button) { } /** * 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. */ 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)); } } /** * This class is used to create the default buttons. */ private static class ButtonFactory { private String text; private int mnemonic; private Icon icon; ButtonFactory(String text, int mnemonic, Icon icon) { this.text = text; this.mnemonic = mnemonic; this.icon = icon; } JButton createButton() { JButton button = new JButton(text); button.setIcon(icon); if (mnemonic != 0) { button.setMnemonic(mnemonic); } return button; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?