📄 basicdesktopiconui.java
字号:
/** The JInternalFrame associated with the JDesktopIcon. */ protected JInternalFrame frame; /** The MouseListener responsible for reacting to MouseEvents on the JDesktopIcon. */ private transient MouseInputListener mouseHandler; /** The Button in the JDesktopIcon responsible for deiconifying it. * This is package-private to avoid an accessor method. */ transient BoundButton button; /** The PropertyChangeListener listening to the JDesktopIcon. */ private transient PropertyChangeListener propertyHandler; /** The default icon used when no frame icon is given by the JInternalFrame. */ static Icon defaultIcon = new InternalFrameDefaultMenuIcon(); /** * This is a helper class that is used in JDesktopIcon and gives the Button a predetermined size. */ private class BoundButton extends JButton { /** * Creates a new BoundButton object. * * @param title The title of the button. */ public BoundButton(String title) { super(title); } /** * This method returns a standard size (based on the defaults of the JDesktopIcon) and the insets. * * @return The preferred size of the JDesktopIcon. */ public Dimension getPreferredSize() { Insets insets = desktopIcon.getInsets(); return new Dimension(iconWidth - insets.left - insets.right, iconHeight - insets.top - insets.bottom); } /** * This method returns the minimum size of the button. * * @return The minimum size of the button. */ public Dimension getMinimumSize() { return getPreferredSize(); } /** * This method returns the maximum size of the button. * * @return The maximum size of the button. */ public Dimension getMaximumSize() { return getPreferredSize(); } } /** * Creates a new BasicDesktopIconUI object. */ public BasicDesktopIconUI() { // Nothing to do here. } /** * This method creates a new BasicDesktopIconUI for the given JComponent. * * @param c The JComponent to create a UI for. * * @return A new BasicDesktopIconUI. */ public static ComponentUI createUI(JComponent c) { return new BasicDesktopIconUI(); } /** * This method installs the UI for the given JComponent. * * @param c The JComponent to install this UI for. */ public void installUI(JComponent c) { if (c instanceof JDesktopIcon) { desktopIcon = (JDesktopIcon) c; desktopIcon.setLayout(new BorderLayout()); frame = desktopIcon.getInternalFrame(); installDefaults(); installComponents(); installListeners(); desktopIcon.setOpaque(true); } } /** * This method uninstalls the UI for the given JComponent. * * @param c The JComponent to uninstall this UI for. */ public void uninstallUI(JComponent c) { desktopIcon.setOpaque(false); uninstallListeners(); uninstallComponents(); uninstallDefaults(); frame = null; desktopIcon.setLayout(null); desktopIcon = null; } /** * This method installs the necessary sub components for the JDesktopIcon. */ protected void installComponents() { // Try to create a button based on what the frame's // state is currently button = new BoundButton(frame.getTitle()); button.setHorizontalAlignment(SwingConstants.LEFT); button.setHorizontalTextPosition(SwingConstants.TRAILING); Icon use = frame.getFrameIcon(); if (use == null) use = defaultIcon; button.setIcon(use); desktopIcon.add(button, SwingConstants.CENTER); } /** * This method uninstalls the sub components for the JDesktopIcon. */ protected void uninstallComponents() { desktopIcon.remove(button); button = null; } /** * This method installs the listeners needed by this UI. */ protected void installListeners() { mouseHandler = createMouseInputListener(); desktopIcon.addMouseMotionListener(mouseHandler); desktopIcon.addMouseListener(mouseHandler); propertyHandler = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName().equals(JInternalFrame.TITLE_PROPERTY)) button.setText(desktopIcon.getInternalFrame().getTitle()); else if (e.getPropertyName().equals(JInternalFrame.FRAME_ICON_PROPERTY)) { Icon use = desktopIcon.getInternalFrame().getFrameIcon(); if (use == null) use = defaultIcon; button.setIcon(use); } desktopIcon.revalidate(); desktopIcon.repaint(); } }; frame.addPropertyChangeListener(propertyHandler); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { deiconize(); } }); } /** * This method uninstalls the listeners needed by the UI. */ protected void uninstallListeners() { // button is nulled so no need to remove it. frame.removePropertyChangeListener(propertyHandler); propertyHandler = null; desktopIcon.removeMouseMotionListener(mouseHandler); desktopIcon.removeMouseListener(mouseHandler); } /** * This method installs the defaults for the JDesktopIcon. */ protected void installDefaults() { // FIXME: Move border to defaults. desktopIcon.setBorder(new DesktopIconBorder()); } /** * This method uninstalls the defaults for the JDesktopIcon. */ protected void uninstallDefaults() { desktopIcon.setBorder(null); } /** * This method creates a new MouseInputListener for the JDesktopIcon. * * @return A new MouseInputListener. */ protected MouseInputListener createMouseInputListener() { return new MouseInputHandler(); } /** * This method returns the preferred size for the given JComponent. * * @param c The JComponent to find a preferred size for. * * @return The preferred size. */ public Dimension getPreferredSize(JComponent c) { return new Dimension(iconWidth, iconHeight); } /** * This method returns the minimum size for the given JComponent. * * @param c The JComponent to find a minimum size for. * * @return The minimum size. */ public Dimension getMinimumSize(JComponent c) { return getPreferredSize(c); } /** * This method returns the maximum size for the given JComponent. * * @param c The JComponent to find a maximum size for. * * @return The maximum size. */ public Dimension getMaximumSize(JComponent c) { return getPreferredSize(c); } /** * This method returns the insets of the given JComponent. * * @param c The JComponent to find insets for. * * @return The insets of the given JComponent. */ public Insets getInsets(JComponent c) { return c.getInsets(); } /** * This method deiconizes the JInternalFrame associated with the JDesktopIcon. */ public void deiconize() { try { frame.setIcon(false); } catch (PropertyVetoException pve) { // We do nothing if the attempt has been vetoed. } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -