-3

如何設置製表位的JTextArea不同長度,自定義選項卡中的Java

第一片應該停止4

第二個選項卡應在20停止(如果我給標籤從4它應該停止20)

第三個選項卡應在30停止(如果我給0標籤應該在30停止)

+0

沒有標籤系統像你所描述的那樣工作。在我熟悉的Tab鍵系統中,隨後按下Tab鍵時,光標會從0到4到20到30到下一行。 – 2012-07-19 14:51:09

回答

2

嘗試這樣的事情。

StyledDocument document = new DefaultStyledDocument(): 

SimpleAttributeSet attributes = new SimpleAttributeSet(); 

TabStop[] tabStops = new TabStop[3]; 
tabStops[0] = new TabStop(4.0, TabStop.ALIGN_LEFT, TabStop.LEAD_DOTS); 
tabStops[1] = new TabStop(20.0, TabStop.ALIGN_LEFT, TabStop.LEAD_DOTS); 
tabStops[2] = new TabStop(30.0, TabStop.ALIGN_LEFT, TabStop.LEAD_DOTS); 

TabSet tabSet = new TabSet(tabStops); 
StyleConstants.setTabSet(attributes, tabSet); 
document.setParagraphAttributes(0, 0, attributes, false); 

當您創建JTextArea,使用Document構造。

+0

謝謝,吉爾伯特 – FirmView 2012-07-19 15:16:29

+0

我在分數位置使用分接頭時遇到了問題。注意tap stop是一個浮點值。因此,我使用Math.rint()將分接頭停止位置四捨五入爲最接近的整數值。 – Yuri 2014-05-08 09:00:02