Swing常用組件之多行文本區(qū)JTextArea
單行文本的輸入存在嚴(yán)重的缺陷,也不適合實際的運用,本節(jié)通過一個無功能的記事本來介紹可以進(jìn)行多行輸入的JTextArea:
JTextArea():創(chuàng)建一個內(nèi)容為空的文本區(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) :指定文本內(nèi)容的文本區(qū)
JTextArea(String text,int rows,int columns) :指定文本內(nèi)容和行數(shù),列數(shù)的文本區(qū)
JTextArea 的一些常用方法:
public void append(String str) :將給定文本追加到文檔結(jié)尾。
boolean getLineWrap() :獲取文本區(qū)的換行策略。
public int getRows() :返回 TextArea 中的行數(shù)。
public boolean getWrapStyleWord() :獲取換行方式(如果文本區(qū)要換行)。
public void setWrapStyleWord(boolean word) :設(shè)置換行方式(如果文本區(qū)要換行)
public void insert(String str, int pos) :將指定文本插入指定位置。
public void setColumns(int columns) :設(shè)置此 TextArea 中的列數(shù)。
public void setFont(Font f) :設(shè)置當(dāng)前字體。
public void setLineWrap(boolean wrap) :設(shè)置文本區(qū)的換行策略。
public void setRows(int rows) :設(shè)置此 TextArea 的行數(shù)。
public void setEditable(boolean b):設(shè)置文本區(qū)的編輯狀態(tài)。參數(shù)為true表示可編輯狀態(tài),為false則表示不可編輯狀態(tài)
將JTextArea放入JScrollPane中,這樣就能利用滾動的效果看到輸入超過JTextArea高度的文字.
JScrollPane
JscrollPane() :創(chuàng)建一個滾動條,水平和垂直都可以顯示
JscrollPane(Component view) : 創(chuàng)建一個顯示指定組件內(nèi)容的滾動條,當(dāng)組件的內(nèi)容超過視圖大小就會顯示水平和垂直的的滾動條
JscrollPane(Component view,int vsbPolicy,int hsbPolicy) :創(chuàng)建一個顯示指定組件內(nèi)容的滾動條,且有指定滾動策略的滾動條
JscrollPane(int vsbPolicy,int hsbPolicy) :創(chuàng)建一個有指定滾動策略的滾動條
JscrollPane類的構(gòu)造方法中使用的滾動條策略主要有以下幾種:
public int getHorizontalScrollBarPolicy (): 獲取水平滾動策略值
public int getVerticalScrollBarPolicy() :獲取垂直滾動策略值
public void getHorizontalScrollBarPolicy (): 設(shè)置水平滾動策略值
public void getVerticalScrollBarPolicy() :設(shè)置垂直滾動策略值
public boolean isWheelScrollingEnabled() : 設(shè)置是否進(jìn)行滾動以響應(yīng)鼠標(biāo)滾輪
public void setViewportView(Conponent view) :設(shè)置滾動條中的滾動組件
public void setWheelScrollingEnabled(boolean handleWheel):啟動/禁用對鼠標(biāo)滾輪滾動的移動響應(yīng)
代碼實例:
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();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
java保證對象在內(nèi)存中唯一性的實現(xiàn)方法
這篇文章主要介紹了java如何保證對象在內(nèi)存中的唯一性,如果創(chuàng)建多個對象的話,可能會引發(fā)出各種各樣的問題,這時,就需要我們保證這個對象在內(nèi)存中的唯一性,需要的朋友可以參考下2019-06-06
Spring Boot中使用Actuator的/info端點輸出Git版本信息
這篇文章主要介紹了Spring Boot中使用Actuator的/info端點輸出Git版本信息,需要的朋友可以參考下2017-06-06
Java Idea TranslationPlugin翻譯插件使用解析
這篇文章主要介紹了Java Idea TranslationPlugin翻譯插件使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04

