📄 basiclabelui.java
字号:
int y1 = Math.min(iconR.y, textR.y); int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height); Dimension rv = new Dimension(x2 - x1, y2 - y1); rv.width += dx; rv.height += dy; return rv; } } /** * @return getPreferredSize(c) */ public Dimension getMinimumSize(JComponent c) { Dimension d = getPreferredSize(c); View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); } return d; } /** * @return getPreferredSize(c) */ public Dimension getMaximumSize(JComponent c) { Dimension d = getPreferredSize(c); View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS); } return d; } /** * Returns the baseline. * * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public int getBaseline(JComponent c, int width, int height) { super.getBaseline(c, width, height); JLabel label = (JLabel)c; String text = label.getText(); if (text == null || "".equals(text) || label.getFont() == null) { return -1; } FontMetrics fm = label.getFontMetrics(label.getFont()); layout(label, fm, width, height); return BasicHTML.getBaseline(label, paintTextR.y, fm.getAscent(), paintTextR.width, paintTextR.height); } /** * Returns an enum indicating how the baseline of the component * changes as the size changes. * * @throws NullPointerException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public Component.BaselineResizeBehavior getBaselineResizeBehavior( JComponent c) { super.getBaselineResizeBehavior(c); if (c.getClientProperty(BasicHTML.propertyKey) != null) { return Component.BaselineResizeBehavior.OTHER; } switch(((JLabel)c).getVerticalAlignment()) { case JLabel.TOP: return Component.BaselineResizeBehavior.CONSTANT_ASCENT; case JLabel.BOTTOM: return Component.BaselineResizeBehavior.CONSTANT_DESCENT; case JLabel.CENTER: return Component.BaselineResizeBehavior.CENTER_OFFSET; } return Component.BaselineResizeBehavior.OTHER; } public void installUI(JComponent c) { installDefaults((JLabel)c); installComponents((JLabel)c); installListeners((JLabel)c); installKeyboardActions((JLabel)c); } public void uninstallUI(JComponent c) { uninstallDefaults((JLabel)c); uninstallComponents((JLabel)c); uninstallListeners((JLabel)c); uninstallKeyboardActions((JLabel)c); } protected void installDefaults(JLabel c){ LookAndFeel.installColorsAndFont(c, "Label.background", "Label.foreground", "Label.font"); LookAndFeel.installProperty(c, "opaque", Boolean.FALSE); } protected void installListeners(JLabel c){ c.addPropertyChangeListener(this); } protected void installComponents(JLabel c){ BasicHTML.updateRenderer(c, c.getText()); c.setInheritsPopupMenu(true); } protected void installKeyboardActions(JLabel l) { int dka = l.getDisplayedMnemonic(); Component lf = l.getLabelFor(); if ((dka != 0) && (lf != null)) { LazyActionMap.installLazyActionMap(l, BasicLabelUI.class, "Label.actionMap"); InputMap inputMap = SwingUtilities.getUIInputMap (l, JComponent.WHEN_IN_FOCUSED_WINDOW); if (inputMap == null) { inputMap = new ComponentInputMapUIResource(l); SwingUtilities.replaceUIInputMap(l, JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap); } inputMap.clear(); inputMap.put(KeyStroke.getKeyStroke(dka, ActionEvent.ALT_MASK, false), "press"); } else { InputMap inputMap = SwingUtilities.getUIInputMap (l, JComponent.WHEN_IN_FOCUSED_WINDOW); if (inputMap != null) { inputMap.clear(); } } } protected void uninstallDefaults(JLabel c){ } protected void uninstallListeners(JLabel c){ c.removePropertyChangeListener(this); } protected void uninstallComponents(JLabel c){ BasicHTML.updateRenderer(c, ""); } protected void uninstallKeyboardActions(JLabel c) { SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null); SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW, null); SwingUtilities.replaceUIActionMap(c, null); } public static ComponentUI createUI(JComponent c) { if (System.getSecurityManager() != null) { return SAFE_BASIC_LABEL_UI; } else { return labelUI; } } public void propertyChange(PropertyChangeEvent e) { String name = e.getPropertyName(); if (name == "text" || "font" == name || "foreground" == name) { // remove the old html view client property if one // existed, and install a new one if the text installed // into the JLabel is html source. JLabel lbl = ((JLabel) e.getSource()); String text = lbl.getText(); BasicHTML.updateRenderer(lbl, text); } else if (name == "labelFor" || name == "displayedMnemonic") { installKeyboardActions((JLabel) e.getSource()); } } // When the accelerator is pressed, temporarily make the JLabel // focusTraversable by registering a WHEN_FOCUSED action for the // release of the accelerator. Then give it focus so it can // prevent unwanted keyTyped events from getting to other components. private static class Actions extends UIAction { private static final String PRESS = "press"; private static final String RELEASE = "release"; Actions(String key) { super(key); } public void actionPerformed(ActionEvent e) { JLabel label = (JLabel)e.getSource(); String key = getName(); if (key == PRESS) { doPress(label); } else if (key == RELEASE) { doRelease(label); } } private void doPress(JLabel label) { Component labelFor = label.getLabelFor(); if (labelFor != null && labelFor.isEnabled()) { InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED); if (inputMap == null) { inputMap = new InputMapUIResource(); SwingUtilities.replaceUIInputMap(label, JComponent.WHEN_FOCUSED, inputMap); } int dka = label.getDisplayedMnemonic(); inputMap.put(KeyStroke.getKeyStroke(dka, ActionEvent.ALT_MASK, true), RELEASE); // Need this if ALT is released before the accelerator inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true), RELEASE); label.requestFocus(); } } private void doRelease(JLabel label) { Component labelFor = label.getLabelFor(); if (labelFor != null && labelFor.isEnabled()) { InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED); if (inputMap != null) { // inputMap should never be null. inputMap.remove(KeyStroke.getKeyStroke(label.getDisplayedMnemonic(), ActionEvent.ALT_MASK, true)); inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true)); } if (labelFor instanceof Container && ((Container) labelFor).isFocusCycleRoot()) { labelFor.requestFocus(); } else { SwingUtilities2.compositeRequestFocus(labelFor); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -