下面是使用幾個嵌套的JSplitPanes
創建的簡單佈局的代碼。允許嵌套的JSplitPanes控制父JSplitPanes
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class CDBurner extends JFrame {
private static final long serialVersionUID = -6027473114929970648L;
JSplitPane main, folder, rest;
JPanel centeral, folders, favourites, tasks;
JLabel label;
private CDBurner() {
super("Dan's CD Burner");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 1));
getContentPane().setBackground(Color.black);
createLayout();
pack();
setMinimumSize(getSize());
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
setVisible(true);
requestFocus();
}
private void createLayout() {
createPanels();
rest = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, centeral, tasks);
rest.setResizeWeight(1);
rest.setContinuousLayout(true);
rest.setOneTouchExpandable(true);
folder = new JSplitPane(JSplitPane.VERTICAL_SPLIT, favourites, folders);
folder.setResizeWeight(0.35);
folder.setContinuousLayout(true);
folder.setOneTouchExpandable(true);
main = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, folder, rest);
main.setResizeWeight(0);
main.setContinuousLayout(true);
main.setOneTouchExpandable(true);
getContentPane().add(main);
}
private void createPanels() {
createFolders();
createCenter();
createTaskSpool();
createFavourites();
}
private void createFolders() {
folders = new JPanel(new GridLayout(1, 1));
label = new JLabel("Folder");
folders.setMinimumSize(new Dimension(300, 100));
folders.add(label);
}
private void createCenter() {
centeral = new JPanel(new GridLayout(1, 1));
label = new JLabel("Central");
centeral.add(label);
centeral.setMinimumSize(new Dimension(300, 100));
}
private void createTaskSpool() {
tasks = new JPanel(new GridLayout(1, 1));
label = new JLabel("Task");
tasks.setMinimumSize(new Dimension(300, 100));
tasks.add(label);
}
private void createFavourites() {
favourites = new JPanel(new GridLayout(1, 1));
label = new JLabel("Fav");
favourites.setMinimumSize(new Dimension(300, 100));
favourites.add(label);
}
public static void main(String[] args) {
new CDBurner();
}
}
由於線rest.setResizeWeight(1);
你可以拖動main (JSplitPane)
分頻器的權利,這將縮小tasks JPanel
到直到兩個在rest
的JPanels
是最小尺寸。但是,如果您嘗試做相反的事情,則什麼都不會發生。請參閱下面的圖片以查看問題。
如果GUI看起來像這樣,您可以拖動main
分頻器。
,你會得到這樣的結果。
但是,如果它看起來像這一點,並試圖拖動rest
分頻器沒有任何反應。
這是因爲rest JSplitPane
雙方已經在他們的最小尺寸。
問題
我怎樣才能讓這個當我拖動rest
分隔它會影響main JSplitPane
,這樣既所示的操作在上面的圖像是可能的嗎?
但是,謝謝你的回答,但它似乎不太好。其餘的分頻器現在非常跳躍,隨機停止移動,你必須重新點擊它。雖然,這看起來似乎是正確的道路。我會研究它 – Dan