使用 GroupLayout 创建自定义 GUI

create customized GUI using GroupLayout

我正在尝试创建像 daches 显示的那样的自定义界面:

-----------------------------------------------------------------
-  -----------------------------------------------------------  -
-  -                                                         -  -
-  -----------------------------------------------------------  -
-                                                               -
-                           ----------                          -
-                           -        -                          -
-                           ----------                          -
-                                                               -
-  ----------  -----------------------------------  ----------  -
-  -        -  -                                 -  -        -  -
-  ----------  -----------------------------------  ----------  -
-                                       ----------  ----------  -
-                                       -        -  -        -  -
-                                       ----------  ----------  -
-----------------------------------------------------------------

我按照这里的例子 GroupLayout Example

这是我使用的代码:

GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    layout.setHorizontalGroup(layout.createParallelGroup(LEADING)
            .addComponent(msgLbl)
            .addGroup(layout.createSequentialGroup()

                    .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(empty)
                        .addComponent(fldrLbl3)
                        .addComponent(empty))   
                    .addGroup(layout.createParallelGroup(LEADING)   
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(empty)
                            .addComponent(timerLabel)
                            .addComponent(empty))
                        .addComponent(fldr)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(empty)
                            .addComponent(empty)
                            .addComponent(strtButton)))

                    .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(empty)
                        .addComponent(chFldrButton)
                        .addComponent(PstPndButton))            
        ));

    layout.linkSize(SwingConstants.VERTICAL, empty, empty, empty, strtButton, PstPndButton);


    layout.setVerticalGroup(layout.createSequentialGroup()
            .addComponent(msgLbl)
            .addGroup(layout.createParallelGroup(LEADING)
                .addComponent(empty)
                .addComponent(empty)
                .addComponent(timerLabel)
                .addComponent(empty)
                .addComponent(empty))
            .addGroup(layout.createParallelGroup(LEADING)
                .addComponent(fldrLbl3)
                .addComponent(fldr)
                .addComponent(chFldrButton))
            .addGroup(layout.createParallelGroup(LEADING)
                    .addComponent(empty)
                    .addComponent(empty)
                    .addComponent(empty)
                    .addComponent(strtButton)
                    .addComponent(PstPndButton))

        );

但由于某些原因显示不正确。我想我遗漏了什么,你能帮帮我吗??

//********************************************* ******************************// 我缺少的是 Alignment 枚举的使用:LEADING、TRAILING、CENTER 和 BASELINE。

跟着我就更清楚了:如何使用GroupLayout

对于未来的用户,正确的方法是:

layout.setHorizontalGroup(layout.createSequentialGroup()
        .addComponent(fldrLbl3)
        .addGroup(layout.createParallelGroup()
            .addGroup(layout.createParallelGroup(CENTER)
                .addComponent(msgLbl)
                .addComponent(timerLabel))
            .addGroup(layout.createParallelGroup(TRAILING )
                .addComponent(fldr)
                .addComponent(strtButton)) )
        .addGroup(layout.createParallelGroup(LEADING)
            .addComponent(chFldrButton)
            .addComponent(PstPndButton))
            );


    layout.setVerticalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(CENTER)
                .addComponent(msgLbl))
        .addGroup(layout.createParallelGroup(CENTER)
                .addComponent(timerLabel))
        .addGroup(layout.createParallelGroup(BASELINE)
                .addComponent(fldrLbl3)
                .addComponent(fldr)
                .addComponent(chFldrButton))
        .addGroup(layout.createParallelGroup(BASELINE)
                .addComponent(strtButton)
                .addComponent(PstPndButton)
                )

        );

这是输出,使用 Nested Layout

import java.awt.*;
import java.util.Random;
import javax.swing.*;

public class NestedLayout {

    private static final int GAP = 5;
    private Random random;

    public NestedLayout () {
        random = new Random ();
    }

    private void displayGUI () {
        JFrame frame = new JFrame ( "Nested Layout Example" );
        frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );

        JPanel contentPane = new JPanel ();
        contentPane.setBorder ( BorderFactory.createEmptyBorder (
                                GAP, GAP, GAP, GAP) );
        contentPane.setLayout ( new GridLayout ( 4, 1, GAP, GAP ) );

        contentPane.add ( getHeaderPanel () );
        contentPane.add ( getMiddlePanel () );
        contentPane.add ( getFooterPanel () );
        contentPane.add ( getPSPanel () );

        frame.setContentPane ( contentPane );
        frame.pack ();
        frame.setLocationByPlatform ( true );
        frame.setVisible ( true );
    }

    private JPanel getHeaderPanel () {
        JPanel panel = new JPanel ();
        panel.setLayout ( new BorderLayout ( GAP, GAP) );
        panel.setBorder ( BorderFactory.createEmptyBorder (
                                GAP, GAP, GAP, GAP) );
        panel.add ( getLabel ( "Header JLabel" ), BorderLayout.CENTER );

        return panel;
    }

    private JPanel getMiddlePanel () {
        JPanel panel = new JPanel ();
        panel.setLayout ( new GridBagLayout () );
        panel.setBorder ( BorderFactory.createEmptyBorder (
                                GAP, GAP, GAP, GAP) );
        panel.add ( getLabel ( "Middle JLabel" ) );

        return panel;
    }

    private JPanel getFooterPanel () {
        JPanel panel = new JPanel ();
        panel.setLayout ( new BorderLayout ( GAP, GAP) );
        panel.setBorder ( BorderFactory.createEmptyBorder (
                                GAP, GAP, GAP, GAP) );

        panel.add ( getLabel ( "Left JLabel" ), BorderLayout.LINE_START );
        panel.add ( getLabel ( "Center JLabel" ), BorderLayout.CENTER );
        panel.add ( getLabel ( "Right JLabel" ), BorderLayout.LINE_END );

        return panel;
    }

    private JPanel getPSPanel () {
        JPanel panel = new JPanel ();
        panel.setLayout ( new FlowLayout ( FlowLayout.RIGHT, GAP, GAP) );
        panel.setBorder ( BorderFactory.createEmptyBorder (
                                GAP, GAP, GAP, GAP) );

        panel.add ( getLabel ( "First JLabel" ) );
        panel.add ( getLabel ( "Second JLabel" ) );

        return panel;
    }

    private JLabel getLabel ( String text ) {
        JLabel label = new JLabel ( text, JLabel.CENTER );
        label.setOpaque ( true );
        label.setBackground ( getRandomColour () );
        return label;
    }

    private Color getRandomColour () {
        return new Color ( random.nextFloat (), random.nextFloat (),
                            random.nextFloat (), random.nextFloat () );
    }

    public static void main ( String[] args ) {
        Runnable runnable = new Runnable () {
            @Override
            public void run () {
                new NestedLayout ().displayGUI ();
            }
        };
        EventQueue.invokeLater ( runnable );
    }
}

输出: