简体   繁体   English

摇摆:GridBagLayouts锚不能嵌套在另一个GridBagLayout中

[英]Swing: GridBagLayouts anchor does not work nested in an other GridBagLayout

I am setting a first GridBagLayout to a main JPanel . 我将第一个GridBagLayout设置为主JPanel To this Panel, ten other panels were added with this layout (outerConstraints). 向此面板添加了其他十个具有此布局的面板(outerConstraints)。 The sub-panels contains an amount of texts, which should be placed WEST with a new Layout and innerConstraints . 子面板包含大量文本,应使用新的Layout和innerConstraints将其放置在WEST innerConstraints But they center. 但他们居中。

After testing a little bit, i found out that the texts / images ARE placed WEST, but only, if the outerConstraints do not fill the white panel horizontally. 经过一点测试后,我发现文本/图像放置在WEST上,但outerConstraints是,如果outerConstraints没有水平填充白色面板。 The problem is, that i can not remove this command, because i need every panel to have the same width. 问题是,我无法删除此命令,因为我需要每个面板都具有相同的宽度。

Is there a possiblity to allow outerConstraints to fill up AND to make its nested GridBagLayout positioning the text left? 是否有可能允许outerConstraints填充并使其嵌套的GridBagLayout将文本左移?

setLayout(new GridBagLayout());

GridBagConstraints outerConstraints = new GridBagConstraints();
outerConstraints.insets = new Insets(5, 5, 5, 5);
outerConstraints.fill = GridBagConstraints.HORIZONTAL;    // seems to be reason, but needed.

    for (int i = 0; i < 10; i++) {

        outerConstraints.gridx = i % 2;
        outerConstraints.gridy = i / 2;

        JPanel agentVisitCard = new JPanel();
        add(agentVisitCard, outerConstraints);

        GridBagConstraints innerConstraints = new GridBagConstraints();
        innerConstraints.anchor = GridBagConstraints.WEST;                 //fails
        innerConstraints.insets = new Insets(5, 5, 5, 5);

        GridBagLayout layout = new GridBagLayout();
        agentVisitCard.setLayout(layout);

        (...)

        JLabel label = new JLabel("Agent " + (i + 1) + ":");

        innerConstraints.gridx = 0;
        innerConstraints.gridy = 0;

        agentVisitCard.add(label, innerConstraints);

Agent 3 does not align left: 特工3不向左对齐: 在此处输入图片说明

The reason the anchor wasn't working is because the components had no weight. 锚不起作用的原因是组件没有重量。 Without that, the area given to the component is only the minimum size needed, ie the size of the component itself, and no bigger. 否则,分配给组件的面积仅为所需的最小尺寸,即组件本身的大小,而不会更大。

Here is a quote from the swing tutorials (under the weightx, weighty section) that explains it: 这是秋千教程 (在weightx,weighty部分下)的引言,解释了它:

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. 除非您为weightx或weighty指定至少一个非零值,否则所有组件都将在其容器的中心聚集在一起。 This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container. 这是因为当权重为0.0(默认值)时,GridBagLayout会在其单元格网格和容器边缘之间放置任何多余的空间。

By default, the weightx and weighty are 0, which is why it was not working for you. 默认情况下,weightx和weighty为0,这就是为什么它对您不起作用的原因。 Since you only wanted to anchor it horizontally, setting the weightx to a non-zero value allowed that to work. 由于您只想将其水平锚定,因此将weightx设置为非零值可使其起作用。 If you wanted a vertical anchor, you would need to set the weighty as well. 如果需要垂直锚,则还需要设置权重。

An explanation of how the weightx and weighty values should be used follows that: 有关应如何使用weightx和weighty值的说明如下:

Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. 通常,权重的极限值设置为0.0和1.0:必要时使用中间的数字。 Larger numbers indicate that the component's row or column should get more space. 较大的数字表示组件的行或列应获得更多的空间。

Although they recommend using values between 0.0 and 1.0, it is not necessary. 尽管他们建议使用0.0到1.0之间的值,但这不是必需的。 Any double value will work. 任何double值都将起作用。 What's important is the ratio between the weights of components if you have several of them. 重要的是,如果您有多个组件,则它们之间的重量比。

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

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