2012-03-28 70 views
0

从上面的问题中,我已经制作了一个类似于MSN和Yahoo Messenger的简单Windows应用程序。当用户键入消息并点击“发送”按钮,下面的代码附加了用户的姓名和消息发送到textarea的:java swing:是否可以改变特定字符串变量的颜色?

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

这将在此格式显示:

Username 
Message the user typed. 

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

+7

字符没有颜色。你在问UI。 – SLaks 2012-03-28 01:11:07

+2

@SLaks这就是为什么我把“java swing:”并问是否“可能”。 – SpicyWeenie 2012-03-28 02:38:52

回答

5

您可以在swing组件中使用HTML标签和样式。看看这个教程 - How to Use HTML in Swing Components

+0

我检查了它,但没有任何关于将外观更改为像我的单个字符串变量。我的字符串名称不能被硬编码,因为它与我用来登录的任何名称相关联。 – SpicyWeenie 2012-03-28 01:58:03

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(); 
       } 
      }); 
    } 
} 
+0

也许是正确的方法,+1 – mKorbel 2012-03-28 06:44:53

+0

呵呵,谢谢,我也希望如此:-) – 2012-03-28 07:23:48