我有一個JSplitPane,它在顯示時應該將窗格分割50%。JSplitPane SetDividerLocation問題
現在,將setDividerLocation的參數設置爲0.5(如建議),Java似乎將其視爲正常數字而不是百分比。和in一樣,divider不是走到窗格的中間,而是幾乎在左窗格的開始位置(窗格是垂直分開的)。 任何解決方法?
我有一個JSplitPane,它在顯示時應該將窗格分割50%。JSplitPane SetDividerLocation問題
現在,將setDividerLocation的參數設置爲0.5(如建議),Java似乎將其視爲正常數字而不是百分比。和in一樣,divider不是走到窗格的中間,而是幾乎在左窗格的開始位置(窗格是垂直分開的)。 任何解決方法?
setDividerLocation(double)方法只適用於「已實現」的框架,這意味着您打包或使框架可見後。
setDividerLocation(int)方法可以隨時使用。
如果您很高興在每次調整窗格大小時將分隔線移動到中間位置,則可以添加ComponentListener並使其componentResized方法調用setDividerLocation(0.5)。
此修復它:
public class JSplitPane3 extends JSplitPane {
private boolean hasProportionalLocation = false;
private double proportionalLocation = 0.5;
private boolean isPainted = false;
public void setDividerLocation(double proportionalLocation) {
if (!isPainted) {
hasProportionalLocation = true;
this.proportionalLocation = proportionalLocation;
} else {
super.setDividerLocation(proportionalLocation);
}
}
public void paint(Graphics g) {
super.paint(g);
if (!isPainted) {
if (hasProportionalLocation) {
super.setDividerLocation(proportionalLocation);
}
isPainted = true;
}
}
}
有一個解決方案here這是一個簡單的函數,不需要子類或任何其他更改你的splitpane。
我錯過了什麼嗎?似乎有很多頗爲曲折的回答這個問題......但我認爲一個簡單的setResizeWeight(0.5)會解決這個問題...它在SplitPane Tutorial
+1 - 還描述(隱藏?!)作爲評論lins314159的回答... – 2011-08-14 19:59:24
哦,哎呀,我忽略了在評論中。謝謝。似乎有點奇怪,儘管最受歡迎的答案沒有提到它......在一個現在已有2年曆史的問題上。 – 2011-08-16 03:14:01
+1這應該是被接受的答案 – 2013-07-01 13:30:28
這對我的作品中描述的基礎上,戴夫光芒link。
/**
* Set JSplitPane proportional divider location
*
* @param jsplitpane JSplitPane to set
* @param proportionalLocation double <0.0; 1.0>
*/
public static void setJSplitPaneDividerLocation(final JSplitPane jsplitpane, final double proportionalLocation)
{
if (jsplitpane.isShowing())
{
if (jsplitpane.getWidth() > 0 && jsplitpane.getHeight() > 0)
{
jsplitpane.setDividerLocation(proportionalLocation);
}
else
{
jsplitpane.addComponentListener(new ComponentAdapter()
{
@Override
public void componentResized(ComponentEvent ce)
{
jsplitpane.removeComponentListener(this);
setJSplitPaneDividerLocation(jsplitpane, proportionalLocation);
}
});
}
}
else
{
jsplitpane.addHierarchyListener(new HierarchyListener()
{
@Override
public void hierarchyChanged(HierarchyEvent e)
{
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && jsplitpane.isShowing())
{
jsplitpane.removeHierarchyListener(this);
setJSplitPaneDividerLocation(jsplitpane, proportionalLocation);
}
}
});
}
}
使用setResizeWeight(double);
中Component
與JSplitPane
下面確實太:
@Override
public void setVisible(final boolean b) {
super.setVisible(b);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
AppFrame.this.jSplitPane.setDividerLocation(0.9d);
}
});
}
只能設置拆分窗格分隔的位置,以百分比時拆分窗格是可見。您可以點擊拆分窗格所有者的事件以確定何時可以設置拆分器的位置。例如:
public class MyFrame extends JFrame {
public MyFrame() {
final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
// ... set up the split pane, and add to the frame ...
// Listen for the frame's "shown" event.
addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent componentEvent) {
// Set the divider location to 20%.
// This works because we know the pane is visible.
splitPane.setDividerLocation(.2);
// Stop listening for "shown" events.
removeComponentListener(this);
}
});
pack();
}
}
我發現setResizeWeight
和setDividerLocation
後來調用的組合提供了人們所期望的行爲:
_splitPane.setResizeWeight(0.5);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
_splitPane.setDividerLocation(0.5);
}
});
你會使用這個setResizeWeight(...)方法。 – camickr 2009-12-10 15:23:44