简体   繁体   English

GridBagLayout没有得到预期的结果

[英]GridBagLayout not getting expected result

Trying to understand how the GridBagLayout for Java works. 试图了解GridBagLayout for Java的工作原理。 Never used it before so its probably a stupid error that I've made. 以前从未使用过它,所以它可能是我犯的一个愚蠢的错误。

My objective is to place a JLabel at the top center of the page. 我的目标是将JLabel放在页面的顶部中心。 I've been using the java tutorials on Oracle but have had no luck. 我一直在使用Oracle上的Java教程,但是没有运气。 It seems the label remains in the center of the page. 标签似乎仍在页面的中心。 (center as in dead center of the x and y graph). (中心位于x和y图的死点)。

From what I understood, if I set the gridx and gridy constraint to 0 , the compiler will look at the top, first row of the program and place the text their. 据我了解,如果将gridxgridy约束设置为0 ,则编译器将在程序的第一行中查找文本并将其放置在文本中。 I then used the PAGE START anchor to place the text in the center of the page. 然后,我使用PAGE START锚将文本放置在页面的中央。 I'm not entirely sure what the weightx and weighty function does in my defence. 我不确定自己的防御中weightxweighty函数的作用。

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
    JLabel label = new JLabel("LOOK AT ME");

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

    //add the label to panel
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.PAGE_START;
    c.weightx = 0; //not sure what this does entirely
    c.gridx = 0; //first column
    c.gridy = 0; //first row
    panelDetail.add(label, c);

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

You're only adding one thing to the GBL using container, and so it will be centered. 您只需要使用容器将一件事添加到GBL中,因此它将居中。 If you add a 2nd component below your JLabel, the JLabel will show up at the top. 如果在JLabel下添加第二个组件,则JLabel将显示在顶部。 For example, 例如,

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.*;

public class Test2 {
   private static void createAndShowGui() {
      JPanel mainPanel = new JPanel(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.gridheight = 1;
      gbc.gridwidth = 1;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.anchor = GridBagConstraints.PAGE_START;

      mainPanel.add(new JLabel("Look at me!", SwingConstants.CENTER), gbc);   


      gbc.gridy = 1;
      gbc.gridheight = 10;
      gbc.gridwidth = 10;

      mainPanel.add(Box.createRigidArea(new Dimension(400, 400)), gbc);

      JFrame frame = new JFrame("Test2");
      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();
         }
      });
   }
}

Myself, I'd use BorderLayout if I wanted my JLabel to be at the top. 我自己,如果我希望我的JLabel位于顶部,我会使用BorderLayout。

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

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