[英]How can I display red text in a JTextArea?
我想在編譯exec文件后用紅色顯示結果中的錯誤(文本),並使用java中的swing在gui的textarea中顯示它。
普通的JTextArea不支持像不同顏色的文本這樣的花哨的東西。 但是,有類似的組件。 請參閱http://java.sun.com/docs/books/tutorial/uiswing/components/text.html
JEditorPane可以獲取HTML格式的內容。 官方的Sun教程也提供了一些見解:
JTextArea類提供顯示多行文本的組件,並可選擇允許用戶編輯文本。 如果您只需要從用戶那里獲得一行輸入,則應使用文本字段。 如果希望文本區域使用多種字體或其他樣式顯示其文本,則應使用編輯器窗格或文本窗格。 如果顯示的文本長度有限且用戶從未編輯過,請使用標簽。
這是使用AttributeSet和StyleConstants向JEditorPane添加文本的快速示例。
這會帶來一個帶有JEditorPane的小框架,您可以使用它來添加許多顏色的文本,而無需使用HTML。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
public class TextColor extends JFrame implements ActionListener {
JTextPane myTextPane;
JTextArea inputTextArea;
public TextColor() {
super();
JPanel temp = new JPanel(new BorderLayout());
inputTextArea = new JTextArea();
JButton btn = new JButton("Add");
btn.addActionListener(this);
temp.add(btn, BorderLayout.SOUTH);
temp.add(inputTextArea, BorderLayout.NORTH);
this.getContentPane().add(temp, BorderLayout.SOUTH);
myTextPane = new JTextPane();
myTextPane.setBorder(new EtchedBorder());
this.getContentPane().add(myTextPane, BorderLayout.CENTER);
this.setSize(600, 600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
Color newTextColor = JColorChooser.showDialog(this, "Choose a Color", Color.black);
//here is where we change the colors
SimpleAttributeSet sas = new SimpleAttributeSet(myTextPane.getCharacterAttributes());
StyleConstants.setForeground(sas, newTextColor);
try {
myTextPane.getDocument().insertString(myTextPane.getDocument().getLength(),
inputTextArea.getText(), sas);
} catch (BadLocationException ble) {
ble.printStackTrace();
}
}
public static void main(String args[]) {
new TextColor();
}
}
史密塔,
請注意粘貼代碼片段,以便人們可以了解問題的確切位置或需要幫助。
來你的問題,
據我所知,沒有辦法在java中的textArea中為不同的文本元素設置不同的顏色。 您只能為所有人設置一種顏色。
另一種方法是使用JTextPane。
看看以下代碼是否有助於您的事業。
String text = "Some Text..."; //This can be any piece of string in your code like
output of your program...
JTextPane myTextPane = new JTextPane();
SimpleAttributeSet sas = new SimpleAttributeSet(myTextPane.getCharacterAttributes());
// As what error you were referring was not clear, I assume there is some code in your
program which pops out some error statement. For convenience I use Exception here..
if( text.contains("Exception") ) //Checking if your output contains Exception...
{
StyleConstants.setForeground(sas, Color.red); //Changing the color of
StyleConstants.setItalic(sas, true);
try
{
myTextPane.getDocument().insertString
(
myTextPane.getDocument().getLength(),
text + "\n",
sas
);
}
catch( BadLocationException ble )
{
text.append(ble.getMessage());
}
}
else
{
StyleConstants.setForeground(sas, Color.GREEN);
try
{
myTextPane.getDocument().insertString
(
myTextPane.getDocument().getLength(),
text + "\n",
sas
);
}
catch(BadLocationException ble)
{
text.append(ble.getMessage());
}
}
我想這可以通過一些修改來解決您的問題。
謝謝。
蘇希爾
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.