我想在編譯exec文件 後顯示紅色的錯誤(文本),並在java的swing中使用gui的textarea顯示它。如何在JTextArea中顯示紅色文本?
3
A
回答
2
正常的JTextArea不支持像不同顏色的文本這樣的花哨事物。但是,也有類似的組件。請參閱http://java.sun.com/docs/books/tutorial/uiswing/components/text.html
2
JEditorPane可以獲取HTML格式的內容。 official Sun tutorial也給出了一些見解:
JTextArea類提供了一個組件,該組件顯示多行文本並可以選擇允許用戶編輯文本。如果您只需要從用戶那裏獲得一行輸入,則應該使用文本字段。如果您希望文本區域使用多種字體或其他樣式顯示其文本,則應使用編輯器窗格或文本窗格。如果顯示的文本長度有限並且從未被用戶編輯過,請使用標籤。
1
下面是使用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();
}
}
0
史密塔,
照顧到粘貼代碼段,這樣一看就明白哪兒問題或需要幫助。
來到你的問題,
據我所知,有沒有辦法在textarea的不同文本元素在Java中設置不同的顏色。您只能爲所有人設置一種顏色。
另一種方法是使用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());
}
}
我想這將解決您的問題,只需少量修改。
謝謝。
Sushil
相關問題
- 1. 在JTextArea中選擇文本不顯示任何選擇顏色
- 2. 如何在警告框中以紅色顯示字體文本?
- 3. 如何在JTextArea中顯示JTree文件
- 4. 顯示在JTextArea中的文本
- 5. 如何在顯示文本時在JTextArea中添加延遲?
- 6. 如何在jtextarea中獲得突出顯示的文本
- 7. 在JTextArea中格式化文本顏色
- 8. 如何在JTextArea中顯示輸入流?
- 9. JTextArea或JTextPane設置突出顯示的文本顏色
- 10. 如何僅在JTextArea的某些部分顯示粗體文本?
- 11. 如何在文本區域中顯示不符合要求的紅色邊框?
- 12. 在事件中沒有顯示在JTextArea中的文本
- 13. 在JTextArea中顯示大文件
- 14. 在Java JTextArea中顯示Unicode
- 15. 在JTextArea中顯示圖像
- 16. 在JTextArea中顯示MalformedURLException
- 17. 在JTextArea中顯示stack.peek
- 18. JTextArea中的不同文本顏色
- 19. Hightlight and change JTextArea中文本的顏色
- 20. 如何使Ext.FormPanel中的按鈕的寬度爲100%並顯示紅色文本?
- 21. 如何在需要時突出顯示文本框邊框紅色?
- 22. 如何在發生捕獲錯誤時突出顯示文本框紅色?
- 23. 如何在MFC中的CRichEditCtrl中顯示紅色的水平線
- 24. 如何更改JTextArea實際顯示的文本?
- 25. 我如何顯示在JTextarea或JTable
- 26. 附MP3文件顯示紅色j
- 27. 代碼拒絕在JTextArea中顯示文本
- 28. JTextArea不顯示
- 29. JTextArea不顯示
- 30. 如何在Java中的JButton,JTextArea上顯示從數據庫檢索的文本?
請張貼一段不起作用的代碼。 – philant 2010-03-05 06:45:50