如何使所有这些摆动控件具有相同的宽度?

发布于 2024-12-27 12:01:33 字数 2232 浏览 1 评论 0原文

我希望数据、视图和格式组具有与运动控件相同的宽度。我尝试了 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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野の 2025-01-03 12:01:33

摆脱所有 setBounds(...) 的使用,并考虑对主 JPanel 使用 BoxLayout:

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();
         }
      });
   }
}

Get rid of all use of setBounds(...) and consider using a BoxLayout for the main JPanel:

public DeeLiteControlPanel() {
   // setBounds(0, 140, 250, 550);
   setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

e.g.,

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();
         }
      });
   }
}
夜无邪 2025-01-03 12:01:33

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);
      ....
     }
  }

Set GridBagLayout to the DeeLiteControlPanel or call setPreferredSize() method for all panels - Data, View and Formatting.

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);
      ....
     }
  }
子栖 2025-01-03 12:01:33

如果你愿意的话,你可以使用强大的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文