📄 swingutilities.java
字号:
public static boolean isMiddleMouseButton(MouseEvent event) { return ((event.getModifiersEx() & InputEvent.BUTTON2_DOWN_MASK) == InputEvent.BUTTON2_DOWN_MASK); } /** * Checks if right mouse button was clicked. * * @param event the event to check * * @return true if right mouse was clicked, false otherwise. */ public static boolean isRightMouseButton(MouseEvent event) { return ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) == InputEvent.BUTTON3_DOWN_MASK); } /** * This frame should be used when constructing a Window/JDialog without * a parent. In this case, we are forced to use this frame as a window's * parent, because we simply cannot pass null instead of parent to Window * constructor, since doing it will result in NullPointerException. */ private static class OwnerFrame extends Frame { public void setVisible(boolean b) { // Do nothing here. } public boolean isShowing() { return true; } } public static boolean notifyAction(Action action, KeyStroke ks, KeyEvent event, Object sender, int modifiers) { if (action != null && action.isEnabled()) { String name = (String) action.getValue(Action.ACTION_COMMAND_KEY); if (name == null && event.getKeyChar() != KeyEvent.CHAR_UNDEFINED) name = new String(new char[] {event.getKeyChar()}); action.actionPerformed(new ActionEvent(sender, ActionEvent.ACTION_PERFORMED, name, modifiers)); return true; } return false; } /** * <p>Change the shared, UI-managed {@link ActionMap} for a given * component. ActionMaps are arranged in a hierarchy, in order to * encourage sharing of common actions between components. The hierarchy * unfortunately places UI-managed ActionMaps at the <em>end</em> of the * parent-pointer chain, as illustrated:</p> * * <pre> * [{@link javax.swing.JComponent#getActionMap()}] * --> [{@link javax.swing.ActionMap}] * parent --> [{@link javax.swing.text.JTextComponent.KeymapActionMap}] * parent --> [{@link javax.swing.plaf.ActionMapUIResource}] * </pre> * * <p>Our goal with this method is to replace the first ActionMap along * this chain which is an instance of {@link ActionMapUIResource}, since * these are the ActionMaps which are supposed to be shared between * components.</p> * * <p>If the provided ActionMap is <code>null</code>, we interpret the * call as a request to remove the UI-managed ActionMap from the * component's ActionMap parent chain.</p> */ public static void replaceUIActionMap(JComponent component, ActionMap uiActionMap) { ActionMap child = component.getActionMap(); if (child == null) component.setActionMap(uiActionMap); else { ActionMap parent = child.getParent(); while (parent != null && !(parent instanceof ActionMapUIResource)) { child = parent; parent = child.getParent(); } child.setParent(uiActionMap); } } /** * <p>Change the shared, UI-managed {@link InputMap} for a given * component. InputMaps are arranged in a hierarchy, in order to * encourage sharing of common input mappings between components. The * hierarchy unfortunately places UI-managed InputMaps at the * <em>end</em> of the parent-pointer chain, as illustrated:</p> * * <pre> * [{@link javax.swing.JComponent#getInputMap()}] * --> [{@link javax.swing.InputMap}] * parent --> [{@link javax.swing.text.JTextComponent.KeymapWrapper}] * parent --> [{@link javax.swing.plaf.InputMapUIResource}] * </pre> * * <p>Our goal with this method is to replace the first InputMap along * this chain which is an instance of {@link InputMapUIResource}, since * these are the InputMaps which are supposed to be shared between * components.</p> * * <p>If the provided InputMap is <code>null</code>, we interpret the * call as a request to remove the UI-managed InputMap from the * component's InputMap parent chain.</p> */ public static void replaceUIInputMap(JComponent component, int condition, InputMap uiInputMap) { InputMap child = component.getInputMap(condition); if (child == null) component.setInputMap(condition, uiInputMap); else { InputMap parent = child.getParent(); while (parent != null && !(parent instanceof InputMapUIResource)) { child = parent; parent = parent.getParent(); } child.setParent(uiInputMap); } } /** * Subtracts a rectangle from another and return the area as an array * of rectangles. * Returns the areas of rectA which are not covered by rectB. * If the rectangles do not overlap, or if either parameter is * <code>null</code>, a zero-size array is returned. * @param rectA The first rectangle * @param rectB The rectangle to subtract from the first * @return An array of rectangles representing the area in rectA * not overlapped by rectB */ public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB) { if (rectA == null || rectB == null) return new Rectangle[0]; Rectangle[] r = new Rectangle[4]; int x1 = rectA.x; int y1 = rectA.y; int w1 = rectA.width; int h1 = rectA.height; int x2 = rectB.x; int y2 = rectB.y; int w2 = rectB.width; int h2 = rectB.height; // (outer box = rectA) // ------------- // |_____0_____| // | |rectB| | // |_1|_____|_2| // | 3 | // ------------- int H0 = (y2 > y1) ? y2 - y1 : 0; // height of box 0 int H3 = (y2 + h2 < y1 + h1) ? y1 + h1 - y2 - h2 : 0; // height box 3 int W1 = (x2 > x1) ? x2 - x1 : 0; // width box 1 int W2 = (x1 + w1 > x2 + w2) ? x1 + w1 - x2 - w2 : 0; // w. box 2 int H12 = (H0 + H3 < h1) ? h1 - H0 - H3 : 0; // height box 1 & 2 if (H0 > 0) r[0] = new Rectangle(x1, y1, w1, H0); else r[0] = null; if (W1 > 0 && H12 > 0) r[1] = new Rectangle(x1, y1 + H0, W1, H12); else r[1] = null; if (W2 > 0 && H12 > 0) r[2] = new Rectangle(x2 + w2, y1 + H0, W2, H12); else r[2] = null; if (H3 > 0) r[3] = new Rectangle(x1, y1 + H0 + H12, w1, H3); else r[3] = null; // sort out null objects int n = 0; for (int i = 0; i < 4; i++) if (r[i] != null) n++; Rectangle[] out = new Rectangle[n]; for (int i = 3; i >= 0; i--) if (r[i] != null) out[--n] = r[i]; return out; } /** * Calculates the intersection of two rectangles. * * @param x upper-left x coodinate of first rectangle * @param y upper-left y coodinate of first rectangle * @param w width of first rectangle * @param h height of first rectangle * @param rect a Rectangle object of the second rectangle * @throws NullPointerException if rect is null. * * @return a rectangle corresponding to the intersection of the * two rectangles. A zero rectangle is returned if the rectangles * do not overlap. */ public static Rectangle computeIntersection(int x, int y, int w, int h, Rectangle rect) { int x2 = (int) rect.getX(); int y2 = (int) rect.getY(); int w2 = (int) rect.getWidth(); int h2 = (int) rect.getHeight(); int dx = (x > x2) ? x : x2; int dy = (y > y2) ? y : y2; int dw = (x + w < x2 + w2) ? (x + w - dx) : (x2 + w2 - dx); int dh = (y + h < y2 + h2) ? (y + h - dy) : (y2 + h2 - dy); if (dw >= 0 && dh >= 0) return new Rectangle(dx, dy, dw, dh); return new Rectangle(0, 0, 0, 0); } /** * Calculates the width of a given string. * * @param fm the <code>FontMetrics</code> object to use * @param str the string * * @return the width of the the string. */ public static int computeStringWidth(FontMetrics fm, String str) { return fm.stringWidth(str); } /** * Calculates the union of two rectangles. * * @param x upper-left x coodinate of first rectangle * @param y upper-left y coodinate of first rectangle * @param w width of first rectangle * @param h height of first rectangle * @param rect a Rectangle object of the second rectangle * @throws NullPointerException if rect is null. * * @return a rectangle corresponding to the union of the * two rectangles. A rectangle encompassing both is returned if the * rectangles do not overlap. */ public static Rectangle computeUnion(int x, int y, int w, int h, Rectangle rect) { int x2 = (int) rect.getX(); int y2 = (int) rect.getY(); int w2 = (int) rect.getWidth(); int h2 = (int) rect.getHeight(); int dx = (x < x2) ? x : x2; int dy = (y < y2) ? y : y2; int dw = (x + w > x2 + w2) ? (x + w - dx) : (x2 + w2 - dx); int dh = (y + h > y2 + h2) ? (y + h - dy) : (y2 + h2 - dy); if (dw >= 0 && dh >= 0) return new Rectangle(dx, dy, dw, dh); return new Rectangle(0, 0, 0, 0); } /** * Tests if a rectangle contains another. * @param a first rectangle * @param b second rectangle * @return true if a contains b, false otherwise * @throws NullPointerException */ public static boolean isRectangleContainingRectangle(Rectangle a, Rectangle b) { // Note: zero-size rects inclusive, differs from Rectangle.contains() return b.width >= 0 && b.height >= 0 && b.width >= 0 && b.height >= 0 && b.x >= a.x && b.x + b.width <= a.x + a.width && b.y >= a.y && b.y + b.height <= a.y + a.height; } /** * Returns the InputMap that is provided by the ComponentUI of * <code>component</code> for the specified condition. * * @param component the component for which the InputMap is returned * @param cond the condition that specifies which of the three input * maps should be returned, may be * {@link JComponent#WHEN_IN_FOCUSED_WINDOW}, * {@link JComponent#WHEN_FOCUSED} or * {@link JComponent#WHEN_ANCESTOR_OF_FOCUSED_COMPONENT} * * @return The input map. */ public static InputMap getUIInputMap(JComponent component, int cond) { if (UIManager.getUI(component) != null) // we assume here that the UI class sets the parent of the component's // InputMap, which is the correct behaviour. If it's not, then // this can be considered a bug return component.getInputMap(cond).getParent(); else return null; } /** * Returns the ActionMap that is provided by the ComponentUI of * <code>component</code>. * * @param component the component for which the ActionMap is returned */ public static ActionMap getUIActionMap(JComponent component) { if (UIManager.getUI(component) != null) // we assume here that the UI class sets the parent of the component's // ActionMap, which is the correct behaviour. If it's not, then // this can be considered a bug return component.getActionMap().getParent(); else return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -