jcomponent.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 2,217 行 · 第 1/5 页
JAVA
2,217 行
/** * Return <code>true</code> if the component can guarantee that none of its * children will overlap in Z-order. This is a hint to the painting system. * The default is to return <code>true</code>, but some components such as * {@link JLayeredPane} should override this to return <code>false</code>. * * @return Whether the component tiles its children */ public boolean isOptimizedDrawingEnabled() { return true; } /** * Return <code>true</code> if this component is currently painting a tile. * * @return Whether the component is painting a tile */ public boolean isPaintingTile() { return false; } /** * Get the value of the {@link #requestFocusEnabled} property. * * @return The current value of the property */ public boolean isRequestFocusEnabled() { return requestFocusEnabled; } /** * Return <code>true</code> if this component is a validation root; this * will cause calls to {@link #invalidate} in this component's children * to be "captured" at this component, and not propagate to its parents. * For most components this should return <code>false</code>, but some * components such as {@link JViewPort} will want to return * <code>true</code>. * * @return Whether this component is a validation root */ public boolean isValidateRoot() { return false; } /** * <p>Paint the component. This is a delicate process, and should only be * called from the repaint thread, under control of the {@link * RepaintManager}. Client code should usually call {@link #repaint} to * trigger painting.</p> * * <p>This method will acquire a double buffer from the {@link * RepaintManager} if the component's {@link #doubleBuffered} property is * <code>true</code> and the <code>paint</code> call is the * <em>first</em> recursive <code>paint</code> call inside swing.</p> * * <p>The method will also modify the provided {@link Graphics} context * via the {@link #getComponentGraphics} method. If you want to customize * the graphics object used for painting, you should override that method * rather than <code>paint</code>.</p> * * <p>The body of the <code>paint</code> call involves calling {@link * #paintComponent}, {@link #paintBorder}, and {@link #paintChildren} in * order. If you want to customize painting behavior, you should override * one of these methods rather than <code>paint</code>.</p> * * <p>For more details on the painting sequence, see <a * href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">this * article</a>.</p> * * @param g The graphics context to paint with * * @see #paintImmediately */ public void paint(Graphics g) { Graphics g2 = g; Image doubleBuffer = null; RepaintManager rm = RepaintManager.currentManager(this); if (isDoubleBuffered() && (rm.isDoubleBufferingEnabled()) && (! Thread.holdsLock(paintLock))) { doubleBuffer = rm.getOffscreenBuffer(this, getWidth(), getHeight()); } synchronized (paintLock) { if (doubleBuffer != null) { g2 = doubleBuffer.getGraphics(); g2.setClip(g.getClipBounds()); } g2 = getComponentGraphics(g2); paintComponent(g2); paintBorder(g2); paintChildren(g2); if (doubleBuffer != null) g.drawImage(doubleBuffer, 0, 0, (ImageObserver) null); } } /** * Paint the component's border. This usually means calling {@link * Border#paintBorder} on the {@link #border} property, if it is * non-<code>null</code>. You may override this if you wish to customize * border painting behavior. The border is painted after the component's * body, but before the component's children. * * @param g The graphics context with which to paint the border * * @see #paint * @see #paintChildren * @see #paintComponent */ protected void paintBorder(Graphics g) { if (getBorder() != null) getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight()); } /** * Paint the component's children. This usually means calling {@link * Container#paint}, which recursively calls {@link #paint} on any of the * component's children, with appropriate changes to coordinate space and * clipping region. You may override this if you wish to customize * children painting behavior. The children are painted after the * component's body and border. * * @param g The graphics context with which to paint the children * * @see #paint * @see #paintBorder * @see #paintComponent */ protected void paintChildren(Graphics g) { super.paint(g); } /** * Paint the component's body. This usually means calling {@link * ComponentUI#update} on the {@link #ui} property of the component, if * it is non-<code>null</code>. You may override this if you wish to * customize the component's body-painting behavior. The component's body * is painted first, before the border and children. * * @param g The graphics context with which to paint the body * * @see #paint * @see #paintBorder * @see #paintChildren */ protected void paintComponent(Graphics g) { if (ui != null) ui.update(g, this); } /** * A variant of {@link #paintImmediately(Rectangle)} which takes * integer parameters. * * @param x The left x coordinate of the dirty region * @param y The top y coordinate of the dirty region * @param w The width of the dirty region * @param h The height of the dirty region */ public void paintImmediately(int x, int y, int w, int h) { paintImmediately(new Rectangle(x, y, w, h)); } /** * Transform the provided dirty rectangle for this component into the * appropriate ancestral {@link JRootPane} and call {@link #paint} on * that root pane. This method is called from the {@link RepaintManager} * and should always be called within the painting thread. * * @param r The dirty rectangle to paint */ public void paintImmediately(Rectangle r) { Component root = SwingUtilities.getRoot(this); if (root == null || ! root.isShowing()) return; Graphics g = root.getGraphics(); if (g == null) return; Rectangle clip = SwingUtilities.convertRectangle(this, r, root); g.setClip(clip); root.paint(g); g.dispose(); } /** * Return a string representation for this component, for use in * debugging. * * @return A string describing this component. */ protected String paramString() { StringBuffer sb = new StringBuffer(); sb.append(super.paramString()); sb.append(",alignmentX=").append(getAlignmentX()); sb.append(",alignmentY=").append(getAlignmentY()); sb.append(",border="); if (getBorder() != null) sb.append(getBorder()); sb.append(",maximumSize="); if (getMaximumSize() != null) sb.append(getMaximumSize()); sb.append(",minimumSize="); if (getMinimumSize() != null) sb.append(getMinimumSize()); sb.append(",preferredSize="); if (getPreferredSize() != null) sb.append(getPreferredSize()); return sb.toString(); } /** * A variant of {@link * #registerKeyboardAction(ActionListener,String,KeyStroke,int)} which * provides <code>null</code> for the command name. */ public void registerKeyboardAction(ActionListener act, KeyStroke stroke, int cond) { registerKeyboardAction(act, null, stroke, cond); } /* * There is some charmingly undocumented behavior sun seems to be using * to simulate the old register/unregister keyboard binding API. It's not * clear to me why this matters, but we shall endeavour to follow suit. * * Two main thing seem to be happening when you do registerKeyboardAction(): * * - no actionMap() entry gets created, just an entry in inputMap() * * - the inputMap() entry is a proxy class which invokes the the * binding's actionListener as a target, and which clobbers the command * name sent in the ActionEvent, providing the binding command name * instead. * * This much you can work out just by asking the input and action maps * what they contain after making bindings, and watching the event which * gets delivered to the recipient. Beyond that, it seems to be a * sun-private solution so I will only immitate it as much as it matters * to external observers. */ private static class ActionListenerProxy extends AbstractAction { ActionListener target; String bindingCommandName; public ActionListenerProxy(ActionListener li, String cmd) { target = li; bindingCommandName = cmd; } public void actionPerformed(ActionEvent e) { ActionEvent derivedEvent = new ActionEvent(e.getSource(), e.getID(), bindingCommandName, e.getModifiers()); target.actionPerformed(derivedEvent); } } /** * An obsolete method to register a keyboard action on this component. * You should use <code>getInputMap</code> and <code>getActionMap</code> * to fetch mapping tables from keystrokes to commands, and commands to * actions, respectively, and modify those mappings directly. * * @param anAction The action to be registered * @param aCommand The command to deliver in the delivered {@link * java.awt.ActionEvent} * @param aKeyStroke The keystroke to register on * @param aCondition One of the values {@link #UNDEFINED_CONDITION}, * {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}, {@link #WHEN_FOCUSED}, or * {@link #WHEN_IN_FOCUSED_WINDOW}, indicating the condition which must * be met for the action to be fired * * @see #unregisterKeyboardAction * @see #getConditionForKeystroke * @see #resetKeyboardActiond */ public void registerKeyboardAction(ActionListener act, String cmd, KeyStroke stroke, int cond) { getInputMap(cond).put(stroke, new ActionListenerProxy(act, cmd)); } public final void setInputMap(int condition, InputMap map) { enableEvents(AWTEvent.KEY_EVENT_MASK); switch (condition) { case WHEN_FOCUSED: inputMap_whenFocused = map; break; case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT: inputMap_whenAncestorOfFocused = map; break; case WHEN_IN_FOCUSED_WINDOW: inputMap_whenInFocusedWindow = map; break; case UNDEFINED_CONDITION: default: throw new IllegalArgumentException(); } } public final InputMap getInputMap(int condition) { enableEvents(AWTEvent.KEY_EVENT_MASK); switch (condition) { case WHEN_FOCUSED: if (inputMap_whenFocused == null) inputMap_whenFocused = new InputMap(); return inputMap_whenFocused; case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT: if (inputMap_whenAncestorOfFocused == null) inputMap_whenAncestorOfFocused = new InputMap(); return inputMap_whenAncestorOfFocused; case WHEN_IN_FOCUSED_WINDOW: if (inputMap_whenInFocusedWindow == null) inputMap_whenInFocusedWindow = new InputMap(); return inputMap_whenInFocusedWindow; case UNDEFINED_CONDITION: default: return null; } } public final InputMap getInputMap() { return getInputMap(WHEN_FOCUSED); } public final ActionMap getActionMap() { if (actionMap == null) actionMap = new ActionMap(); return actionMap; } public final void setActionMap(ActionMap map) { actionMap = map; } /** * Return the condition that determines whether a registered action * occurs in response to the specified keystroke. * * @param aKeyStroke The keystroke to return the condition of * * @return One of the values {@link #UNDEFINED_CONDITION}, {@link * #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}, {@link #WHEN_FOCUSED}, or {@link * #WHEN_IN_FOCUSED_WINDOW} * * @deprecated As of 1.3 KeyStrokes can be registered with multiple * simultaneous conditions. * * @see #registerKeyboardAction * @see #unregisterKeyboardAction * @see #resetKeyboardActiond */ public int getConditionForKeyStroke(KeyStroke ks) { if (inputMap_whenFocused != null && inputMap_whenFocused.get(ks) != null) return WHEN_FOCUSED; else if (inputMap_whenAncestorOfFocused != null && inputMap_whenAncestorOfFocused.get(ks) != null) return WHEN_ANCESTOR_OF_FOCUSED_COMPONENT; else if (inputMap_whenInFocusedWindow != null && inputMap_whenInFocusedWindow.get(ks) != null) return WHEN_IN_FOCUSED_WINDOW; else return UNDEFINED_CONDITION; } /** * Get the ActionListener (typically an {@link Action} object) which is * associated with a particular keystroke. * * @param aKeyStroke The keystroke to retrieve the action of * * @return The action associated with the specified keystroke * * @deprecated Use {@link #getActionMap()} */ public ActionListener getActionForKeyStroke(KeyStroke ks) { Object cmd = getInputMap().get(ks); if (cmd != null) { if (cmd instanceof ActionListenerProxy) return (ActionListenerProxy) cmd; else if (cmd instanceof String) return getActionMap().get(cmd); } return null; } /** * A hook for subclasses which want to customize event processing. */ protected void processComponentKeyEvent(KeyEvent e) { }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?