📄 basicinternalframetitlepane.java
字号:
protected LayoutManager createLayout() { return getHandler(); } private class Handler implements LayoutManager, PropertyChangeListener { // // PropertyChangeListener // public void propertyChange(PropertyChangeEvent evt) { String prop = (String)evt.getPropertyName(); if (prop == JInternalFrame.IS_SELECTED_PROPERTY) { repaint(); return; } if (prop == JInternalFrame.IS_ICON_PROPERTY || prop == JInternalFrame.IS_MAXIMUM_PROPERTY) { setButtonIcons(); enableActions(); return; } if ("closable" == prop) { if ((Boolean)evt.getNewValue() == Boolean.TRUE) { add(closeButton); } else { remove(closeButton); } } else if ("maximizable" == prop) { if ((Boolean)evt.getNewValue() == Boolean.TRUE) { add(maxButton); } else { remove(maxButton); } } else if ("iconable" == prop) { if ((Boolean)evt.getNewValue() == Boolean.TRUE) { add(iconButton); } else { remove(iconButton); } } enableActions(); revalidate(); repaint(); } // // LayoutManager // public void addLayoutComponent(String name, Component c) {} public void removeLayoutComponent(Component c) {} public Dimension preferredLayoutSize(Container c) { return minimumLayoutSize(c); } public Dimension minimumLayoutSize(Container c) { // Calculate width. int width = 22; if (frame.isClosable()) { width += 19; } if (frame.isMaximizable()) { width += 19; } if (frame.isIconifiable()) { width += 19; } FontMetrics fm = frame.getFontMetrics(getFont()); String frameTitle = frame.getTitle(); int title_w = frameTitle != null ? SwingUtilities2.stringWidth( frame, fm, frameTitle) : 0; int title_length = frameTitle != null ? frameTitle.length() : 0; // Leave room for three characters in the title. if (title_length > 3) { int subtitle_w = SwingUtilities2.stringWidth( frame, fm, frameTitle.substring(0, 3) + "..."); width += (title_w < subtitle_w) ? title_w : subtitle_w; } else { width += title_w; } // Calculate height. Icon icon = frame.getFrameIcon(); int fontHeight = fm.getHeight(); fontHeight += 2; int iconHeight = 0; if (icon != null) { // SystemMenuBar forces the icon to be 16x16 or less. iconHeight = Math.min(icon.getIconHeight(), 16); } iconHeight += 2; int height = Math.max( fontHeight, iconHeight ); Dimension dim = new Dimension(width, height); // Take into account the border insets if any. if (getBorder() != null) { Insets insets = getBorder().getBorderInsets(c); dim.height += insets.top + insets.bottom; dim.width += insets.left + insets.right; } return dim; } public void layoutContainer(Container c) { boolean leftToRight = BasicGraphicsUtils.isLeftToRight(frame); int w = getWidth(); int h = getHeight(); int x; int buttonHeight = closeButton.getIcon().getIconHeight(); Icon icon = frame.getFrameIcon(); int iconHeight = 0; if (icon != null) { iconHeight = icon.getIconHeight(); } x = (leftToRight) ? 2 : w - 16 - 2; menuBar.setBounds(x, (h - iconHeight) / 2, 16, 16); x = (leftToRight) ? w - 16 - 2 : 2; if (frame.isClosable()) { closeButton.setBounds(x, (h - buttonHeight) / 2, 16, 14); x += (leftToRight) ? -(16 + 2) : 16 + 2; } if (frame.isMaximizable()) { maxButton.setBounds(x, (h - buttonHeight) / 2, 16, 14); x += (leftToRight) ? -(16 + 2) : 16 + 2; } if (frame.isIconifiable()) { iconButton.setBounds(x, (h - buttonHeight) / 2, 16, 14); } } } /** * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class PropertyChangeHandler implements PropertyChangeListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void propertyChange(PropertyChangeEvent evt) { getHandler().propertyChange(evt); } } /** * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class TitlePaneLayout implements LayoutManager { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void addLayoutComponent(String name, Component c) { getHandler().addLayoutComponent(name, c); } public void removeLayoutComponent(Component c) { getHandler().removeLayoutComponent(c); } public Dimension preferredLayoutSize(Container c) { return getHandler().preferredLayoutSize(c); } public Dimension minimumLayoutSize(Container c) { return getHandler().minimumLayoutSize(c); } public void layoutContainer(Container c) { getHandler().layoutContainer(c); } } /** * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class CloseAction extends AbstractAction { public CloseAction() { super(CLOSE_CMD); } public void actionPerformed(ActionEvent e) { if(frame.isClosable()) { frame.doDefaultCloseAction(); } } } // end CloseAction /** * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class MaximizeAction extends AbstractAction { public MaximizeAction() { super(MAXIMIZE_CMD); } public void actionPerformed(ActionEvent evt) { if (frame.isMaximizable()) { if (frame.isMaximum() && frame.isIcon()) { try { frame.setIcon(false); } catch (PropertyVetoException e) { } } else if (!frame.isMaximum()) { try { frame.setMaximum(true); } catch (PropertyVetoException e) { } } else { try { frame.setMaximum(false); } catch (PropertyVetoException e) { } } } } } /** * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class IconifyAction extends AbstractAction { public IconifyAction() { super(ICONIFY_CMD); } public void actionPerformed(ActionEvent e) { if(frame.isIconifiable()) { if(!frame.isIcon()) { try { frame.setIcon(true); } catch (PropertyVetoException e1) { } } else{ try { frame.setIcon(false); } catch (PropertyVetoException e1) { } } } } } // end IconifyAction /** * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class RestoreAction extends AbstractAction { public RestoreAction() { super(RESTORE_CMD); } public void actionPerformed(ActionEvent evt) { if (frame.isMaximizable() && frame.isMaximum() && frame.isIcon()) { try { frame.setIcon(false); } catch (PropertyVetoException e) { } } else if (frame.isMaximizable() && frame.isMaximum()) { try { frame.setMaximum(false); } catch (PropertyVetoException e) { } } else if (frame.isIconifiable() && frame.isIcon()) { try { frame.setIcon(false); } catch (PropertyVetoException e) { } } } } /** * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class MoveAction extends AbstractAction { public MoveAction() { super(MOVE_CMD); } public void actionPerformed(ActionEvent e) { // This action is currently undefined } } // end MoveAction /* * Handles showing and hiding the system menu. */ private class ShowSystemMenuAction extends AbstractAction { private boolean show; // whether to show the menu public ShowSystemMenuAction(boolean show) { this.show = show; } public void actionPerformed(ActionEvent e) { if (show) { windowMenu.doClick(); } else { windowMenu.setVisible(false); } } } /** * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class SizeAction extends AbstractAction { public SizeAction() { super(SIZE_CMD); } public void actionPerformed(ActionEvent e) { // This action is currently undefined } } // end SizeAction /** * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class SystemMenuBar extends JMenuBar { public boolean isFocusTraversable() { return false; } public void requestFocus() {} public void paint(Graphics g) { Icon icon = frame.getFrameIcon(); if (icon == null) { icon = (Icon)DefaultLookup.get(frame, frame.getUI(), "InternalFrame.icon"); } if (icon != null) { // Resize to 16x16 if necessary. if (icon instanceof ImageIcon && (icon.getIconWidth() > 16 || icon.getIconHeight() > 16)) { Image img = ((ImageIcon)icon).getImage(); ((ImageIcon)icon).setImage(img.getScaledInstance(16, 16, Image.SCALE_SMOOTH)); } icon.paintIcon(this, g, 0, 0); } } public boolean isOpaque() { return true; } } // end SystemMenuBar private class NoFocusButton extends JButton { private String uiKey; public NoFocusButton(String uiKey, String opacityKey) { setFocusPainted(false); setMargin(new Insets(0,0,0,0)); this.uiKey = uiKey; Object opacity = UIManager.get(opacityKey); if (opacity instanceof Boolean) { setOpaque(((Boolean)opacity).booleanValue()); } } public boolean isFocusTraversable() { return false; } public void requestFocus() {}; public AccessibleContext getAccessibleContext() { AccessibleContext ac = super.getAccessibleContext(); if (uiKey != null) { ac.setAccessibleName(UIManager.getString(uiKey)); uiKey = null; } return ac; } }; // end NoFocusButton} // End Title Pane Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -