程式扎記: [ Java 常見問題 ] Swing: JSplitPane splitting 50% precisely

標籤

2014年1月13日 星期一

[ Java 常見問題 ] Swing: JSplitPane splitting 50% precisely

來源自 這裡
Question:
In Swing, what's the best way to make the JSplitPane to split two jpanels with 50% size each. It looks like if I don't set preferred sizes on the panels it always makes the first panel almost invisible (2%) and the second one (98%)

Answer:
可以透過 API:setResizeWeight(weight). 該 API 的使用說明如下:
Specifies how to distribute extra space when the size of the split pane changes. A value of 0, the default, indicates the right/bottom component gets all the extra space (the left/top component acts fixed), where as a value of 1 specifies the left/top component gets all the extra space (the right/bottom component acts fixed). Specifically, the left/top component gets (weight * diff) extra space and the right/bottom component gets (1 - weight) * diff extra space.

假設你已經有一個 JSplitPane 物件 jSplitPaneEdit, 則你可以如下設定該 JSplitPane 的物件透過 divider 50/50 切割左右或上下平面:
  1. jSplitPaneEdit.setDividerLocation(-1);  
  2. jSplitPaneEdit.setResizeWeight(0.5);  
  3. jSplitPaneEdit.repaint();  

This message was edited 1 time. Last update was at 13/01/2014 16:44:07

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!