GridBagLayout Java - weightY导致意外结果

时间:2015-03-13 21:43:15

标签: java swing layout

尝试理解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 ---]

1 个答案:

答案 0 :(得分:0)

如果你想让按钮全部位于顶部并且它们之间的间隙最小,那么要么考虑使用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();
         }
      });
   }
}

创造了这个:

enter image description here

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