2014-03-28 47 views
1

我正在編寫一個Java程序,並且在程序的某個部分使用了一個JTextpane。 在此的JTextPane我用tab(\ t),以列,是這樣的:Java JTextpane Tab Size

txtpnMTrace = new JTextPane(); 
txtpnMTrace.setFont(new Font("Dialog", Font.PLAIN, 11)); 
doc = txtpnMTrace.getStyledDocument(); 
... 
doc.insertString(doc.getLength(),value1+"\t"+value2+"\t"+value3+"\n", null); 

... 

我可以在標籤看到列中的值(\ t)的距離。 標籤距離總是相同的,我想改變這個距離來改善外觀。

您知道嗎如何更改JTextPane中的選項卡(\ t)大小?

在此先感謝。

回答

3
import javax.swing.*; 
import javax.swing.text.*; 

public class TabExample { 

    public static void main(String[] args) { 

     JTextPane pane = new JTextPane(); 
     TabStop[] tabs = new TabStop[4]; 
     tabs[0] = new TabStop(60, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE); 
     tabs[1] = new TabStop(100, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); 
     tabs[2] = new TabStop(200, TabStop.ALIGN_CENTER, TabStop.LEAD_NONE); 
     tabs[3] = new TabStop(300, TabStop.ALIGN_DECIMAL, TabStop.LEAD_NONE); 
     TabSet tabset = new TabSet(tabs); 

     StyleContext sc = StyleContext.getDefaultStyleContext(); 
     AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, 
     StyleConstants.TabSet, tabset); 
     pane.setParagraphAttributes(aset, false); 
     pane.setText("\tright\tleft\tcenter\tdecimal\n" + "\t1\t1\t1\t1.0\n" 
     + "\t200.002\t200.002\t200.002\t200.002\n" 
     + "\t.33\t.33\t.33\t.33\n"); 

     JFrame frame = new JFrame("TabExample"); 
     frame.setContentPane(new JScrollPane(pane)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(360, 120); 
     frame.setVisible(true); 
    } 
} 
+0

感謝您的回答,我試了一下,工作正常。謝謝 – ramon74