简体   繁体   English

GridBagLayout Java-weightY导致意外结果

[英]GridBagLayout Java - weightY causing unexpected results

Trying to understand GridBagLayout and to fully work it. 试图了解GridBagLayout并充分发挥作用。 I'm trying some examples. 我正在尝试一些例子。

What my aim is to have three buttons directly below one another, and three buttons going across the screen. 我的目标是在彼此之间直接位于三个按钮之间,并在屏幕上显示三个按钮。

Now I can get the first two rows working fine but the third row just goes straight to the middle of the screen. 现在,我可以使前两行正常工作,但第三行直接进入屏幕中间。

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

Edit 编辑

The button label 5, should be directly underneath the button label 4: 按钮标签5应该位于按钮标签4的下方:

Like so: 像这样:

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

at the moment its like: 此刻它像:

[----LABEL 4 ---]


(dont want this gap between the two?)



[----LABEL 5 ---]

If you want the buttons all at the top with minimal gap between them, then either consider using a GridLayout and placing your JButtons into the GridLayout using JPanel, and then have your main JPanel use a BorderLayout add the button holding JPanel to your main JPanel BorderLayout.PAGE_START . 如果您希望所有按钮都位于顶部,并且它们之间的间隔最小,则可以考虑使用GridLayout并使用JPanel将JButtons放入GridLayout中,然后让主JPanel使用BorderLayout将包含JPanel的按钮添加到主JPanel BorderLayout.PAGE_START Or the button holding JPanel could use a GridBagLayout, but I'd still nest it into a main JPanel and again at the BorderLayout.PAGE_START position. 或者,持有JPanel的按钮可以使用GridBagLayout,但我仍将其嵌套到主JPanel中,然后再次嵌套在BorderLayout.PAGE_START位置。 For example, 例如,

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

Which creates this: 这创建了这个:

在此处输入图片说明

And as per my comment, if this image doesn't match the overall structure of the GUI you're trying to create, then please show us an image of just what it is you're trying to achieve. 根据我的评论,如果该图像与您要创建的GUI的整体结构不匹配,那么请向我们显示一张您要实现的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM