GroupLayout和组件大小跨度

时间:2014-01-12 14:34:14

标签: java swing layout-manager grouplayout

我正在使用SWING为UI创建一个Java应用程序。 我对LayoutManager的选择转到GroupLayout,但现在我遇到了Component的问题。

enter image description here

基本布局由三列和两行组成(有嵌套行,如下面的代码所示),第二行只包含带有JCheckBoxes列表的JPane。

我的问题是我希望以跨越列的方式插入JPanel,而不影响其他列的大小(即Canvas必须是方形而不是矩形)。

是否可以或我必须更改LayoutManager?

以下是代码:

    //create and set LayoutManager
    GroupLayout gp = new GroupLayout(this.getContentPane());
    gp.setAutoCreateContainerGaps(true);
    gp.setAutoCreateGaps(true);
    this.setLayout(gp);
    //set alignment criteria
    GroupLayout.Alignment hAlign = GroupLayout.Alignment.TRAILING;
    GroupLayout.Alignment vAlign = GroupLayout.Alignment.BASELINE;

    //add component into layout
    //set horizontal group
    gp.setHorizontalGroup(gp.createSequentialGroup()
            .addGroup(gp.createParallelGroup(hAlign)
                    .addComponent(imageCanvas)
                    .addComponent(densitiesPanel))
            .addGroup(gp.createParallelGroup(hAlign)
                    .addComponent(projectPathField)
                    .addComponent(sourceDensityLabel)
                    .addComponent(sourceSizeLabel))
            .addGroup(gp.createParallelGroup(hAlign)
                    .addComponent(projectPathButton)
                    .addComponent(sourceDensityComboBox)
                    .addComponent(sourceSizeTextField))
                    );

    //set vertical group
    gp.setVerticalGroup(gp.createSequentialGroup()
            .addGroup(gp.createParallelGroup(vAlign)
                    .addComponent(imageCanvas)
            .addGroup(gp.createSequentialGroup()
                    .addGroup(gp.createParallelGroup(vAlign)
                            .addComponent(projectPathField)
                            .addComponent(projectPathButton))
                    .addGroup(gp.createParallelGroup(vAlign)
                            .addComponent(sourceDensityLabel)
                            .addComponent(sourceDensityComboBox))
                    .addGroup(gp.createParallelGroup(vAlign)
                            .addComponent(sourceSizeLabel)
                            .addComponent(sourceSizeTextField)))
                    )
                    .addComponent(densitiesPanel)
            );

2 个答案:

答案 0 :(得分:1)

我自己解决了添加第二个JPanel:我称之为 mainPanel 并给它上面代码的GroupLayout;然后使用BoxLayout设置TopContainer并分别添加两个窗格,结果如下:

enter image description here

答案 1 :(得分:1)

我与您分享了GroupLayout的一个例子。 我希望你喜欢它:)。

enter image description here

import static javax.swing.GroupLayout.Alignment.BASELINE;
import static javax.swing.GroupLayout.Alignment.CENTER;
import static javax.swing.GroupLayout.Alignment.LEADING;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

public class MyForm extends JFrame {

    public MyForm() {

        JLabel jlblTitle = new JLabel("Form Builder");

        JLabel jlblUsername = new JLabel("Username:");
        JLabel jlblGroup = new JLabel("Group:");
        JLabel jlblAddress = new JLabel("Address:");

        JComboBox jcmbxUsername = new JComboBox();
        JComboBox jcmbxGroup = new JComboBox();
        JComboBox jcmbxAddress = new JComboBox();

        JButton jbtnUsername = new JButton("New Username");
        JButton jbtnGroup = new JButton("New Group");
        JButton jbtnAddress = new JButton("New Address");

        JButton jbtnCreate = new JButton("Create");

        JPanel jpanUser = new JPanel();

        GroupLayout layout = new GroupLayout(jpanUser);

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(CENTER)
                        .addComponent(jlblTitle)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(LEADING)
                                .addComponent(jlblUsername)
                                .addComponent(jlblGroup)
                                .addComponent(jlblAddress))
                        .addGroup(
                            layout.createParallelGroup(LEADING)
                                .addComponent(jcmbxUsername)
                                .addComponent(jcmbxGroup)
                                .addComponent(jcmbxAddress)
                                .addComponent(jbtnCreate, Alignment.CENTER))
                        .addGroup(
                            layout.createParallelGroup(LEADING)
                                .addComponent(jbtnUsername)
                                .addComponent(jbtnGroup)
                                .addComponent(jbtnAddress)))));

        layout.linkSize(SwingConstants.HORIZONTAL, jbtnUsername, jbtnGroup,
                jbtnAddress);

        layout.setVerticalGroup(layout
                .createSequentialGroup()
                .addGroup(
                        layout.createParallelGroup(BASELINE)
                        .addComponent(jlblTitle))
                .addGroup(
                        layout.createParallelGroup(LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(
                                    layout.createParallelGroup(BASELINE)
                                    .addComponent(jlblUsername)
                                    .addComponent(jcmbxUsername)
                                    .addComponent(jbtnUsername))
                            .addGroup(
                                    layout.createParallelGroup(BASELINE)
                                    .addComponent(jlblGroup)
                                    .addComponent(jcmbxGroup)
                                    .addComponent(jbtnGroup))
                            .addGroup(
                                    layout.createParallelGroup(BASELINE)
                                    .addComponent(jlblAddress)
                                    .addComponent(jcmbxAddress)
                                    .addComponent(jbtnAddress))
                            .addComponent(jbtnCreate))));

        jpanUser.setLayout(layout);

        BoxLayout layoutMain = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
        getContentPane().setLayout(layoutMain);

        add(jpanUser);

        setTitle("My form");
        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 MyForm().setVisible(true);
            }
        });
    }
}