java - 使用 GroupLayout 创建自定义 GUI

标签 java swing layout-manager 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。

跟着我就更清楚了:How to Use GroupLayout

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

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

        );

最佳答案

这是输出,使用嵌套布局

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

输出:

NESTED_LAYOUT

关于java - 使用 GroupLayout 创建自定义 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29014615/

相关文章:

Java Swing - TableLayout - 未从布局中删除的组件

java.lang.IllegalArgumentException : plexus.container.default:无效的模块名称: 'default'不是Java标识符

java - Android:反转 RecyclerView 位置

java - 有关 java swing/GridBagLaytout 的帮助

java - Eclipse View 中的 GridBagLayout

java - GridBagLayout - 一行的高度导致下一行的宽度发生变化

resize - 网格调整大小

java - 在两个单独的 .java 文件之间传递数据

java - 如何在 IntelliJ 代码中中断异常

java - 如何防止 JSlider 全范围移动?