jtextcomponent.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,538 行 · 第 1/3 页

JAVA
1,538
字号
      {	return doc.getText(getSelectionStart(), getSelectionEnd());      }    catch (BadLocationException e)      {	// This should never happen.	return null;      }  }  /**   * Returns a string that specifies the name of the Look and Feel class   * that renders this component.   *   * @return the string "TextComponentUI"   */  public String getUIClassID()  {    return "TextComponentUI";  }  /**   * Returns a string representation of this JTextComponent.   */  protected String paramString()  {    return "JTextComponent";  }  /**   * This method returns the label's UI delegate.   *   * @return The label's UI delegate.   */  public TextUI getUI()  {    return (TextUI) ui;  }  /**   * This method sets the label's UI delegate.   *   * @param ui The label's UI delegate.   */  public void setUI(TextUI newUI)  {    super.setUI(newUI);  }  /**   * This method resets the label's UI delegate to the default UI for the   * current look and feel.   */  public void updateUI()  {    setUI((TextUI) UIManager.getUI(this));  }  public Dimension getPreferredScrollableViewportSize()  {    return null;  }  public int getScrollableUnitIncrement(Rectangle visible, int orientation,                                        int direction)  {    return 0;  }  public int getScrollableBlockIncrement(Rectangle visible, int orientation,                                         int direction)  {    return 0;  }  /**   * Checks whether this text component it editable.   *   * @return true if editable, false otherwise   */  public boolean isEditable()  {    return editable;  }  /**   * Enables/disabled this text component's editability.   *   * @param newValue true to make it editable, false otherwise.   */  public void setEditable(boolean newValue)  {    if (editable == newValue)      return;    boolean oldValue = editable;    editable = newValue;    firePropertyChange("editable", oldValue, newValue);  }  /**   * The <code>Caret</code> object used in this text component.   *   * @return the caret object   */  public Caret getCaret()  {    return caret;  }  /**   * Sets a new <code>Caret</code> for this text component.   *   * @param newCaret the new <code>Caret</code> to set   */  public void setCaret(Caret newCaret)  {    if (caret != null)      caret.deinstall(this);        Caret oldCaret = caret;    caret = newCaret;    if (caret != null)      caret.install(this);        firePropertyChange("caret", oldCaret, newCaret);  }  public Color getCaretColor()  {    return caretColor;  }  public void setCaretColor(Color newColor)  {    Color oldCaretColor = caretColor;    caretColor = newColor;    firePropertyChange("caretColor", oldCaretColor, newColor);  }  public Color getDisabledTextColor()  {    return disabledTextColor;  }  public void setDisabledTextColor(Color newColor)  {    Color oldColor = disabledTextColor;    disabledTextColor = newColor;    firePropertyChange("disabledTextColor", oldColor, newColor);  }  public Color getSelectedTextColor()  {    return selectedTextColor;  }  public void setSelectedTextColor(Color newColor)  {    Color oldColor = selectedTextColor;    selectedTextColor = newColor;    firePropertyChange("selectedTextColor", oldColor, newColor);  }  public Color getSelectionColor()  {    return selectionColor;  }  public void setSelectionColor(Color newColor)  {    Color oldColor = selectionColor;    selectionColor = newColor;    firePropertyChange("selectionColor", oldColor, newColor);  }  /**   * Retrisves the current caret position.   *   * @return the current position   */  public int getCaretPosition()  {    return caret.getDot();  }  /**   * Sets the caret to a new position.   *   * @param position the new position   */  public void setCaretPosition(int position)  {    if (doc == null)      return;    if (position < 0 || position > doc.getLength())      throw new IllegalArgumentException();    caret.setDot(position);  }  /**   * Moves the caret to a given position. This selects the text between   * the old and the new position of the caret.   */  public void moveCaretPosition(int position)  {    if (doc == null)      return;    if (position < 0 || position > doc.getLength())      throw new IllegalArgumentException();    caret.moveDot(position);  }  public Highlighter getHighlighter()  {    return highlighter;  }  public void setHighlighter(Highlighter newHighlighter)  {    if (highlighter != null)      highlighter.deinstall(this);        Highlighter oldHighlighter = highlighter;    highlighter = newHighlighter;    if (highlighter != null)      highlighter.install(this);        firePropertyChange("highlighter", oldHighlighter, newHighlighter);  }  /**   * Returns the start postion of the currently selected text.   *   * @return the start postion   */  public int getSelectionStart()  {    return Math.min(caret.getDot(), caret.getMark());  }  /**   * Selects the text from the given postion to the selection end position.   *   * @param end the start positon of the selected text.   */  public void setSelectionStart(int start)  {    select(start, getSelectionEnd());  }  /**   * Returns the end postion of the currently selected text.   *   * @return the end postion   */  public int getSelectionEnd()  {    return Math.max(caret.getDot(), caret.getMark());  }  /**   * Selects the text from the selection start postion to the given position.   *   * @param end the end positon of the selected text.   */  public void setSelectionEnd(int end)  {    select(getSelectionStart(), end);  }  /**   * Selects a part of the content of the text component.   *   * @param start the start position of the selected text   * @param ent the end position of the selected text   */  public void select(int start, int end)  {    int length = doc.getLength();        start = Math.max(start, 0);    start = Math.min(start, length);    end = Math.max(end, 0);    end = Math.min(end, length);    setCaretPosition(start);    moveCaretPosition(end);  }  /**   * Selects the whole content of the text component.   */  public void selectAll()  {    select(0, doc.getLength());  }  public synchronized void replaceSelection(String content)  {    int dot = caret.getDot();    int mark = caret.getMark();    // If content is empty delete selection.    if (content == null)      {	caret.setDot(dot);	return;      }    try      {	int start = getSelectionStart();	int end = getSelectionEnd();		// Remove selected text.	if (dot != mark)	  doc.remove(start, end - start);	// Insert new text.	doc.insertString(start, content, null);	// Set dot to new position.	setCaretPosition(start + content.length());      }    catch (BadLocationException e)      {	// This should never happen.      }  }  public boolean getScrollableTracksViewportHeight()  {    if (getParent() instanceof JViewport)      return ((JViewport) getParent()).getHeight() > getPreferredSize().height;    return false;  }  public boolean getScrollableTracksViewportWidth()  {    if (getParent() instanceof JViewport)      return ((JViewport) getParent()).getWidth() > getPreferredSize().width;    return false;  }  /**   * Adds a <code>CaretListener</code> object to this text component.   *   * @param listener the listener to add   */  public void addCaretListener(CaretListener listener)  {    listenerList.add(CaretListener.class, listener);  }  /**   * Removed a <code>CaretListener</code> object from this text component.   *   * @param listener the listener to remove   */  public void removeCaretListener(CaretListener listener)  {    listenerList.remove(CaretListener.class, listener);  }  /**   * Returns all added <code>CaretListener</code> objects.   *   * @return an array of listeners   */  public CaretListener[] getCaretListeners()  {    return (CaretListener[]) getListeners(CaretListener.class);  }  /**   * Notifies all registered <code>CaretListener</code> objects that the caret   * was updated.   *   * @param event the event to send   */  protected void fireCaretUpdate(CaretEvent event)  {    CaretListener[] listeners = getCaretListeners();    for (int index = 0; index < listeners.length; ++index)      listeners[index].caretUpdate(event);  }  /**   * Adds an <code>InputListener</code> object to this text component.   *   * @param listener the listener to add   */  public void addInputMethodListener(InputMethodListener listener)  {    listenerList.add(InputMethodListener.class, listener);  }  /**   * Removes an <code>InputListener</code> object from this text component.   *   * @param listener the listener to remove   */  public void removeInputMethodListener(InputMethodListener listener)  {    listenerList.remove(InputMethodListener.class, listener);  }  /**   * Returns all added <code>InputMethodListener</code> objects.   *   * @return an array of listeners   */  public InputMethodListener[] getInputMethodListeners()  {    return (InputMethodListener[]) getListeners(InputMethodListener.class);  }  public Rectangle modelToView(int position) throws BadLocationException  {    return getUI().modelToView(this, position);  }  public boolean getDragEnabled()  {    return dragEnabled;  }  public void setDragEnabled(boolean enabled)  {    dragEnabled = enabled;  }  public int viewToModel(Point pt)  {    return getUI().viewToModel(this, pt);  }  public void copy()  {    doTransferAction("copy", TransferHandler.getCopyAction());  }  public void cut()  {    doTransferAction("cut", TransferHandler.getCutAction());  }  public void paste()  {    doTransferAction("paste", TransferHandler.getPasteAction());  }  private void doTransferAction(String name, Action action)  {    // Install default TransferHandler if none set.    if (getTransferHandler() == null)      {	if (defaultTransferHandler == null)	  defaultTransferHandler = new DefaultTransferHandler();		setTransferHandler(defaultTransferHandler);      }    // Perform action.    ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,					action.getValue(Action.NAME).toString());    action.actionPerformed(event);  }  public void setFocusAccelerator(char newKey)  {    if (focusAccelerator == newKey)      return;    char oldKey = focusAccelerator;    focusAccelerator = newKey;    firePropertyChange(FOCUS_ACCELERATOR_KEY, oldKey, newKey);  }    public char getFocusAccelerator()  {    return focusAccelerator;  }  /**   * @since 1.4   */  public NavigationFilter getNavigationFilter()  {    return navigationFilter;  }  /**   * @since 1.4   */  public void setNavigationFilter(NavigationFilter filter)  {    navigationFilter = filter;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?