📄 jtextarea.java
字号:
* row settings if they are explicitly set, or fall back to * the superclass's behaviour. * * @return the preferred size of that text component in the case * it is embedded within a JScrollPane */ public Dimension getPreferredScrollableViewportSize() { if ((rows > 0) && (columns > 0)) return new Dimension(columns * getColumnWidth(), rows * getRowHeight()); else return super.getPreferredScrollableViewportSize(); } /** * Returns the UI class ID string. * * @return the string "TextAreaUI" */ public String getUIClassID() { return "TextAreaUI"; } /** * Returns the current number of columns. * * @return number of columns */ public int getColumns() { return columns; } /** * Sets the number of rows. * * @param columns number of columns * * @exception IllegalArgumentException if columns is negative */ public void setColumns(int columns) { if (columns < 0) throw new IllegalArgumentException(); if (columns != this.columns) { this.columns = columns; revalidate(); } } /** * Returns the current number of rows. * * @return number of rows */ public int getRows() { return rows; } /** * Sets the number of rows. * * @param rows number of rows * * @exception IllegalArgumentException if rows is negative */ public void setRows(int rows) { if (rows < 0) throw new IllegalArgumentException(); if (rows != this.rows) { this.rows = rows; revalidate(); } } /** * Checks whether line wrapping is enabled. * * @return <code>true</code> if line wrapping is enabled, * <code>false</code> otherwise */ public boolean getLineWrap() { return lineWrap; } /** * Enables/disables line wrapping. * * @param flag <code>true</code> to enable line wrapping, * <code>false</code> otherwise */ public void setLineWrap(boolean flag) { if (lineWrap == flag) return; boolean oldValue = lineWrap; lineWrap = flag; firePropertyChange("lineWrap", oldValue, lineWrap); } /** * Checks whether word style wrapping is enabled. * * @return <code>true</code> if word style wrapping is enabled, * <code>false</code> otherwise */ public boolean getWrapStyleWord() { return wrapStyleWord; } /** * Enables/Disables word style wrapping. * * @param flag <code>true</code> to enable word style wrapping, * <code>false</code> otherwise */ public void setWrapStyleWord(boolean flag) { if (wrapStyleWord == flag) return; boolean oldValue = wrapStyleWord; wrapStyleWord = flag; firePropertyChange("wrapStyleWord", oldValue, wrapStyleWord); } /** * Returns the number of characters used for a tab. * This defaults to 8. * * @return the current number of spaces used for a tab. */ public int getTabSize() { return tabSize; } /** * Sets the number of characters used for a tab to the * supplied value. If a change to the tab size property * occurs (i.e. newSize != tabSize), a property change event * is fired. * * @param newSize The new number of characters to use for a tab. */ public void setTabSize(int newSize) { if (tabSize == newSize) return; int oldValue = tabSize; tabSize = newSize; firePropertyChange("tabSize", oldValue, tabSize); } protected int getColumnWidth() { FontMetrics metrics = getToolkit().getFontMetrics(getFont()); return metrics.charWidth('m'); } public int getLineCount() { return getDocument().getDefaultRootElement().getElementCount(); } public int getLineStartOffset(int line) throws BadLocationException { int lineCount = getLineCount(); if (line < 0 || line > lineCount) throw new BadLocationException("Non-existing line number", line); Element lineElem = getDocument().getDefaultRootElement().getElement(line); return lineElem.getStartOffset(); } public int getLineEndOffset(int line) throws BadLocationException { int lineCount = getLineCount(); if (line < 0 || line > lineCount) throw new BadLocationException("Non-existing line number", line); Element lineElem = getDocument().getDefaultRootElement().getElement(line); return lineElem.getEndOffset(); } public int getLineOfOffset(int offset) throws BadLocationException { Document doc = getDocument(); if (offset < doc.getStartPosition().getOffset() || offset >= doc.getEndPosition().getOffset()) throw new BadLocationException("offset outside of document", offset); return doc.getDefaultRootElement().getElementIndex(offset); } protected int getRowHeight() { FontMetrics metrics = getToolkit().getFontMetrics(getFont()); return metrics.getHeight(); } /** * Inserts the supplied text at the specified position. Nothing * happens in the case that the model or the supplied string is null * or of zero length. * * @param string The string of text to insert. * @param position The position at which to insert the supplied text. * @throws IllegalArgumentException if the position is < 0 or greater * than the length of the current text. */ public void insert(String string, int position) { // Retrieve the document model. Document doc = getDocument(); // Check the model and string for validity. if (doc == null || string == null || string.length() == 0) return; // Insert the text into the model. try { doc.insertString(position, string, null); } catch (BadLocationException e) { throw new IllegalArgumentException("The supplied position, " + position + ", was invalid."); } } public void replaceRange(String text, int start, int end) { Document doc = getDocument(); if (start > end || start < doc.getStartPosition().getOffset() || end >= doc.getEndPosition().getOffset()) throw new IllegalArgumentException(); try { doc.remove(start, end - start); doc.insertString(start, text, null); } catch (BadLocationException e) { // This cannot happen as we check offset above. } } /** * Returns the preferred size for the JTextArea. This is the maximum of * the size that is needed to display the content and the requested size * as per {@link #getColumns} and {@link #getRows}. * * @return the preferred size of the JTextArea */ public Dimension getPreferredSize() { int reqWidth = getColumns() * getColumnWidth(); int reqHeight = getRows() * getRowHeight(); View view = getUI().getRootView(this); int neededWidth = (int) view.getPreferredSpan(View.HORIZONTAL); int neededHeight = (int) view.getPreferredSpan(View.VERTICAL); return new Dimension(Math.max(reqWidth, neededWidth), Math.max(reqHeight, neededHeight)); } /** * Returns the accessible context associated with the <code>JTextArea</code>. * * @return the accessible context associated with the <code>JTextArea</code> */ public AccessibleContext getAccessibleContext() { if (accessibleContext == null) accessibleContext = new AccessibleJTextArea(); return accessibleContext; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -