單行文本的輸入存在嚴重的缺陷,也不適合實際的運用,本節(jié)通過一個無功能的記事本來介紹可以進行多行輸入的JTextArea:
JTextArea():創(chuàng)建一個內容為空的文本區(qū)
JTextArea(Document doc) :創(chuàng)建具有指定文檔的文本區(qū)
JTextArea(Document doc,String text,int rows,int columns) :創(chuàng)建具有指定文檔,行數(shù),列數(shù)的文本區(qū)
JTextArea(int rows,int columns) :指定行數(shù),列數(shù)的文本區(qū)
JTextArea(String text) :指定文本內容的文本區(qū)
JTextArea(String text,int rows,int columns) :指定文本內容和行數(shù),列數(shù)的文本區(qū)
JTextArea 的一些常用方法:
public void append(String str) :將給定文本追加到文檔結尾。
boolean getLineWrap() :獲取文本區(qū)的換行策略。
public int getRows() :返回 TextArea 中的行數(shù)。
public boolean getWrapStyleWord() :獲取換行方式(如果文本區(qū)要換行)。
public void setWrapStyleWord(boolean word) :設置換行方式(如果文本區(qū)要換行)
public void insert(String str, int pos) :將指定文本插入指定位置。
public void setColumns(int columns) :設置此 TextArea 中的列數(shù)。
public void setFont(Font f) :設置當前字體。
public void setLineWrap(boolean wrap) :設置文本區(qū)的換行策略。
public void setRows(int rows) :設置此 TextArea 的行數(shù)。
public void setEditable(boolean b):設置文本區(qū)的編輯狀態(tài)。參數(shù)為true表示可編輯狀態(tài),為false則表示不可編輯狀態(tài)
將JTextArea放入JScrollPane中,這樣就能利用滾動的效果看到輸入超過JTextArea高度的文字.
JScrollPane
JscrollPane() :創(chuàng)建一個滾動條,水平和垂直都可以顯示
JscrollPane(Component view) : 創(chuàng)建一個顯示指定組件內容的滾動條,當組件的內容超過視圖大小就會顯示水平和垂直的的滾動條
JscrollPane(Component view,int vsbPolicy,int hsbPolicy) :創(chuàng)建一個顯示指定組件內容的滾動條,且有指定滾動策略的滾動條
JscrollPane(int vsbPolicy,int hsbPolicy) :創(chuàng)建一個有指定滾動策略的滾動條
JscrollPane類的構造方法中使用的滾動條策略主要有以下幾種:
public int getHorizontalScrollBarPolicy (): 獲取水平滾動策略值
public int getVerticalScrollBarPolicy() :獲取垂直滾動策略值
public void getHorizontalScrollBarPolicy (): 設置水平滾動策略值
public void getVerticalScrollBarPolicy() :設置垂直滾動策略值
public boolean isWheelScrollingEnabled() : 設置是否進行滾動以響應鼠標滾輪
public void setViewportView(Conponent view) :設置滾動條中的滾動組件
public void setWheelScrollingEnabled(boolean handleWheel):啟動/禁用對鼠標滾輪滾動的移動響應
代碼實例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package ch10; import java.awt.event.*; import javax.swing.*; public class NoteTextArea extends JFrame implements ActionListener { private JPanel jp = new JPanel (); JButton jb1 = new JButton( "點我自動換行" ); JButton jb2 = new JButton( "點我不換行" ); private JTextArea jt = new JTextArea(); private JScrollPane js = new JScrollPane(jt); public NoteTextArea() { jp.setLayout( null ); jb1.setBounds( 40 , 40 , 180 , 20 ); jb2.setBounds( 280 , 40 , 180 , 20 ); jp.add(jb1); jp.add(jb2); jb1.addActionListener( this ); jb2.addActionListener( this ); js.setBounds( 40 , 80 , 420 , 100 ); jp.add(js); jt.setLineWrap( false ); for ( int i= 0 ;i< 30 ;i++) { jt.append( "自動換行,不換行!" ); } this .add(jp); this .setBounds( 80 , 80 , 300 , 300 ); this .setVisible( true ); this .setTitle( "記事本多行文本區(qū)" ); this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent a) { if (a.getSource()==jb1) { jt.setLineWrap( true ); } else if (a.getSource()==jb2) { jt.setLineWrap( false ); } } public static void main(String args[]) { new NoteTextArea(); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助。