我创建了一个父容器,我在其上放置了几个JPanel
个对象,每个对象包含多个JButton
个对象。
我创建父面板,添加GridBagConstraints
,然后将每个子面板添加到父面板:
final JPanel options = new JPanel(new GridBagLayout());
options.setBorder(new TitledBorder("Select Option"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.anchor = GridBagConstraints.NORTH;
options.add(findPanel, gbc);
options.add(addPanel, gbc);
options.add(changePanel, gbc);
options.add(dropPanel, gbc);
gbc.weighty = 1.0;
options.add(new JPanel(), gbc);
options.add(new JPanel(), gbc);
用于占用我想要的面板下的额外空间。效果很好....直到我想在用户交互后更改父级的内容:
partnoFai.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
options.remove(findPanel);
options.remove(addPanel);
options.remove(changePanel);
options.remove(dropPanel);
options.add(partnoFaiInp, gbc);
gbc.weighty = 1.0;
options.add(new JPanel(), gbc);
frame.pack();
frame.validate();
}
} );
当我希望它位于顶部时,它会将新面板options.add(partnoFaiInp, gbc);
添加到父级的中间位置。为什么不会gbc.anchor = GridBagConstraint.NORTH;
将新面板保留在面板的NORTH
中?
感谢任何帮助。
答案 0 :(得分:1)
你必须从堆栈的角度来考虑options.add(findPanel, gbc);
options.add(addPanel, gbc);
options.add(changePanel, gbc);
options.add(dropPanel, gbc);
gbc.weighty = 1.0;
options.add(new JPanel(), gbc);
首次使用...
设置面板时{findPanel, addPanel, changePanel, dropPanel, JPanel}
容器有一个类似于options.remove(findPanel);
options.remove(addPanel);
options.remove(changePanel);
options.remove(dropPanel);
使用类似的东西删除组件时
{JPanel}
容器现在有一个类似于options.add(partnoFaiInp, gbc);
gbc.weighty = 1.0;
options.add(new JPanel(), gbc);
然后当您使用...
添加新组件时{JPanel, partnoFaiInp, JPanel}
容器现在有一个类似于options.add(partnoFaiInp, gbc, 0);
frame.pack();
frame.validate();
因此,您可以在添加面板时指定面板的插入点,而不是添加另一个“填充”组件...
{partnoFaiInp, JPanel}
容器现在有一个类似于tell application "System Events" to repeat 6 times
tell process "Photoshop" to repeat until frontmost is true
set frontmost to true
delay 1
end repeat
keystroke "/" using command down
keystroke "c" using command down
keystroke tab
tell process "Illustrator" to repeat until frontmost is true
set frontmost to true
delay 1
end repeat
keystroke "v" using command down
keystroke return
keystroke "-"
keystroke space
end repeat