2011-11-06 25 views
0

我正在制作一个简单的聊天应用程序,我试图找出哪个文本组件更适合使用。组件需要支持彩色文本,提供换行和支持滚动窗格。它也必须允许用户选择要使用的字体(大小,样式等)。要使用哪个文本组件?

这是最好的选择吗?谢谢 。

+3

看看这个网页上的差异:http://download.oracle.com/javase/tutorial/uiswing/components/text.html从你的描述,我认为对JEditorPane是可能是最好的选择。它支持彩色文本,而JTextArea没有 –

+0

谷歌没有提起这件事..谢谢你生病给它一个阅读。 – Giannis

+0

@猎人,如果这是一个答案,我可以+1。 –

回答

4

JTextArea可以完成所有这些工作,您可以查看Document界面,因为这是一个聊天应用程序。该文档将使您能够同步两个组件,如JTextField和JTextArea。文档不是任何类型的文本字段,而是与其中一个一起使用。 JTextField具有Document“JTextField(Document doc)”的构造函数方法。要设置文本的颜色只需调用JTextArea的setForeground(Color)方法,该方法也是从其父组件JComponent继承的。

enter image description here

import javax.swing.*; 
import java.awt.*; 

public class Example { 

    JFrame frameA = new JFrame("Example"); 
    JTextArea textA = new JTextArea(); 

    public Example() { 
     frameA.setSize(600, 300); 
     frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Container content = frameA.getContentPane(); // Set the Color of textA. 
     textA.setForeground(Color.red); 
     content.add(textA); 
     frameA.setVisible(true); 
    } 

    public static void main(String[] args) { 
     Example exam = new Example(); 
    } 
} 
+0

'JTextArea'不支持彩色文本。尽管'JTextPane'和'JEditorPane'都可以。 –

+0

是的JTextArea在这里是一个代码示例。 – ThunderWolf

+0

import javax.swing。*; import java.awt。*; 公共类示例{JFrame frameA = new JFrame(“Example”); JTextArea textA = new JTextArea(); public示例(){ frameA.setSize(600,300); frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = frameA.getContentPane(); //设置textA的颜色。 textA.setForeground(new Color(255,150,150)); content.add(textA); frameA.setVisible(true); } public static void main(String [] args){ 示例exam = new Example(); } } – ThunderWolf