2012-04-23 760 views
5

正如標題所示,我只是試圖在設置了LineBorder的TextArea上設置邊距(提供一些填充)。沒有設置邊框,.setMargins工作正常。這是特定的代碼塊。Java Swing - 使用線邊框在TextArea上設置邊距

aboutArea = new JTextArea("program info etc....."); 

Border border = BorderFactory.createLineBorder(Color.BLACK); 

aboutArea.setSize(400, 200); 
aboutArea.setBorder(border); 
aboutArea.setEditable(false); 
aboutArea.setFont(new Font("Verdana", Font.BOLD, 12)); 

add(aboutArea); 

我已分別對這些嘗試:

aboutArea.setMargins(10,10,10,10); 
.getBorders(aboutArea).set(10,10,10,10); 
UIManager.put("aboutArea.margin", new Insets(10, 10, 10, 10)); 

,但我申請了邊境後,並不影響利潤率,填充始終爲0。任何想法如何設置填充上與textarea的邊境?

+2

爲'JTextArea'利潤率的關鍵是'「TextArea.margin 「'。 – trashgod 2012-04-23 07:20:19

回答

26

如果你嘗試加入一個CompoundBorder,不會做這,這會給你幾乎同樣的事情

JTextArea tarea = new JTextArea("program info etc."); 
Border border = BorderFactory.createLineBorder(Color.BLACK); 
tarea.setBorder(BorderFactory.createCompoundBorder(border, 
      BorderFactory.createEmptyBorder(10, 10, 10, 10))); 

CHECK THE MIDDLE JTextArea as OUTPUT

+1

@Daniel一切都取決於JTextArea是否放置在JScrollPane中+1 – mKorbel 2012-04-23 05:58:11

+1

+1,但是對於屏幕截圖(傾斜頭部),爲什麼不把GUI放在普通白色BG之前?例如。 Ctrl-t當我在FF中並獲得超過一半屏幕尺寸的空白區域時。有關更多提示,請參閱[如何創建屏幕截圖?](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post) – 2012-04-23 07:28:13

+1

@ mKorbel你提醒我,我通常將組件添加到「JPanel」中,然後將邊框設置爲面板。它適用於滾動窗格,按鈕,標籤.. – 2012-04-23 07:33:59