2010-06-05 27 views
2

如何將文本放入jTextArea的特定列中?Java Swing:JTextArea列問題

 private javax.swing.JTextArea jTextArea1; 
     jTextArea1.setColumns(4); 
     jTextArea1.insert(price, 0); //column 1 
     jTextArea1.insert(cost, 0); //column 2 
     jTextArea1.insert(quantity, 0);  //column etc.. 
     jTextArea1.insert(itemName, 0); 
     jTextArea1.insert("\n", 0); 

回答

1

最好的方法是使用JTable。

但是,如果您確實想使用文本cmponent,則可以使用JTextPane並使用選項卡進行播放。看我的例子JTextPane Tab Size

0

您的代碼插入您的文本在相同的位置! 將0替換爲您希望文本位於的任何列。而看到結果

編輯:

列大小意味着字符,無法言語的量。 正如已評論,看看JTable

+0

雅我不真的得到的位置屬性。 basiclly我想將價格,成本,數量,名稱設置爲單個列 – nubme 2010-06-05 21:59:02

+0

然後,您沒有使用正確的Swing組件。 你最好看看JTable。有適當的專欄。您只需設置一個包含所有值的數組,將數組的每個元素放置在其相應的列中。 – Marc 2010-06-05 22:01:32

+0

雅我想使用它,但我不確定如何在while語句中執行它,因爲我從文件中讀取值。 – nubme 2010-06-05 22:11:00

1

列指的是一個字符,所以如果列= 4這意味着寬度將是4個字符寬。

+0

哦,有沒有辦法將我的文本分成列或縮進它,以便它均勻地出現在我的textarea中? – nubme 2010-06-05 22:00:12

+0

改用JTable或多個JTextAreas – JRL 2010-06-05 22:00:37

3

有時候(強大的)JTable太多了。

如果你只是想要一個JLabel/JTextArea中相似的組成一些列JTextPane中或在JEditorPane中使用的HTML表:

enter image description here

import java.awt.Font; 
import javax.swing.JDialog; 
import javax.swing.JTextPane; 
import javax.swing.UIManager; 
import javax.swing.text.html.HTMLDocument; 

public class ColumnsInJTextPane 
{ 

    public ColumnsInJTextPane() 
    { 
    double price = 124.75; 
    int quantity = 3; 
    String itemName = " iPad"; 

    JTextPane t = new JTextPane(); 
    t.setContentType("text/html"); 

    StringBuilder text = new StringBuilder(150); 
    text.append("<html><body>"); 
    text.append("<table border='0' style='margin:4px 2px 12px 6px' width='100%'>"); 

    text.append("<tr>" + "<td width='30' align='left' valign='top' style='margin-right:8px'>"); 
    text.append(price); 
    text.append("</td>"); 

    text.append("<td align='left' valign='top' style='margin-right:8px'>"); 
    text.append(itemName); 
    text.append("</td>"); 

    text.append("<td width='20' align='left' valign='top' style='margin-right:8px'>"); 
    text.append(quantity); 
    text.append("</td>" + "</tr>"); 

    text.append("<tr>" + "<td>"); 
    text.append(price * 4); 
    text.append("</td>"); 

    text.append("<td>"); 
    text.append((((Boolean) itemName.equals(itemName)).toString().concat(itemName))); 
    text.append("</td>"); 

    text.append("<td>"); 
    text.append(quantity/2); 
    text.append("</td>" + "</tr>"); 

    text.append("</table>"); 
    text.append("</body></html>"); 

    t.setText(text.toString()); 

    //to get a consistent (body) appearance use the font from the Label using a CSS rule (instead of the value in javax.swing.text.html.default.css) 
    Font font = UIManager.getFont("Label.font"); 
    String bodyRule = 
    "body { font-family: " + font.getFamily() + "; " + "font-size: " + font.getSize() + "pt; }"; 
    ((HTMLDocument) t.getDocument()).getStyleSheet().addRule(bodyRule); 

    JDialog d = new JDialog(); 
    d.add(t); 
    d.pack(); 
    d.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
    new ColumnsInJTextPane(); 
    } 

} 

有一些調整和技巧在這個例子證明了HTML是如何真正的;-)

  • 使用空間的修整
  • 第二列生長/因爲在其TD-標籤省略的寬度
  • CSS空間被拋出的無特殊原因
  • 爲一致的外觀的JLabel的字體具有與的JDialog尺寸收縮從UIManager中提取並通過CSS設置
  • 如果您減小了JDialog的寬度,那麼第二列中的文本將很好地包裝爲文字
    (哦,等等!這是一個不錯功能的HTML)