📄 jtextcomponent.java
字号:
public void updateUI() { setUI((TextUI) UIManager.getUI(this)); } public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } public int getScrollableUnitIncrement(Rectangle visible, int orientation, int direction) { // We return 1/10 of the visible area as documented in Sun's API docs. if (orientation == SwingConstants.HORIZONTAL) return visible.width / 10; else if (orientation == SwingConstants.VERTICAL) return visible.height / 10; else throw new IllegalArgumentException("orientation must be either " + "javax.swing.SwingConstants.VERTICAL " + "or " + "javax.swing.SwingConstants.HORIZONTAL" ); } public int getScrollableBlockIncrement(Rectangle visible, int orientation, int direction) { // We return the whole visible area as documented in Sun's API docs. if (orientation == SwingConstants.HORIZONTAL) return visible.width; else if (orientation == SwingConstants.VERTICAL) return visible.height; else throw new IllegalArgumentException("orientation must be either " + "javax.swing.SwingConstants.VERTICAL " + "or " + "javax.swing.SwingConstants.HORIZONTAL" ); } /** * 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 start 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 end 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, start); 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; } /** * Read and set the content this component. If not overridden, the * method reads the component content as a plain text. * * The second parameter of this method describes the input stream. It can * be String, URL, File and so on. If not null, this object is added to * the properties of the associated document under the key * {@link Document#StreamDescriptionProperty}. * * @param input an input stream to read from. * @param streamDescription an object, describing the stream. * * @throws IOException if the reader throws it. * * @see #getDocument() * @see Document#getProperty(Object) */ public void read(Reader input, Object streamDescription) throws IOException { if (streamDescription != null) { Document d = getDocument(); if (d != null) d.putProperty(Document.StreamDescriptionProperty, streamDescription); } StringBuffer b = new StringBuffer(); int c; // Read till -1 (EOF). while ((c = input.read()) >= 0) b.append((char) c); setText(b.toString()); } /** * Write the content of this component to the given stream. If not * overridden, the method writes the component content as a plain text. * * @param output the writer to write into. * * @throws IOException if the writer throws it. */ public void write(Writer output) throws IOException { output.write(getText()); } /** * Returns the tooltip text for this text component for the given mouse * event. This forwards the call to * {@link TextUI#getToolTipText(JTextComponent, Point)}. * * @param ev the mouse event * * @return the tooltip text for this text component for the given mouse * event */ public String getToolTipText(MouseEvent ev) { return getUI().getToolTipText(this, ev.getPoint()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -