GridBagLayout Java - weightY 导致意外结果

GridBagLayout Java - weightY causing unexpected results

试图理解 GridBagLayout 并全力以赴。我正在尝试一些示例。

我的目标是让三个按钮彼此正下方,并且三个按钮横跨屏幕。

现在我可以让前两行正常工作,但第三行直接到屏幕中间。

import javax.swing.*;
import java.awt.*;

class test
{
    public static void main (String Args [])
    {
    //frame and jpanel stuff
    JFrame processDetail = new JFrame("Enter information for processes");
    JPanel panelDetail = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    //label to add on top centre
    JButton label = new JButton("Proccess:");
    JButton label2 = new JButton("Arrival Time");
    JButton label3 = new JButton("Quanta Time");
    JButton label4 = new JButton("woooo");
    JButton label5 = new JButton("LdsdaE");
    JButton label6 = new JButton("affafa 666");

    //set size of frame and operation
    processDetail.setSize(500,500);
    processDetail.setDefaultCloseOperation(processDetail.EXIT_ON_CLOSE);

    //add the label to panel
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.5;
    //c.weighty = 0.5;
    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.PAGE_START;
    panelDetail.add(label, c); //row 1 left

    c.gridx=1;
    panelDetail.add(label2, c); //row 1 middle

    c.gridx=2;
    panelDetail.add(label3, c); //row 1right

    c.weighty=0.1;
    c.gridx=0;
    c.gridy=1;
    panelDetail.add(label4, c); //row 2 left

    c.gridx=0;
    c.gridy=2;
    panelDetail.add(label5, c); //row 3 left

    processDetail.add(panelDetail);
    processDetail.setVisible(true);
    }
}

编辑

按钮标签5,应该直接在按钮标签4的下面:

像这样:

[----LABEL 4 ---]
[----LABEL 5 ---]

目前是这样的:

[----LABEL 4 ---]


(dont want this gap between the two?)



[----LABEL 5 ---]

如果您希望所有按钮都位于顶部且它们之间的间隙最小,那么可以考虑使用 GridLayout 并使用 JPanel 将 JButton 放入 GridLayout 中,然后让您的主 JPanel 使用 BorderLayout 添加包含 JPanel 的按钮到您的主 JPanel BorderLayout.PAGE_START。或者,包含 JPanel 的按钮可以使用 GridBagLayout,但我仍将其嵌套到主 JPanel 中,并再次嵌套在 BorderLayout.PAGE_START 位置。例如,

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;

public class Test4 extends JPanel {
   private static final long serialVersionUID = 1L;
   private static final int GAP = 3;

   public Test4() {
      JPanel buttonHoldingPanel = new JPanel(new GridLayout(3, 3, GAP, GAP));
      buttonHoldingPanel.add(new JButton("Process"));
      buttonHoldingPanel.add(new JButton("Arrival Time"));
      buttonHoldingPanel.add(new JButton("Quanta Time"));
      buttonHoldingPanel.add(new JButton("woooo"));
      buttonHoldingPanel.add(new JLabel());
      buttonHoldingPanel.add(new JLabel());
      buttonHoldingPanel.add(new JButton("LdsdaE"));
      buttonHoldingPanel.add(new JLabel());
      buttonHoldingPanel.add(new JLabel());

      setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
      setLayout(new BorderLayout());
      add(buttonHoldingPanel, BorderLayout.PAGE_START);
      add(Box.createRigidArea(new Dimension(400, 300)), BorderLayout.CENTER);
   }

   private static void createAndShowGui() {
      Test4 mainPanel = new Test4();

      JFrame frame = new JFrame("Test4");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_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();
         }
      });
   }
}

创建这个:

根据我的评论,如果此图像与您尝试创建的 GUI 的整体结构不匹配,请向我们展示您正在尝试实现的图像。