gpt4 book ai didi

Java GridBagLayout 对齐按钮

转载 作者:行者123 更新时间:2023-12-01 18:15:50 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com