Swing JTextArea
JTextArea類是一個多行區域以顯示純文本。
類聲明
以下是聲明的 javax.swing.JTextArea類:
public class JTextArea extends JTextComponent
類構造函數
S.N. | 構造函數 & 描述 |
---|---|
1 |
JTextArea() Constructs a new TextArea. |
2 |
JTextArea(Document doc) Constructs a new JTextArea with the given document model, and defaults for all of the other arguments (null, 0, 0). |
3 |
JTextArea(Document doc, String text, int rows, int columns) Constructs a new JTextArea with the specified number of rows and columns, and the given model. |
4 |
JTextArea(int rows, int columns) Constructs a new empty TextArea with the specified number of rows and columns. |
5 |
JTextArea(String text) Constructs a new TextArea with the specified text displayed. |
6 |
JTextArea(String text, int rows, int columns) Constructs a new TextArea with the specified text and number of rows and columns. |
類方法
S.N. | 方法 & 描述 |
---|---|
1 |
void append(String str) Appends the given text to the end of the document. |
2 |
protected Document createDefaultModel() Creates the default implementation of the model to be used at construction if one isn't explicitly given. |
3 |
AccessibleContext getAccessibleContext() Gets the AccessibleContext associated with this JTextArea. |
4 |
int getColumns() Returns the number of columns in the TextArea. |
5 |
protected int getColumnWidth() Gets column width. |
6 |
int getLineCount() Determines the number of lines contained in the area. |
7 |
int getLineEndOffset(int line) Determines the offset of the end of the given line. |
8 |
int getLineOfOffset(int offset) Translates an offset into the components text to a line number. |
9 |
int getLineStartOffset(int line) Determines the offset of the start of the given line. |
10 |
boolean getLineWrap() Gets the line-wrapping policy of the text area. |
11 |
Dimension getPreferredScrollableViewportSize() Returns the preferred size of the viewport if this component is embedded in a JScrollPane. |
12 |
Dimension getPreferredSize() Returns the preferred size of the TextArea. |
13 |
protected int getRowHeight() Defines the meaning of the height of a row. |
14 |
int getRows() Returns the number of rows in the TextArea. |
15 |
boolean getScrollableTracksViewportWidth() Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport. |
16 |
int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation. |
17 |
int getTabSize() Gets the number of characters used to expand tabs. |
18 |
String getUIClassID() Returns the class ID for the UI. |
19 |
boolean getWrapStyleWord() Gets the style of wrapping used if the text area is wrapping lines. |
20 |
void insert(String str, int pos) Inserts the specified text at the specified position. |
21 |
protected String paramString() Returns a string representation of this JTextArea. |
22 |
void replaceRange(String str, int start, int end) Replaces text from the indicated start to end position with the new text specified. |
23 |
void setColumns(int columns) Sets the number of columns for this TextArea. |
24 |
void setFont(Font f) Sets the current font. |
25 |
void setLineWrap(boolean wrap) Sets the line-wrapping policy of the text area. |
26 |
void setRows(int rows) Sets the number of rows for this TextArea. |
27 |
void setTabSize(int size) Sets the number of characters to expand tabs to. |
28 |
void setWrapStyleWord(boolean word) Sets the style of wrapping used if the text area is wrapping lines. |
方法繼承
這個類從以下類繼承的方法:
-
javax.swing.text.JTextComponent
-
javax.swing.JComponent
-
java.awt.Container
-
java.awt.Component
-
java.lang.Object
JTextArea 例子
選擇使用任何編輯器創建以下java程序在 D:/ > SWING > com > yiibai > gui >
SwingControlDemo.javapackage com.yiibai.gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingControlDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingControlDemo(){ prepareGUI(); } public static void main(String[] args){ SwingControlDemo swingControlDemo = new SwingControlDemo(); swingControlDemo.showTextAreaDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java Swing Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new JLabel("", JLabel.CENTER); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showTextAreaDemo(){ headerLabel.setText("Control in action: JTextArea"); JLabel commentlabel= new JLabel("Comments: ", JLabel.RIGHT); final JTextArea commentTextArea = new JTextArea("This is a Swing tutorial " +"to make GUI application in Java.",5,20); JScrollPane scrollPane = new JScrollPane(commentTextArea); JButton showButton = new JButton("Show"); showButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { statusLabel.setText( commentTextArea.getText()); } }); controlPanel.add(commentlabel); controlPanel.add(scrollPane); controlPanel.add(showButton); mainFrame.setVisible(true); } }
編譯程序,使用命令提示符。到 D:/ > SWING 然後輸出以下命令。
D:SWING>javac comyiibaiguiSwingControlDemo.java
如果冇有錯誤出現,這意味著編譯成功。使用下麵的命令來運行程序。
D:SWING>java com.yiibai.gui.SwingControlDemo
驗證下麵的輸出