Swing
Swing教學首頁
Swing介紹
Swing開發環境安裝
Swing控件
Swing Component類
Swing Container類
Swing JComponent類
Swing JLabel
Swing JButton
Swing JColorChooser
Swing JCheckBox
Swing JRadioButton
Swing JList
Swing JComboBox
Swing JTextField
Swing JTextArea
Swing ImageIcon
Swing JScrollBar
Swing JOptionPane
Swing JFileChooser
Swing JProgressBar
Swing JSlider
Swing JSpinner
Swing事件處理
SWING Event事件類
SWING AWTEvent事件類
SWING ActionEvent事件類
SWING InputEvent事件類
SWING KeyEvent事件類
SWING MouseEvent事件類
SWING WindowEvent事件類
SWING AdjustmentEvent事件處理
SWING ComponentEvent處理類
SWING ContainerEvent事件處理類
SWING MouseMotionEvent事件處理
SWING PaintEvent事件類
Swing事件監聽器
SWING ActionListener接口
SWING ComponentListener接口
Swing ItemListener接口
Swing KeyListener接口
Swing MouseListener Interface
Swing WindowListener接口
Swing AdjustmentListener接口
Swing ContainerListener接口
Swing MouseMotionListener接口
Swing FocusListener接口
Swing事件適配器
Swing FocusAdapter類
Swing KeyAdapter類
Swing MouseAdapter類
Swing WindowAdapter類
Swing MouseMotionAdapter類
Swing Layout佈局
Swing LayoutManager接口
Swing LayoutManager2接口
Swing BorderLayout佈局
Swing CardLayout佈局
Swing FlowLayout佈局類
Swing GridLayout佈局類
Swing GridBagLayout佈局類
Swing GroupLayout佈局類
Swing SpringLayout佈局類
Swing Menu菜單類
Swing JMenuBar類
Swing JMenuItem類
Swing JMenu類
Swing JCheckboxMenuItem類及例子
Swing JRadioButtonMenuItem類及例子
Swing JPopupMenu類及實例
Swing容器
Swing JPanel類及實例
Swing JFrame類和實例
Swing JWindow類及實例

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.java

package 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

驗證下面的輸出

Swing