如何使用GridBagLayout? [英] How to Use GridBagLayout?

查看:85
本文介绍了如何使用GridBagLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以对我已经阅读了Oracle文档,但是我不太了解.

I have read the Oracle Documentation, but I didn't understand it very well.

如果有人可以根据使用GridBagLayout的个人经验写出'documentation'之类的东西,我将不胜感激.

I will be very grateful if someone could write something like a 'documentation' based on his personal experience with using GridBagLayout..

谢谢!

推荐答案

我一直在阅读和使用有关

I've been reading and using a lot about GridBagLayout, and I saw your question as an opportunity to make a post that could help everyone that needs to know how to make JFrames or JDialogs without the "Drag and Drop" GUI some IDES have.

帖子将分为3个部分;基础知识,建议和常见错误.

因此,首先,您需要了解什么是 GridBagLayout .
GridBagLayout是
布局管理器 ,也是最复杂的经理之一.
完全控制组件在窗口中的显示方式是完美的选择.

So first of all, you will need to understand what is GridBagLayout.
GridBagLayout is a Layout Manager, and one of the most complex managers.
It's perfect for having total control over how the components are displayed in your Window.

此布局管理器使用两个非常简单的变量, gridx

This layout manager works with two very simple variables, gridx and gridy.
Imagine your frame to be something like Excel, the gridx works as the Row variable, and gridy works as the Column variable.
So, gridx corresponds to 1,2,3... and gridy corresponds to A,B,C... (All of this referring to Excel of course).

现在您已经了解了基础知识,并且拥有使用GridBagLayout设计框架所需的抽象视图,让我们看看如何将其用于布局.

Now that you know the basics, and have the abstract view needed to design Frames using the GridBagLayout, let's see how to use it for your layout.

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

您也可以按照以下方式进行操作,但是我更喜欢以前的方法.

You can also do it the following way, but I prefer the previous method.

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

因此,现在我们有了带有布局的容器,我们需要创建 GridBagConstraints .您不仅需要GridBagConstraints来创建 gridx/gridy ,而且还需要

So now that we have our container with our layout, we need to create our GridBagConstraints. You need the GridBagConstraints not only for creating your gridx/gridy, but also for gridwidth and gridheight.
gridwidth & gridheight tells Java how many rows or columns the object occupies.
The way to create your GridBagConstraints is shown below:

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

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

还有一个 anchor 函数,用于指示对象应从何处开始.
例如,在框架的中心或线的起点.
锚的种类繁多,全部都是:

There's also an anchor function, that is used to indicate where the object should start.
For example, in the Center of the Frame or at the Line Start.
There's a variety for anchors, here's all of them:

请注意它们的组织方式.这是每个锚点在JFrame中的定位方式.

FIRST_LINE_START .... PAGE_START .... FIRST_LINE_END

FIRST_LINE_START....PAGE_START.... FIRST_LINE_END

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

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

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

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

这是编写代码的方法:

 gbc.anchor = GridBagConstraints.LINE_START;

最后,还有另一个重要功能,

Finally, there's one other important function, fill.
This function is used for extending the object to the gridsize, and it is very useful when your Windows is resizable.

有3种使用填充"的方式:

There are 3 ways to use "fill":

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.

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

Now, there's one method I want to show you that's very useful. It's especially good if you want to avoid large piles of code when you're adding objects to your container, now you can add one object in one line.
The method is like this:

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

下面是如何使用该方法的示例:

Here's an example of how the method can be used:

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个!如果width和height属性丢失,Java不会显示对象,但是这些仅在使用方法时发生,如果您要以手动方式进行操作,不必太担心它,但是仍然推荐.
  5. gbc.anchor的默认值始终为"CENTER".
  6. 我没有谈论 RELATIVE REMAINDER ,因为该帖子会很大,但是您可以随时查看有关它的信息.(我更喜欢不使用它们.)
  7. 仅在需要它们进行其他操作时才将诸如JPanel之类的变量声明为类的属性,因为它们会占用内存.应该只在构造函数内部声明将只使用一次的JLabel,因为当构造函数完成运行时,声明的变量不再存在.这是一个很好的做法,可以节省内存,这对于应该可以在甚至普通PC上运行的超大型应用程序来说,这是非常重要的事情.

  1. It is recommended to first declare and then initialize in the constructor.
  2. Be sure to assign a short name to GridBagConstraints, as you will deal with this variable quite a lot.
  3. Gridx and Gridy are always 0 by default.
  4. Always assign at least 1 to width and height! Java doesn't display the objects if the width and height attributes are missing, but these only happens when using method, if you are going to do it the manual way, don't worry too much about it, but it is still recommended.
  5. The default value for gbc.anchor will always be "CENTER".
  6. I didn't talk about RELATIVE and REMAINDER, because the post will be very large, however feel free to look info about it. (I prefer not using them).
  7. Only declare variables like JPanel as a property of the class when you need them for further things, because they consume memory. JLabels that will be only be used once should be declared inside the constructor, because when the constructor finishes running, the variables that were declared don't exist any more. This is a very good practice that can save memory, which is a really important thing in very large apps that should be able of running in even mediocre PC's.

  1. 请注意,锚点后面所有添加的对象都将受到它的影响.填充也会发生同样的情况,如果您不希望调整对象的大小,只需输入:NONE而不是VERTICAL,HORIZONTAL或BOTH.

  • 我建议您首先完全了解如何使用GridBagLayout,然后再使用该方法;请记住,我们主要使用这种方法来节省时间和空间.

    常见错误

    1. 使用方法添加对象时不添加宽度或高度,将导致失败,因为该对象将不会显示.我也建议您也设置它,尽管您没有使用该方法.

    1. Not adding width or height when using a method to add the object, will result in a failure, as the object won't appear. I also recommend setting it too although you aren't using the method.

    当不使用该方法时,先添加对象而不先添加,",然后添加GridBagConstraints,这将导致逻辑​​错误,这意味着应用程序将运行,但不会显示对象.

    When not using the method, adding an object without putting a "," and then the GridBagConstraints, will cause a logic error, that means the application will run but it will not show the objects.

    撰写本文的资源:Oracle文档,我的经验和其他人(例如@MadProgrammer)的帮助.

    Resources to write this: Oracle Documentation, my experience and help of others such as @MadProgrammer.

    这篇关于如何使用GridBagLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆