JAVA文本域顯示不同字體顏色的文字

[img]http://dl2.iteye.com/upload/attachment/0111/8456/7d80e4b9-6eb6-3123-994c-79f8dfbd496f.png[/img]


package JTextPane;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;

/**
* @version 1.0
* @author 劉勝軍
*/
public class Test extends JFrame {
private static final long serialVersionUID = 1L;

/** 定義一個歷史面板,用於顯示已經發送的文字 */
private JTextPane _old = new JTextPane();

/** 定義一個輸入面板,用於顯示正在輸入的內容 */
private JTextPane _new = new JTextPane();

/** 聲明三組樣式,具體的在方法public static void styleInit() {}中定義 */
private Style style1 = null;
private Style style2 = null;
private Style style3 = null;

/** 下拉列表,用於選擇樣式 */
private JComboBox<String> box = new JComboBox<String>();

/** 發送按鈕,用於將消息提交到歷史面板 */
private JButton send = new JButton("提交");

public static void main(String[] args) {
new Test();
}

/**
* 構造方法,需要完成所以初始化操作 鼠標放在方法名上,可以顯示其內容
*/
public Test() {
styleInit();
init();
}

/** 樣式初始化 */
public void styleInit() {

Style style = _new.getStyledDocument().addStyle(null, null);// 獲取組件空樣式,addStyle(null,
// null)會返回一個空樣式

StyleConstants.setFontFamily(style, "楷體");// 爲style樣式設置字體屬性
StyleConstants.setFontSize(style, 18);// 爲style樣式設置字體大小

Style normal = _new.addStyle("normal", style);// 將style樣式添加到組件,並命名爲normal,返回一個樣式由Style
// normal變量接收
/** 這個時候,組件編輯器關聯的模型中就添加了一個樣式normal,這個樣式是最基本的一個樣式,其他樣式可以根據他進行修改 */

style1 = _new.addStyle("style1", normal);// 基於normal樣式,在添加三次,分別命名爲style1,style2,style3
style2 = _new.addStyle("style2", normal);// 此時,style1,style2,style3三個樣式和normal樣式是一模一樣的
style3 = _new.addStyle("style3", normal);// 如果修改,可以對每個變量單獨修改,具體修改方式如下

StyleConstants.setForeground(style1, Color.GREEN);// 將style1的顏色設置爲綠色

StyleConstants.setForeground(style2, Color.RED);// 將style2的顏色設置爲紅色

StyleConstants.setForeground(style3, Color.BLACK);// 將style3的顏色設置爲黑色
StyleConstants.setFontSize(style3, 14);// 將style3的大小設置爲14
}

/** 初始化佈局 */
public void init() {
this.setBounds(200, 100, 420, 520);
this.setLayout(null);

this._old.setEditable(false);
// 定義滾動面板,放歷史面板,以實現滾動條(有需要的時候顯示)和換行
JScrollPane js_old = new JScrollPane(_old,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// 設置位置大小
js_old.setBounds(0, 0, 400, 300);
// 添加到窗體
this.add(js_old);

// 定義滾動面板,放輸入面板,以實現滾動條(有需要的時候顯示)和換行
JScrollPane js_new = new JScrollPane(_new,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// 設置位置大小
js_new.setBounds(0, 350, 400, 150);
// 添加到窗體
this.add(js_new);

this.box.addItem("style1");
this.box.addItem("style2");
this.box.addItem("style3");
this.box.setBounds(50, 315, 100, 20);
this.add(this.box);

this.send.setBounds(200, 315, 100, 20);
this.add(this.send);

this.send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
inserMessage();
}
});

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

/** 將文字插入到歷史面板,並清空輸入面板 */
public void inserMessage() {
try {
/** 判斷下拉列表內容,確定使用哪種樣式 */
Style style = (box.getSelectedItem().equals("style1")) ? style1
: (box.getSelectedItem().equals("style2")) ? style2
: style3;
/**
* 獲取歷史面板的insertString方法,將文字追加到歷史面板上
*
* void insertString(int offset, String str, AttributeSet a) throws
* BadLocationException offset - 要插入內容的偏移量,該值 >=
* 0。跟蹤給定的位置或其後位置的更改的所有位置都將移動。 str - 要插入的字符串 a -
* 要與插入的內容關聯的屬性。如果沒有屬性,它可能爲 null。
*
* this._old.getStyledDocument().getLength()這一句是獲取當前面板內容的總長度,
* 作爲要插入內容的偏移量 this._new.getText()+"\n"這一句是獲取輸入面板內容 style這一句是使用的樣式
*/
this._old.getStyledDocument().insertString(
this._old.getStyledDocument().getLength(),
this._new.getText() + "\n", style);
/** 將輸入面板置空 */
this._new.setText(null);
} catch (BadLocationException e) {
e.printStackTrace();
}
}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章