我希望数据,视图和格式组的宽度与运动控件的宽度相同。我试过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;
}
}
答案 0 :(得分:5)
摆脱setBounds(...)
的所有使用,并考虑使用BoxLayout作为主要JPanel:
public DeeLiteControlPanel() {
// setBounds(0, 140, 250, 550);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
例如,
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class DeeLiteControlPanel extends JPanel {
public DeeLiteControlPanel() {
// setBounds(0, 140, 250, 550);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
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);
JComboBox _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;
}
private static void createAndShowGui() {
DeeLiteControlPanel mainPanel = new DeeLiteControlPanel();
JFrame frame = new JFrame("DeeliteControlPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
答案 1 :(得分:3)
将GridBagLayout设置为所有面板的DeeLiteControlPanel
或调用setPreferredSize()
方法 - 数据,视图和格式。
class DeeLiteControlPanel extends JPanel
{
public DeeLiteControlPanel()
{
setLayout(new GridBagLayout());
GridBagConstraints gs=new GridBagConstraints();
gs.anchor=GridBagConstraints.NORTHWEST;
gs.fill=GridBagConstraints.BOTH; //or HORIZONTAL
gs.gridx=0;
gs.gridy=0;
add(dataGroup,gs);
gs.gridx=0;
gs.gridy=1;
add(viewGroup,gs);
....
}
}
答案 2 :(得分:1)
如果您愿意,可以使用强大的GroupLayout。它非常完美,可以让您对组件的尺寸以及如何将它们的尺寸组合在一起进行如此大的控制。尽管我让Netbeans负责这一切。