Java GridBagLayout 对齐按钮

标签 java swing button layout-manager gridbaglayout

我的 GridbagLayout 有问题;我有 5 个按钮,我想以这种方式拥有它们: Target ButtonDisposition

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

例如:

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

出现: wrong buttons aligning 我对其他方法(例如 GridLayout)不感兴趣,我想知道我缺少什么。

最佳答案

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

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

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

Example

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

关于Java GridBagLayout 对齐按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29633865/

相关文章:

Java HashMultiMap 存储问题

java - 使用 ffmpeg 修剪和合并音频和视频文件

Java 线程在关闭后停止

java - 线程在 actionPerformed 方法内部 hibernate

java - 跨应用程序重用 JEditorPane

css - Bootstrap btn-group 未对齐

java - 卸载旧 Java 版本后 Eclipse 崩溃

javax.swing.text.html.HTMLDocument : how do I find a html image map in a document?

javascript - js 按类在按钮上堆叠

ios - 无法更改 UITableViewCell 中按钮的选定状态图像