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

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...