📄 basicinternalframeui.java
字号:
public class InternalFrameLayout implements LayoutManager { /** * This method is called when the given Component is added to the * JInternalFrame. * * @param name * The name of the Component. * @param c * The Component added. */ public void addLayoutComponent(String name, Component c) { // Nothing to do here. } /** * This method is used to set the bounds of the children of the * JInternalFrame. * * @param c * The Container to lay out. */ public void layoutContainer(Container c) { Dimension dims = frame.getSize(); Insets insets = frame.getInsets(); dims.width -= insets.left + insets.right; dims.height -= insets.top + insets.bottom; frame.getRootPane().getGlassPane().setBounds(0, 0, dims.width, dims.height); int nh = 0; int sh = 0; int ew = 0; int ww = 0; if (northPane != null) { Dimension nDims = northPane.getPreferredSize(); nh = Math.min(nDims.height, dims.height); northPane.setBounds(insets.left, insets.top, dims.width, nh); } if (southPane != null) { Dimension sDims = southPane.getPreferredSize(); sh = Math.min(sDims.height, dims.height - nh); southPane.setBounds(insets.left, insets.top + dims.height - sh, dims.width, sh); } int remHeight = dims.height - sh - nh; if (westPane != null) { Dimension wDims = westPane.getPreferredSize(); ww = Math.min(dims.width, wDims.width); westPane.setBounds(insets.left, insets.top + nh, ww, remHeight); } if (eastPane != null) { Dimension eDims = eastPane.getPreferredSize(); ew = Math.min(eDims.width, dims.width - ww); eastPane.setBounds(insets.left + dims.width - ew, insets.top + nh, ew, remHeight); } int remWidth = dims.width - ww - ew; frame.getRootPane().setBounds(insets.left + ww, insets.top + nh, remWidth, remHeight); } /** * This method returns the minimum layout size. * * @param c * The Container to find a minimum layout size for. * @return The minimum dimensions for the JInternalFrame. */ public Dimension minimumLayoutSize(Container c) { return getSize(c, true); } /** * This method returns the maximum layout size. * * @param c * The Container to find a maximum layout size for. * @return The maximum dimensions for the JInternalFrame. */ public Dimension maximumLayoutSize(Container c) { return preferredLayoutSize(c); } /** * Th8is method returns the preferred layout size. * * @param c * The Container to find a preferred layout size for. * @return The preferred dimensions for the JInternalFrame. */ public Dimension preferredLayoutSize(Container c) { return getSize(c, false); } /** * DOCUMENT ME! * * @param c * DOCUMENT ME! * @param min * DOCUMENT ME! * @return DOCUMENT ME! */ private Dimension getSize(Container c, boolean min) { Insets insets = frame.getInsets(); Dimension contentDims = frame.getContentPane().getPreferredSize(); if (min) contentDims.width = contentDims.height = 0; int nWidth = 0; int nHeight = 0; int sWidth = 0; int sHeight = 0; int eWidth = 0; int eHeight = 0; int wWidth = 0; int wHeight = 0; Dimension dims; if (northPane != null) { dims = northPane.getPreferredSize(); if (dims != null) { nWidth = dims.width; nHeight = dims.height; } } if (southPane != null) { dims = southPane.getPreferredSize(); if (dims != null) { sWidth = dims.width; sHeight = dims.height; } } if (eastPane != null) { dims = eastPane.getPreferredSize(); if (dims != null) { sWidth = dims.width; sHeight = dims.height; } } if (westPane != null) { dims = westPane.getPreferredSize(); if (dims != null) { wWidth = dims.width; wHeight = dims.height; } } int width = Math.max(sWidth, nWidth); width = Math.max(width, contentDims.width + eWidth + wWidth); int height = Math.max(eHeight, wHeight); height = Math.max(height, contentDims.height); height += nHeight + sHeight; width += insets.left + insets.right; height += insets.top + insets.bottom; return new Dimension(width, height); } /** * This method is called when a Component is removed from the * JInternalFrame. * * @param c The Component that was removed. */ public void removeLayoutComponent(Component c) { // Nothing to do here. } } /** * This helper class is used to listen to the JDesktopPane's glassPane for * MouseEvents. The JInternalFrame can then be selected if a click is * detected on its children. */ protected class GlassPaneDispatcher implements MouseInputListener { /** The MouseEvent target. */ private transient Component mouseEventTarget; /** The component pressed. */ private transient Component pressedComponent; /** The last component entered. */ private transient Component lastComponentEntered; /** Used to store/reset lastComponentEntered. */ private transient Component tempComponent; /** The number of presses. */ private transient int pressCount; /** * This method is called when the mouse enters the glass pane. * * @param e * The MouseEvent. */ public void mouseEntered(MouseEvent e) { handleEvent(e); } /** * This method is called when the mouse is clicked on the glass pane. * * @param e * The MouseEvent. */ public void mouseClicked(MouseEvent e) { handleEvent(e); } /** * This method is called when the mouse is dragged in the glass pane. * * @param e * The MouseEvent. */ public void mouseDragged(MouseEvent e) { handleEvent(e); } /** * This method is called when the mouse exits the glass pane. * * @param e * The MouseEvent. */ public void mouseExited(MouseEvent e) { handleEvent(e); } /** * This method is called when the mouse is moved in the glass pane. * * @param e * The MouseEvent. */ public void mouseMoved(MouseEvent e) { handleEvent(e); } /** * This method is called when the mouse is pressed in the glass pane. * * @param e * The MouseEvent. */ public void mousePressed(MouseEvent e) { activateFrame(frame); handleEvent(e); } /** * This method is called when the mouse is released in the glass pane. * * @param e * The MouseEvent. */ public void mouseReleased(MouseEvent e) { handleEvent(e); } /** * This method acquires a candidate component to dispatch the MouseEvent to. * * @param me * The MouseEvent to acquire a component for. */ private void acquireComponentForMouseEvent(MouseEvent me) { int x = me.getX(); int y = me.getY(); // Find the candidate which should receive this event. Component parent = frame.getLayeredPane(); if (parent == null) return; Component candidate = null; Point p = me.getPoint(); while (candidate == null && parent != null) { candidate = SwingUtilities.getDeepestComponentAt(parent, p.x, p.y); if (candidate == null) { p = SwingUtilities.convertPoint(parent, p.x, p.y, parent.getParent()); parent = parent.getParent(); } } // If the only candidate we found was the native container itself, // don't dispatch any event at all. We only care about the lightweight // children here. if (candidate == frame.getContentPane()) candidate = null; // If our candidate is new, inform the old target we're leaving. if (lastComponentEntered != null && lastComponentEntered.isShowing() && lastComponentEntered != candidate) { Point tp = SwingUtilities.convertPoint(frame.getContentPane(), x, y, lastComponentEntered); MouseEvent exited = new MouseEvent(lastComponentEntered, MouseEvent.MOUSE_EXITED, me.getWhen(), me.getModifiersEx(), tp.x, tp.y, me.getClickCount(), me.isPopupTrigger(), me.getButton()); tempComponent = lastComponentEntered; lastComponentEntered = null; tempComponent.dispatchEvent(exited); } // If we have a candidate, maybe enter it. if (candidate != null) { mouseEventTarget = candidate; if (candidate.isLightweight() && candidate.isShowing() && candidate != frame.getContentPane() && candidate != lastComponentEntered) { lastComponentEntered = mouseEventTarget; Point cp = SwingUtilities.convertPoint(frame.getContentPane(), x, y, lastComponentEntered); MouseEvent entered = new MouseEvent(lastComponentEntered, MouseEvent.MOUSE_ENTERED, me.getWhen(), me.getModifiersEx(), cp.x, cp.y, me.getClickCount(), me.isPopupTrigger(), me.getButton()); lastComponentEntered.dispatchEvent(entered); } } if (me.getID() == MouseEvent.MOUSE_RELEASED || me.getID() == MouseEvent.MOUSE_PRESSED && pressCount > 0 || me.getID() == MouseEvent.MOUSE_DRAGGED) // If any of the following events occur while a button is held down, // they should be dispatched to the same component to which the // original MOUSE_PRESSED event was dispatched: // - MOUSE_RELEASED // - MOUSE_PRESSED: another button pressed while the first is held down // - MOUSE_DRAGGED mouseEventTarget = pressedComponent; else if (me.getID() == MouseEvent.MOUSE_CLICKED) { // Don't dispatch CLICKED events whose target is not the same as the // target for the original PRESSED event. if (candidate != pressedComponent) mouseEventTarget = null; else if (pressCount == 0) pressedComponent = null; } } /** * This is a helper method that dispatches the GlassPane MouseEvents to the * proper component. * * @param e * The AWTEvent to be dispatched. Usually an instance of * MouseEvent. */ private void handleEvent(AWTEvent e) { if (e instanceof MouseEvent) { MouseEvent me = (MouseEvent) e; acquireComponentForMouseEvent(me); //If there is no target, return if (mouseEventTarget == null) return; //Avoid re-dispatching to ourselves and causing an infinite loop if (mouseEventTarget.equals(frame.getGlassPane())) return; // Avoid dispatching ENTERED and EXITED events twice. if (mouseEventTarget.isShowing() && e.getID() != MouseEvent.MOUSE_ENTERED && e.getID() != MouseEvent.MOUSE_EXITED) { MouseEvent newEvt = SwingUtilities.convertMouseEvent( frame.getGlassPane(), me, mouseEventTarget); mouseEventTarget.dispatchEvent(newEvt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -