2010-03-05 33 views

回答

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

相關問題