如何使所有这些摆动控件具有相同的宽度?
我希望数据、视图和格式组具有与运动控件相同的宽度。我尝试了 setMinimumSize(new Dimension(250,30)) 但没有效果。
class DeeLiteControlPanel extends JPanel
{
public DeeLiteControlPanel()
{
setBounds(0, 140, 250, 550);
JPanel dataGroup = createGroupWithName("Data");
add(dataGroup);
JButton loadBtn = new JButton("Load data");
dataGroup.add(loadBtn);
JPanel viewGroup = createGroupWithName("View");
viewGroup.setLayout(new BorderLayout(5, 5));
add(viewGroup);
JButton skyBtn = new JButton("View from sky");
viewGroup.add(skyBtn, BorderLayout.NORTH);
final JToggleButton chaseBtn = new JToggleButton("Chase Vehicle");
viewGroup.add(chaseBtn, BorderLayout.EAST);
_vehicleList = new JComboBox();
_vehicleList.setEditable(false);
viewGroup.add(_vehicleList, BorderLayout.WEST);
JButton fitToWinBtn = new JButton("Fit to Window");
viewGroup.add(fitToWinBtn, BorderLayout.SOUTH);
JPanel formattingGroup = createGroupWithName("Formatting");
formattingGroup.setMinimumSize(new Dimension(250, 20));
formattingGroup.setLayout(new GridLayout(5, 0));
add(formattingGroup);
final JCheckBox showSurface = new JCheckBox("Show surface");
showSurface.setSelected(true);
formattingGroup.add(showSurface);
final JCheckBox showTerrain = new JCheckBox("Show terrain");
showTerrain.setSelected(true);
formattingGroup.add(showTerrain);
final JCheckBox showVehicleStatus = new JCheckBox("Show vehicle status");
showVehicleStatus.setSelected(true);
formattingGroup.add(showVehicleStatus);
JPanel pnl = createGroupWithName("Depth Stretch");
formattingGroup.add(pnl);
JSlider slider = new JSlider(0, 10);
pnl.add(slider);
}
public static JPanel createGroupWithName(String name)
{
JPanel pnl = new JPanel();
pnl.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), name));
pnl.setMinimumSize(new Dimension(250, 30));
return pnl;
}
}
I'd like the Data, View and Formatting group to have the same width as Motion controls. I tried setMinimumSize(new Dimension(250,30))
but it has no effect.
class DeeLiteControlPanel extends JPanel
{
public DeeLiteControlPanel()
{
setBounds(0, 140, 250, 550);
JPanel dataGroup = createGroupWithName("Data");
add(dataGroup);
JButton loadBtn = new JButton("Load data");
dataGroup.add(loadBtn);
JPanel viewGroup = createGroupWithName("View");
viewGroup.setLayout(new BorderLayout(5, 5));
add(viewGroup);
JButton skyBtn = new JButton("View from sky");
viewGroup.add(skyBtn, BorderLayout.NORTH);
final JToggleButton chaseBtn = new JToggleButton("Chase Vehicle");
viewGroup.add(chaseBtn, BorderLayout.EAST);
_vehicleList = new JComboBox();
_vehicleList.setEditable(false);
viewGroup.add(_vehicleList, BorderLayout.WEST);
JButton fitToWinBtn = new JButton("Fit to Window");
viewGroup.add(fitToWinBtn, BorderLayout.SOUTH);
JPanel formattingGroup = createGroupWithName("Formatting");
formattingGroup.setMinimumSize(new Dimension(250, 20));
formattingGroup.setLayout(new GridLayout(5, 0));
add(formattingGroup);
final JCheckBox showSurface = new JCheckBox("Show surface");
showSurface.setSelected(true);
formattingGroup.add(showSurface);
final JCheckBox showTerrain = new JCheckBox("Show terrain");
showTerrain.setSelected(true);
formattingGroup.add(showTerrain);
final JCheckBox showVehicleStatus = new JCheckBox("Show vehicle status");
showVehicleStatus.setSelected(true);
formattingGroup.add(showVehicleStatus);
JPanel pnl = createGroupWithName("Depth Stretch");
formattingGroup.add(pnl);
JSlider slider = new JSlider(0, 10);
pnl.add(slider);
}
public static JPanel createGroupWithName(String name)
{
JPanel pnl = new JPanel();
pnl.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), name));
pnl.setMinimumSize(new Dimension(250, 30));
return pnl;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
摆脱所有
setBounds(...)
的使用,并考虑对主 JPanel 使用 BoxLayout:例如,
Get rid of all use of
setBounds(...)
and consider using a BoxLayout for the main JPanel:e.g.,
将 GridBagLayout 设置为
DeeLiteControlPanel
或为所有面板调用setPreferredSize()
方法 - 数据、视图和格式。Set GridBagLayout to the
DeeLiteControlPanel
or callsetPreferredSize()
method for all panels - Data, View and Formatting.如果你愿意的话,你可以使用强大的GroupLayout。它工作完美,让您可以很好地控制组件的尺寸以及如何将它们的尺寸结合在一起。大多数情况下,我让 Netbeans 来处理这一切。
if you are up to it, you can use the powerful GroupLayout. it works perfect and gives you so much control on the sizes of the components and how you can tie their sizes together. mostly though i let Netbeans take care of all that.