【问题标题】:Is it possible to make the elements inside a JPanel with GridBagLayout to start from the top left?是否可以使用 GridBagLayout 使 JPanel 中的元素从左上角开始?
【发布时间】:2023-03-09 14:01:01
【问题描述】:

我有一个 Swing 表单,其中包含 JScrollPane(activityScrollPane) 用于 JPanel(activityPanel)。该面板包含一个 JTextField 和一个 JButton(用于向面板添加更多字段)。现在的问题是元素从面板的中心开始,如下图所示(边界标记为activityScrollPane 边界)

以下是我目前用来制作滚动窗格和相关组件的代码。

 //part of the code for creating the ScrollPane

        final JPanel activityPanel = new JPanel(new GridBagLayout());
        gbc.gridx=0;
        gbc.gridy=0;
        JScrollPane activityScrollPane = new JScrollPane(activityPanel); 
        //adding activity fields
        activityFields = new ArrayList<JTextField>();
        fieldIndex = 0;
        activityFields.add(new JTextField(30));

        final GridBagConstraints activityGBC = new GridBagConstraints();
        activityGBC.gridx=0;
        activityGBC.gridy=0;
        activityGBC.anchor = GridBagConstraints.FIRST_LINE_START;
        activityPanel.add(activityFields.get(fieldIndex),activityGBC);

        fieldIndex++;
        JButton btn_more = (new JButton("more"));
        activityGBC.gridx=1;
        activityPanel.add(btn_more,activityGBC);

如何使 JTextField 和 JButton 或任何组件出现在 JScrollPane 的左上角。我已经尝试过使用

activityConstraints.anchor = GridBagConstraints.NORTHWEST;

正如 SO post 中指出的那样,但它似乎根本不起作用。

【问题讨论】:

    标签: java swing gridbaglayout


    【解决方案1】:

    尝试使用 BorderLayoutcontrols.setLayout(new BorderLayout());,然后将其应用于您的 JPanel controls.add(yourPanel, BorderLayout.PAGE_START);

    我也有GridBagLayout 的问题,所以我用BorderLayout 解决了它,它工作得很好。


    所以我为你的小例子写了:

    private void initComponents() {
    
            controls = new Container();
            controls = getContentPane();
            controls.setLayout(new BorderLayout());
    
            panel = new JPanel();
            panel.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
    
            field = new JTextField(20);
            c.gridx = 0;
            c.gridy = 0;
            panel.add(field, c);
    
            one = new JButton("Go!");
            c.gridx = 1;
            c.gridy = 0;
            panel.add(one, c);
    
            controls.add(panel, BorderLayout.PAGE_START);
    
        }  
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      很抱歉,我的回答与您的要求不符,但是您为什么不使用 GroupLayout 而不是 GridBag Layout,这样更容易处理

      【讨论】:

        【解决方案3】:

        我认为这可能是简单可行的,你可以

        到这个JPanel

        • JPanels 包含JComponent 放入GridLayout(关于滚动的注意,您必须更改滚动增量)

        或者使用最复杂的JComponents作为

        • JPanels 包含JComponent 作为项目添加到JList

        • JPanels 包含JComponent 作为JTable 的行(只有一个列,有或没有TableHeader

        【讨论】:

          【解决方案4】:

          您只是忘记提供任何weightx/weighty 值,至少提供一个非零值即可。看看这个代码示例:

          import java.awt.*;
          import javax.swing.*;
          public class GridBagLayoutDemo
          {
              private JTextField tfield1;
              private JButton button1;
          
              private void displayGUI()
              {
                  JFrame frame = new JFrame("GridBagLayout Demo");
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
                  JPanel contentPane = new JPanel();
                  contentPane.setLayout(new GridBagLayout());
          
                  tfield1 = new JTextField(10);
                  GridBagConstraints gbc = new GridBagConstraints();
                  gbc.anchor = GridBagConstraints.FIRST_LINE_START;   
                  gbc.weightx = 1.0;
                  gbc.weighty = 1.0;  
                  gbc.gridx = 0;
                  gbc.gridy = 0;
                  gbc.fill = GridBagConstraints.HORIZONTAL;       
                  contentPane.add(tfield1, gbc);
          
                  button1 = new JButton("More");
                  gbc.gridx = 1;
                  gbc.gridy = 0;
                  gbc.fill = GridBagConstraints.NONE;
                  contentPane.add(button1, gbc);
          
                  frame.setContentPane(contentPane);
                  frame.pack();
                  frame.setVisible(true);
              }
          
              public static void main(String... args)
              {
                  SwingUtilities.invokeLater(new Runnable()
                  {
                      public void run()
                      {
                          new GridBagLayoutDemo().displayGUI();
                      }
                  });
              }
          }
          

          最新编辑:沿 Y 轴没有间距

          import java.awt.*;
          import javax.swing.*;
          public class GridBagLayoutDemo
          {
              private JTextField tfield1;
              private JButton button1;
              private JTextField tfield2;
              private JButton button2;
          
              private void displayGUI()
              {
                  JFrame frame = new JFrame("GridBagLayout Demo");
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
                  JPanel contentPane = new JPanel();
                  contentPane.setLayout(new GridBagLayout());
          
                  tfield1 = new JTextField(10);
                  GridBagConstraints gbc = new GridBagConstraints();
                  gbc.anchor = GridBagConstraints.FIRST_LINE_START;   
                  gbc.weightx = 1.0;
                  //gbc.weighty = 0.2;    
                  gbc.gridx = 0;
                  gbc.gridy = 0;
                  gbc.fill = GridBagConstraints.HORIZONTAL;       
                  contentPane.add(tfield1, gbc);
          
                  button1 = new JButton("More");
                  gbc.gridx = 1;
                  gbc.gridy = 0;
                  gbc.fill = GridBagConstraints.NONE;
                  contentPane.add(button1, gbc);
          
                  tfield2 = new JTextField(10);
                  gbc.weightx = 1.0;
                  gbc.weighty = 0.2;  
                  gbc.gridx = 0;
                  gbc.gridy = 1;
                  gbc.fill = GridBagConstraints.HORIZONTAL;       
                  contentPane.add(tfield2, gbc);
          
                  button2 = new JButton("More");
                  gbc.gridx = 1;
                  gbc.gridy = 1;
                  gbc.fill = GridBagConstraints.NONE;
                  contentPane.add(button2, gbc);
          
                  JScrollPane scroller = new JScrollPane(contentPane);
          
                  frame.add(scroller);
                  frame.pack();
                  frame.setVisible(true);
              }
          
              public static void main(String... args)
              {
                  SwingUtilities.invokeLater(new Runnable()
                  {
                      public void run()
                      {
                          new GridBagLayoutDemo().displayGUI();
                      }
                  });
              }
          }
          

          【讨论】:

          • 它适用于第一个字段。但是当我尝试添加更多 JTextFields 时,它们会平均分布在整个高度上。有没有办法防止这种情况发生。
          • @Ankit :太简单了,只需评论这一行 gbc.weighty = 1.0,尽管保持 weightx 的值不变 :-),如果你想要别的东西,我不能不明白:-)
          • 从顶部开始的第一部分很好。但问题是,如果我使用weighty,那么它会从顶部开始,但是当我添加更多字段时,它们不会从前一个字段的下方开始。相反,它从中心开始。我想我使用了错误的布局。
          • 只需使用weightx,不要使用weighty,如果您不想在组件之间沿Y轴间隔,请尝试此最新编辑,看看这是您想要的:- )
          【解决方案5】:

          在右侧添加一个面板,在底部添加一个面板。

          右侧面板:权重 X 设置为 1.0。 将填充设置为水平

          底部面板:权重 Y 设置为 1.0。 将 Fill 设置为 vertical

          可能有更好的方法,但这个方法对我有用。

          【讨论】:

            猜你喜欢
            • 2011-09-15
            • 1970-01-01
            • 1970-01-01
            • 2021-02-14
            • 2011-06-30
            • 2017-02-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多