首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么GroupLayout没有按要求得到结果?

为什么GroupLayout没有按要求得到结果?
EN

Stack Overflow用户
提问于 2015-01-06 14:48:18
回答 1查看 181关注 0票数 0

在这里,我想添加按4-3-3阵型在足球中的按钮,但我得到了3-3-3。我怎样才能把四个按钮放在一排?我已经被推荐到这个网站:2.html

代码:

代码语言:javascript
复制
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import static javax.swing.GroupLayout.Alignment.*;

class Abc extends JFrame
{
JButton b[];

Abc()
{
b=new JButton[11];
JPanel jp=new JPanel();
for(int i=0;i<b.length;i++)
{
b[i]=new JButton();
}
GroupLayout layout=new GroupLayout(jp);
jp.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

layout.setHorizontalGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(LEADING)
.addComponent(b[0])
.addComponent(b[1])
    .addGroup(layout.createSequentialGroup())
    .addComponent(b[2])
    .addComponent(b[3]))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(b[4])
.addComponent(b[5])
    .addGroup(layout.createSequentialGroup())
    .addComponent(b[6]))
.addComponent(b[7])
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(b[8])
.addComponent(b[9])
    .addGroup(layout.createSequentialGroup())
    .addComponent(b[10]))
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(b[0])
.addComponent(b[4])
.addComponent(b[8]))

.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(b[1])
.addComponent(b[5])
.addComponent(b[9]))
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(b[2])
.addComponent(b[3])
.addComponent(b[6])
.addComponent(b[10]))

.addComponent(b[7])
);
setTitle("kuvh b");
setSize(1000,1000);
for(int i=0;i<11;i++)
{
add(b[i]);
}
add(jp);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                                  "javax.swing.plaf.metal.MetalLookAndFeel");
                                //  "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                                //UIManager.getCrossPlatformLookAndFeelClassName());
                } catch (Exception ex) {
                    ex.printStackTrace();

           }
                new Abc().setVisible(true);
            }
        });
    }
}

我想用这个做一个4-3-3队形的结构。请帮助我,代码正导致3-3-3的形成。第三行有3个按钮,但我想要4个按钮,请帮忙。

见产出: 1:

  1. http://imgur.com/jxADf2t

我希望我能尽可能找到我的解决方案。

EN

回答 1

Stack Overflow用户

发布于 2015-01-07 13:53:50

我找到了两种方法来做你想做的事情;使按钮对中的方式并不像它可能的那样容易,但是对组件的中心化可能并不像其他对齐一样常见。

您可以在FlowLayout中对组件进行居中;FlowLayout的缺点是,如果用户将窗口缩小到组件不再适合的程度,那么布局就会包装组件。这对于某些事情是非常有用的,但对你的足球运动员却没有用。我已经将我的例子包装在滚动窗格中,这样就不会发生这种情况了。

另一种使组件中心化的方法是使用GroupLayout,但是GroupLayout对您试图实现的总体布局并不好。GroupLayout的目的是要使用的地方,你有整体的行和列,在其中排列的东西,而你的四线足球运动员不是垂直排列,只有水平方向。但是您可以使用GroupLayout的对中特性来进行水平对中,并为每一行创建一个单独的GroupLayout。

我的示例使用FlowLayout作为第一行,使用GroupLayout作为第二行,只是为了说明如何完成它。我没有解决当窗口足够大时,球员之间出现的差距的问题。特别是对于例子,我不使用将方法调用的样式加到其他方法调用和构造函数上;我认为这种样式的深嵌套括号和非直截了当的逻辑使得更难以弄清楚(或跟踪)正在发生的事情。

您也可以使用GridBagLayout来对事物进行中心化,但如果有其他需要的话,我根本不使用它。

我希望这能回答你的问题。

代码语言:javascript
复制
package grouplayout;

import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main2 extends JFrame
{
    public static void main(String ... arguments)
    {
        Main2 main2 = new Main2();
        main2.createUI();
        main2.setVisible(true);
    }

    public void createUI()
    {
        JPanel wingPanel = new JPanel();
        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setHgap(35);
        wingPanel.setLayout(flowLayout);

        JButton btnone = new JButton("Lwing");
        JButton btntwo = new JButton("center");
        JButton btnthr = new JButton("Rwing");
        wingPanel.add(btnone);
        wingPanel.add(btntwo);
        wingPanel.add(btnthr);

        // -------------------------------------------

        JButton mid1 = new JButton("mid1");
        JButton mid2 = new JButton("mid2");
        JButton mid3 = new JButton("mid3");
        JButton mid4 = new JButton("mid4");
        JPanel midfieldPanel = new JPanel();
        GroupLayout groupLayout = new GroupLayout(midfieldPanel);

        GroupLayout.SequentialGroup horizontalGroup = groupLayout.createSequentialGroup();
        groupLayout.setHorizontalGroup(horizontalGroup);
        horizontalGroup.addComponent(mid1);
        horizontalGroup.addComponent(mid2);
        horizontalGroup.addComponent(mid3);
        horizontalGroup.addComponent(mid4);

        GroupLayout.SequentialGroup verticalGroup = groupLayout.createSequentialGroup();
        groupLayout.setVerticalGroup(verticalGroup);

        GroupLayout.ParallelGroup midButtonGroup = groupLayout.createParallelGroup(GroupLayout.Alignment.CENTER);
        midButtonGroup.addComponent(mid1);
        midButtonGroup.addComponent(mid2);
        midButtonGroup.addComponent(mid3);
        midButtonGroup.addComponent(mid4);

        verticalGroup.addGroup(midButtonGroup);

        JPanel teamPanel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(teamPanel, BoxLayout.PAGE_AXIS);
        teamPanel.setLayout(boxLayout);

        teamPanel.add(wingPanel);
        teamPanel.add(midfieldPanel);

        JScrollPane scrollPane = new JScrollPane(teamPanel);
        getContentPane().add(scrollPane);

        pack();

    }
}

编辑:按照要求,下面只对GroupLayout做同样的事情。

两个组之间没有交互,因为GroupLayout在列中对齐东西,而您的玩家不在列中。

是的,我认为这很困难--据我所知,GroupLayout实际上是为了供GUI构建工具使用,而不是用于手工构建GUI。我个人有一个或两个支持类,它允许使用稍微简单一些的逻辑构建GroupLayout UI。但无论如何,我认为你需要理解积木:

GroupLayout允许并要求将每个组件分别放置在水平和垂直行/列位置;这很有用,因为有那么多UI需要混合组件的行和列以及可变的额外组件。

维数X中的序贯组在维数X中顺序排列;维数X中的并行群也按顺序排列,但垂直于维数X。

布局维护组件的首选大小;行宽和列高设置为组件的最大首选大小。

整个GroupLayout对象有一个垂直分组和一个水平分组;在这个分组中,创建顺序组和并行组来创建所需的总体布局。

我知道我读过的教程中的例子--不要创建单独的变量来保存内部顺序组和并行组,而更喜欢使用new X().addComponent().addGroup()等表单。但我认为这会使理解代码的实际操作变得更困难,而不是更容易;嵌套括号成为它们自己的维护问题。所以我认为这是一个更好的方法来做一些事情,特别是那些刚刚开始这个布局的人。

代码语言:javascript
复制
package grouplayout;

import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main3 extends JFrame
{
    public static void main(String ... arguments)
    {
        Main3 main2 = new Main3();
        main2.createUI();
        main2.setVisible(true);
    }

    public void createUI()
    {
        JButton btnone = new JButton("Lwing");
        JButton btntwo = new JButton("center");
        JButton btnthr = new JButton("Rwing");

        JPanel wingPanel = new JPanel();
        GroupLayout wingGroupLayout = new GroupLayout(wingPanel);

        GroupLayout.SequentialGroup wingHorizontalGroup = wingGroupLayout.createSequentialGroup();
        wingGroupLayout.setHorizontalGroup(wingHorizontalGroup);
        wingHorizontalGroup.addComponent(btnone);
        wingHorizontalGroup.addComponent(btntwo);
        wingHorizontalGroup.addComponent(btnthr);

        GroupLayout.SequentialGroup wingVerticalGroup = wingGroupLayout.createSequentialGroup();
        wingGroupLayout.setVerticalGroup(wingVerticalGroup);

        GroupLayout.ParallelGroup wingButtonGroup = wingGroupLayout.createParallelGroup();
        wingButtonGroup.addComponent(btnone);
        wingButtonGroup.addComponent(btntwo);
        wingButtonGroup.addComponent(btnthr);

        wingVerticalGroup.addGroup(wingButtonGroup);

        // -------------------------------------------

        JButton mid1 = new JButton("mid1");
        JButton mid2 = new JButton("mid2");
        JButton mid3 = new JButton("mid3");
        JButton mid4 = new JButton("mid4");
        JPanel midfieldPanel = new JPanel();
        GroupLayout groupLayout = new GroupLayout(midfieldPanel);

        GroupLayout.SequentialGroup horizontalGroup = groupLayout.createSequentialGroup();
        groupLayout.setHorizontalGroup(horizontalGroup);
        horizontalGroup.addComponent(mid1);
        horizontalGroup.addComponent(mid2);
        horizontalGroup.addComponent(mid3);
        horizontalGroup.addComponent(mid4);

        GroupLayout.SequentialGroup verticalGroup = groupLayout.createSequentialGroup();
        groupLayout.setVerticalGroup(verticalGroup);

        GroupLayout.ParallelGroup midButtonGroup = groupLayout.createParallelGroup(GroupLayout.Alignment.CENTER);
        midButtonGroup.addComponent(mid1);
        midButtonGroup.addComponent(mid2);
        midButtonGroup.addComponent(mid3);
        midButtonGroup.addComponent(mid4);

        verticalGroup.addGroup(midButtonGroup);

        JPanel teamPanel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(teamPanel, BoxLayout.PAGE_AXIS);
        teamPanel.setLayout(boxLayout);

        teamPanel.add(wingPanel);
        teamPanel.add(midfieldPanel);

        JScrollPane scrollPane = new JScrollPane(teamPanel);
        getContentPane().add(scrollPane);

        pack();

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27801110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档