如何使用GridBagLayout?

时间:2015-06-05 00:31:40

标签: java gridbaglayout

我希望有人能更深入地了解GridBagLayout

我已经阅读了Oracle文档,但我对此并不了解。

如果有人根据他使用GridBagLayout的个人经验编写类似'documentation'的内容,我将非常感激。

感谢!!!

1 个答案:

答案 0 :(得分:10)

我一直在阅读和使用很多关于GridBagLayout的内容,我认为您的问题是一个发布帖子的机会,可以帮助每个需要知道如何制作 JFrame或JDialogs的人没有"拖放" GUI有些IDES。

该职位将分为3个部分;基础知识,建议和频繁错误。

基本

首先,您需要了解什么是 GridBagLayout
GridBagLayout是 Layout Manager ,也是最复杂的经理之一。
它完全可以完全控制组件在窗口中的显示方式

此布局管理器使用两个非常简单的变量 gridx gridy
假设您的框架类似于 Excel gridx 用作变量,而 gridy 用作变量。
因此, gridx 对应1,2,3 ...而 gridy 对应于A,B,C ...(当然所有这些都指的是Excel)。

现在你已经了解了基础知识,并且使用GridBagLayout设计了Frames所需的抽象视图,让我们看看如何将它用于你的布局

//You need to import java.awt.GridBagLayout for using the Layout
import java.awt.GridBagLayout
import javax.swing.*; //"*" Is used for importing the whole class

public class Example{

    //First we need to declare the objects
    JPanel yourcontainer; 
    GridBagLayout ourlayout;

    public Example(){

        //And then we initialize them
        yourcontainer = new JPanel();
        ourlayout = new GridBagLayout();
        //Setting the layout manager for our container (in this case the JPanel)
        yourcontainer.setLayout(ourlayout);
    }
}

您也可以通过以下方式进行操作,但我更喜欢上一种方法。

public Example() {
    //We already declared and initialized our container
    yourcontainer.setLayout(new GridBagLayout());
}

现在我们的容器已经布局了,我们需要创建 GridBagConstraints 。您需要GridBagConstraints不仅用于创建 gridx / gridy ,还用于 gridwidth gridheight 。< br /> gridwidth&amp; gridheight 告诉Java 对象占用的行数或列数 创建GridBagConstraints的方法如下所示:

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;

gbc.gridwidth = 0;
gbc.gridheight= 0;  

还有一个 anchor函数,用于指示对象应该从哪里开始。
例如,在框架中心或线路开始处 这里有各种各样的锚点,所有这些都是:

记下他们是如何组织的。这是每个锚点在JFrame中的定位方式。

FIRST_LINE_START .... PAGE_START .... FIRST_LINE_END

LINE_START ...................... CENTER .............. LINE_END

LAST_LINE_START ........ PAGE_END ....... LAST_LINE_END

以下是编码方式:

 gbc.anchor = GridBagConstraints.LINE_START;

最后,还有另外一项重要功能, fill
此函数用于将对象扩展到gridsize,当Windows 可调整大小时,它非常有用。

有三种使用方法&#34;填充&#34;:

gbc.fill = GridBagConstraints.VERTICAL; //The object will extend only vertically.
gbc.fill = GridBagConstraints.HORIZONTAL; //The object will extend only horizontally.
gbc.fill = GridBagConstraints.BOTH; //The object will extend the same way in the 2 sides.

现在,我想向您展示一种非常有用的方法。如果您想要在容器中添加对象时避免大量代码,那就特别好了,现在您可以在一行中添加一个对象。
方法是这样的:

import java.awt.Component;
import java.awt.GridBagLayout;

public Class Cockatoo{

    public void addobjects(Component componente, Container yourcontainer, GridBagLayout layout, GridBagConstraints gbc, int gridx, int gridy, int gridwidth, int gridheigth){

        gbc.gridx = gridx;
        gbc.gridy = gridy;

        gbc.gridwidth = gridwith;
        gbc.gridheight = gridheight;

        layout.setConstraints(componente, gbc);
        yourcontainer.add(componente);
    }
}

以下是该方法的使用示例:

public class Test {

    JPanel container;
    GridBagLayout thelayout;
    GridBagConstraints gbc;

    JButton ok;

    public Test() { 

        Cockatoo v = new Cockatoo();//We create an instance of the class
        container = new JPanel();
        thelayout = new GridBagLayout();
        gbc = new GridBagConstraints();
        container.setLayout(thelayout);
        getContentPane().add(container);

        JLabel title = new JLabel("I like Cockatoos");
        ok = new JButton("Me too");

        v.addobjects(title, container, thelayout, gbc, 0, 0, 1, 1);
        v.addobjects(button, container, thelayout, gbc, 0, 1, 1, 1);
    }
}

最终建议和说明

  1. 建议首先声明,然后在构造函数中初始化。
  2. 请务必为GridBagConstraints指定短名称,因为您将对此变量进行大量处理。
  3. 默认情况下,Gridx和Gridy始终为0。
  4. 始终为宽度和高度指定至少1 !如果缺少宽度和高度属性,Java不显示对象,但这些仅在使用方法时发生,如果您要以手动方式进行,不要过于担心,但<强烈>仍然建议。
  5. gbc.anchor的默认值将始终为&#34; CENTER&#34;。
  6. 我没有谈论 RELATIVE REMAINDER ,因为帖子会非常大,但随时可以查看有关它的信息。 (我不喜欢使用它们。)
  7. 只有在需要更多内容时才将JPanel等变量声明为类的属性,因为它们会占用内存。只使用一次的JLabel应该在构造函数中声明,因为当构造函数完成运行时,声明的变量不再存在。这是一个非常好的练习,可以节省内存,这在非常大的应用程序中非常重要,应该能够在平庸的PC中运行。

    1. 锚点后面的所有添加对象都会受到影响,请注意这一点。填充也是如此,如果你不想要一个对象调整大小,只需要:NONE而不是VERTICAL,HORIZONTAL或BOTH。
  8. 我建议你先完全理解如何使用GridBagLayout,然后使用该方法;请记住,我们主要使用节省时间和空间的方法。

  9. 常见错误

    1. 使用方法添加对象时,不添加宽度或高度会导致失败,因为对象不会出现。我也建议你设置它,尽管你没有使用这种方法。

    2. 不使用该方法时,添加一个对象而不添加&#34;,&#34;然后GridBagConstraints会导致逻辑错误,这意味着应用程序将运行但不会显示对象。

    3. 写这篇文章的资源:Oracle文档,我的经验以及@MadProgrammer等其他人的帮助。