繁体   English   中英

Java GridBagLayout对齐按钮

[英]Java GridBagLayout Aligning buttons

我对GridbagLayout有问题; 我有5个按钮,我想以这种方式使用它们: 目标按钮的位置

我已经尝试了不同的方法,但是没有人以正确的方式工作。

例如:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestGridBagLayout {

    protected void initUI() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel southPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridwidth = 2;
        gbc.gridy = 0;
        JButton enterRoom = new JButton("Enter room");
        JButton exitRoom = new JButton("Exit room");
        JButton login = new JButton("Login");
        JButton logout = new JButton("Logout");
        JButton whoIsIn = new JButton("Who is in");

        gbc.gridx = 1;
        southPanel.add(enterRoom, gbc);

        gbc.gridx = 5;
        southPanel.add(exitRoom, gbc);

        gbc.gridy = 1;

        gbc.gridx = 0;
        southPanel.add(login, gbc);

        gbc.gridx = 3;
        southPanel.add(logout, gbc);

        gbc.gridx = 6;
        southPanel.add(whoIsIn, gbc);

        frame.add(southPanel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestGridBagLayout().initUI();
            }
        });
    }
}

显示: 错误的按钮对齐 我对其他方法(例如GridLayout)不感兴趣,我想知道自己缺少什么。

GridbagLayout似乎需要一行,其中组件占据该行中的所有列。 请参阅: 为什么此GridBagLayout不能按计划显示? 作为该解决方案的基础。

请注意,水平支柱的大小选择为“注销”按钮的大小的一半,以便两个单元格具有“注销”按钮的宽度,以使所需组件居中。

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

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        JButton enterRoom = new JButton("Enter room");
        JButton exitRoom = new JButton("Exit room");
        JButton login = new JButton("Login");
        JButton logout = new JButton("Logout");
        JButton whoIsIn = new JButton("Who is in");

        setLayout( new GridBagLayout() );
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 0,5, 0);

        gbc.gridwidth = 2;

        gbc.gridx = 1;
        gbc.gridy = 0;
        add(enterRoom, gbc);

        gbc.gridx = 5;
        gbc.gridy = 0;
        add(exitRoom, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        add(login, gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        add(logout, gbc);

        gbc.gridx = 6;
        gbc.gridy = 1;
        add(whoIsIn, gbc);

        //  Add dummy components so every cell has a component.

        gbc.insets = new Insets(0, 0, 0, 0);
        gbc.gridwidth = 1;
        gbc.gridy = 2;
        int strutWidth = logout.getPreferredSize().width / 2;

        for (int i = 0; i < 8; i++)
        {
            gbc.gridx = i;
            add(Box.createHorizontalStrut(strutWidth), gbc);
        }
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE(), BorderLayout.NORTH);
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

在某些情况下,GridBagLayout可能是一种奇怪的动物。 但是无论如何,只有在“ spanned”列中有一个实际组件需要一定宽度的情况下,gridwidth才起作用(例如,如果您说gridx = 0且gridwidth = 2,则列0具有一个组件,而“ spanned” “列是第1列)。

在您的情况下,第2列,第4列和第7列没有组件,因此它们的宽度设置为0。此外,第5列的宽度也为0,因为第6列为退出房间按钮提供了足够的技巧,所以最终得到您看到的结果。

现在,不确定要尝试实现的布局类型(我看到了您的屏幕截图,但是当面板折叠/扩展宽度时应该如何表现?)。 因此,请在下面找到一个与您描述的内容更接近的示例(尽管我觉得它不是很好)

例

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestGridBagLayout2 {

    protected void initUI() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel southPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridy = 0;
        JButton enterRoom = new JButton("Enter room");
        JButton exitRoom = new JButton("Exit room");
        JButton login = new JButton("Login");
        JButton logout = new JButton("Logout");
        JButton whoIsIn = new JButton("Who is in");

        gbc.gridx = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        southPanel.add(enterRoom, gbc);

        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridx = 2;
        southPanel.add(exitRoom, gbc);

        gbc.gridy = 1;

        gbc.gridx = 0;
        southPanel.add(login, gbc);

        gbc.weightx = 0;
        gbc.gridx = 1;
        southPanel.add(logout, gbc);

        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.gridx = 2;
        southPanel.add(whoIsIn, gbc);

        frame.add(southPanel);
        frame.pack();
        frame.setSize(frame.getWidth() * 4 / 3, frame.getHeight());
        frame.setMinimumSize(frame.getSize());
        frame.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestGridBagLayout().initUI();
            }
        });
    }
}

暂无
暂无

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

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