- Swing 教程
- Swing - 首頁
- Swing - 概述
- Swing - 環境配置
- Swing - 控制元件
- Swing - 事件處理
- Swing - 事件類
- Swing - 事件監聽器
- Swing - 事件介面卡
- Swing - 佈局
- Swing - 選單
- Swing - 容器
- Swing 有用資源
- Swing - 快速指南
- Swing - 有用資源
- Swing - 討論
Swing - JTextArea 類
介紹
JTextArea 類是一個用於顯示純文字的多行區域。
類宣告
以下是javax.swing.JTextArea類的宣告:
public class JTextArea extends JTextComponent
類建構函式
| 序號 | 建構函式及描述 |
|---|---|
| 1 |
JTextArea() 構造一個新的文字區域。 |
| 2 |
JTextArea(Document doc) 使用給定的文件模型和所有其他引數的預設值(null, 0, 0)構造一個新的 JTextArea。 |
| 3 |
JTextArea(Document doc, String text, int rows, int columns) 使用指定行數、列數和給定模型構造一個新的 JTextArea。 |
| 4 |
JTextArea(int rows, int columns) 使用指定行數和列數構造一個新的空文字區域。 |
| 5 |
JTextArea(String text) 構造一個顯示指定文字的新文字區域。 |
| 6 |
JTextArea(String text, int rows, int columns) 使用指定的文字和行數、列數構造一個新的文字區域。 |
類方法
| 序號 | 方法及描述 |
|---|---|
| 1 |
void append(String str) 將給定文字追加到文件末尾。 |
| 2 |
protected Document createDefaultModel() 建立模型的預設實現,如果構造時沒有顯式給出,則使用此實現。 |
| 3 |
AccessibleContext getAccessibleContext() 獲取與此 JTextArea 關聯的 AccessibleContext。 |
| 4 |
int getColumns() 返回文字區域中的列數。 |
| 5 |
protected int getColumnWidth() 獲取列寬。 |
| 6 |
int getLineCount() 確定區域中包含的行數。 |
| 7 |
int getLineEndOffset(int line) 確定給定行的末尾的偏移量。 |
| 8 |
int getLineOfOffset(int offset) 將元件文字中的偏移量轉換為行號。 |
| 9 |
int getLineStartOffset(int line) 確定給定行的開頭的偏移量。 |
| 10 |
boolean getLineWrap() 獲取文字區域的換行策略。 |
| 11 |
Dimension getPreferredScrollableViewportSize() 如果此元件嵌入 JScrollPane 中,則返回視口的首選大小。 |
| 12 |
Dimension getPreferredSize() 返回文字區域的首選大小。 |
| 13 |
protected int getRowHeight() 定義行高的含義。 |
| 14 |
int getRows() 返回文字區域中的行數。 |
| 15 |
boolean getScrollableTracksViewportWidth() 如果視口應始終強制此 Scrollable 的寬度與視口的寬度匹配,則返回 true。 |
| 16 |
int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) 顯示邏輯行或列的元件應計算將完全顯示一個新行或列的滾動增量,具體取決於 orientation 的值。 |
| 17 |
int getTabSize() 獲取用於擴充套件製表符的字元數。 |
| 18 |
String getUIClassID() 返回 UI 的類 ID。 |
| 19 |
boolean getWrapStyleWord() 如果文字區域正在換行,則獲取使用的換行樣式。 |
| 20 |
void insert(String str, int pos) 在指定位置插入指定的文字。 |
| 21 |
protected String paramString() 返回此 JTextArea 的字串表示形式。 |
| 22 |
void replaceRange(String str, int start, int end) 將從指示的起始位置到結束位置的文字替換為指定的新的文字。 |
| 23 |
void setColumns(int columns) 設定此文字區域的列數。 |
| 24 |
void setFont(Font f) 設定當前字型。 |
| 25 |
void setLineWrap(boolean wrap) 設定文字區域的換行策略。 |
| 26 |
void setRows(int rows) 設定此文字區域的行數。 |
| 27 |
void setTabSize(int size) 設定要擴充套件製表符的字元數。 |
| 28 |
void setWrapStyleWord(boolean word) 設定使用的換行樣式,如果文字區域正在換行。 |
繼承的方法
此類繼承自以下類的方法:
- javax.swing.text.JTextComponent
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.lang.Object
JTextArea 示例
使用您選擇的任何編輯器建立以下 Java 程式,例如在D:/ > SWING > com > tutorialspoint > gui >
SwingControlDemo.java
package com.tutorialspoint.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 com\tutorialspoint\gui\SwingControlDemo.java
如果未出現錯誤,則表示編譯成功。使用以下命令執行程式。
D:\SWING>java com.tutorialspoint.gui.SwingControlDemo
驗證以下輸出。