我是在application中开发,如果JTextArea无法实现,可以用什么类来实现?

解决方案 »

  1.   

    //你得用JTextPane,下面是一个例子。
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.text.*;
    import java.io.*;public class Test {
      JFrame frame, frame2;
      JTextPane textPane,textPane2;
      File file;
      Icon image;  public Test(){
        frame = new JFrame("JTextPane");
        frame2 = new JFrame("提取的内容");
        textPane = new JTextPane();
        textPane2 = new JTextPane();
        file = new File("icon.gif");
        image = new ImageIcon(file.getAbsoluteFile().toString());
      }  public void insert(String str, AttributeSet attrSet) {
        Document doc = textPane.getDocument();
        str ="\n" + str ;
        try {
          doc.insertString(doc.getLength(), str, attrSet);
        }
        catch (BadLocationException e) {
          System.out.println("BadLocationException: " + e);
        }
      }  public void setDocs(String str,Color col,boolean bold,int fontSize) {
        SimpleAttributeSet attrSet = new SimpleAttributeSet();
        StyleConstants.setForeground(attrSet, col);
        //颜色
        if(bold==true){
          StyleConstants.setBold(attrSet, true);
        }//字体类型
        StyleConstants.setFontSize(attrSet, fontSize);
        //字体大小
        insert(str, attrSet);
      }  public void gui() {
        textPane.insertIcon(image);
        setDocs("第一行的文字",Color.red,false,20);
        setDocs("第二行的文字",Color.BLACK,true,25);
        setDocs("第三行的文字",Color.BLUE,false,20);
        frame.getContentPane().add(textPane, BorderLayout.CENTER);
        frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }});
        frame2.getContentPane().add(textPane2, BorderLayout.CENTER);
        frame.setSize(200,300);
        frame.setVisible(true);
        frame2.setLocation(200, 0);
        frame2.setSize(200,300);
        frame2.setVisible(true);
      }
      public static void main(String[] args) {
        Test test = new Test();
        test.gui();
        StyledDocument docs = test.textPane.getStyledDocument();//取得textPane中的StyledDocument类文档
        test.textPane2.setStyledDocument(docs);//将StyledDocument类文档传给textPane2
      }
    }
      

  2.   

    可以通过设置前景色实现
    JTextArea tArea;
    tArea.setForeground(Color.blue);