java swing:是否有可能改变特定字符串变量的颜色?

时间:2012-03-28 01:09:56

标签: java string swing text colors

从上面的问题中,我制作了一个类似于MSN和Yahoo Messenger的简单Windows应用程序。当用户键入消息并点击“发送”按钮时,下面的代码会将用户的姓名和消息附加到textArea:

textArea_ChatLog.append(chatName + "\n" + " " + msgChat + "\n");

将以此格式显示:

Username
Message the user typed.

是否有改变“chatName”颜色的解决方案?我发现的唯一信息是改变整个组件的颜色(这是我不想要的)。

2 个答案:

答案 0 :(得分:5)

您可以使用带有swing组件的HTML标签和样式。看一下本教程 - How to Use HTML in Swing Components

答案 1 :(得分:1)

看起来你应该使用的是JTextPane/JEditorPane,为了改变随机字符串文字的颜色,试试这个代码,看起来这就是你想要的: - )

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class TextPaneTest extends JFrame
{
    private JPanel topPanel;
    private JTextPane tPane;
    private JTextField tfield;
    private String username = null;

    public TextPaneTest()
    {
        topPanel = new JPanel(); 
        topPanel.setLayout(new BorderLayout(5, 5)); 

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);            

        EmptyBorder eb = new EmptyBorder(new Insets(10, 10, 10, 10));

        tPane = new JTextPane();                
        tPane.setBorder(eb);
        //tPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
        tPane.setMargin(new Insets(5, 5, 5, 5));
        JScrollPane scrollPane = new JScrollPane(tPane);

        topPanel.add(scrollPane, BorderLayout.CENTER);

        tfield = new JTextField(10);
        tfield.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (tfield.getDocument().getLength() > 0)
                {
                    appendToPane(tPane, username + " : ", Color.MAGENTA);
                    appendToPane(tPane, tfield.getText() + "\n", Color.DARK_GRAY);
                    tfield.selectAll();
                }
            }
        });     
        topPanel.add(tfield, BorderLayout.PAGE_END);        

        getContentPane().add(topPanel);     

        setSize(200, 100);
        setVisible(true);           

        while (username == null)
        {
            username = JOptionPane.showInputDialog(null, "Please Enter USERNAME : ");
        }
        tfield.requestFocusInWindow();
    }

    private void appendToPane(JTextPane tp, String msg, Color c)
    {
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

        aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

        int len = tp.getDocument().getLength();
        tp.setCaretPosition(len);
        tp.setCharacterAttributes(aset, false);
        tp.replaceSelection(msg);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TextPaneTest();
                }
            });
    }
}