我正在尝试在我的程序中设置我的面板,而我没有得到我认为的GridBagLayout的预期行为(当然它正确,GIGO)。我想要页面上的一些组件来调整大小。
我设置它的方式分为两部分:
myPlayerContentPane =
new PlayerContentPane(myEventListener);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty =1;
this.add(myPlayerContentPane,c);
myPlayerContentPane.setBorder(BorderFactory.createLineBorder(Color.black));
当我调整框架大小时,此子窗格应该展开,对吗?这是因为重量和填充命令。
setLayout(new GridBagLayout());
c.insets = new Insets(1, 1, 1, 1);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(newCharacterButton,c);
newCharacterButton.addActionListener(new ActionListener(){
...
});
c.gridx = 1;
c.gridy = 0;
c.gridheight = 1;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(newCharacterName,c);
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(loadCharacterButton,c);
loadCharacterButton.addActionListener(new ActionListener() {
...
});
c.gridx = 2;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(editCharacterButton,c);
c.gridx = 2;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(deleteCharacterButton,c);
deleteCharacterButton.addActionListener(new ActionListener(){
...
});
c.gridx = 2;
c.gridy = 2;
c.fill = GridBagConstraints.BOTH;
c.gridheight = 3;
c.ipady = 200;
c.weighty = 1;
this.add(characterListScroller,c);
c.gridx = 0;
c.gridy = 4;
c.fill = GridBagConstraints.BOTH;
c.gridwidth = 2;
c.ipady = 200;
c.weighty =1;
this.add(characterSummaryScroller,c);
现在,当程序加载时,它看起来很好,我希望在调整大小时,两个JScrollPanes会调整大小。但是,即使我删除了在代码中设置首选大小的所有实例(即使是框架本身),我也看不到任何调整大小的行为。
这是程序首次加载时的设置,捕捉到最小尺寸:
这是我调整框架大小的时候,面板不跟风。
现在我的管理器窗格是在一个带有CardLayout的框架内,我刚刚检查过,并且不会随框架调整大小。这是顶级窗格(我设置)。我怎样才能让它全部调整大小?
答案 0 :(得分:2)
如果将顶部面板包装在另一个带有BorderLayout的JPanel中(假设您将面板放在CENTER中),则应该获得所需的行为。 BorderLayout自动调整到框架大小并强制其中心面板占据整个可用区域。