2012-06-27 34 views
1

我在任何地方搜索和研究過我都能找到沒有結果的答案。所以我再次尋求幫助。如何創建顯示在Java Swing組件中的格式化文本(JTextPane,JEditorPane ...)

我想在桌面java swing應用程序中顯示格式化文本。該文本將在某些變量對象的基礎上編程生成,並且不可編輯。

我不知道最好是使用JTextPane還是JEditorPane,或者是什麼。問題是我沒有找到解釋如何使用它們的手冊或教程。我是否必須創建一個HTMLDocument來插入文本?如何創建它?...

是在這種情況下顯示文本的正確方法?或者我可以使用表或標籤或類似的東西。

我需要你的一些建議,如果有一些地方我可以學習如何做,請告訴我。

回答

6

這裏看看這個代碼示例,如果你想要進一步修改這一點,請將Font也作爲參數添加,並在指定的位置使用適當的參數。在JTextField中寫點東西,然後按ENTER幾次。

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 int counter; 
    private Color[] colours = { 
           Color.RED, 
           Color.BLUE, 
           Color.DARK_GRAY, 
           Color.PINK, 
           Color.BLACK, 
           Color.MAGENTA, 
           Color.YELLOW, 
           Color.ORANGE 
           }; 

    public TextPaneTest() { 
     counter = 0; 
    } 

    private void createAndDisplayGUI() {  
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   

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

     tPane = new JTextPane();     
     tPane.setBorder(eb); 
     tPane.setMargin(new Insets(5, 5, 5, 5)); 
     JScrollPane scroller = new JScrollPane(); 
     scroller.setViewportView(tPane); 

     tfield = new JTextField(); 
     tfield.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       counter++; 
       if (counter == 8) 
        counter = 0; 
       String text = tfield.getText() + "\n"; 
       appendToPane(tPane, text, colours[counter]);  
       tfield.selectAll(); 
      } 
     });  

     getContentPane().add(scroller, BorderLayout.CENTER); 
     getContentPane().add(tfield, BorderLayout.PAGE_END); 

     setSize(200, 100); 
     setVisible(true); 
     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() { 
      @Override 
      public void run() { 
       new TextPaneTest().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 
3

我在任何地方都能搜索和研究,我找不到答案。所以我再次尋求幫助。

我一定要創建一個HTMLDocument的插入的文本支持?如何創建呢?...

是是展現正道在這種情況下的文字?或者我可以使用表格或標籤或類似的東西。

  • 的樣式文本是JEditorPane/JTextPane最好的選擇的

    教程
  • 例子或here

+0

我不知道我是愚蠢的還是什麼,但是那些關於文本窗格的Oracle教程並沒有幫助。 – giorgiline

+0

對不起,這是你的問題:-),檢查這個論壇,也許我的個人資料 – mKorbel