gpt4 book ai didi

java - GridBagLayout 从屏幕中间启动 Jbuttons

转载 作者:行者123 更新时间:2023-12-02 12:10:56 24 4
gpt4 key购买 nike

我在 GridBagLayout 中有 14 个按钮。问题是按钮从屏幕中间垂直开始,而不是从开始屏幕开始。另外我想要的是按钮大小相同,并在按钮之间添加一些空间。

Sreenshot:

这是我的代码:

    private final int MAX_TABLES = 14;
JButton [] buttonsTables = new JButton[MAX_TABLES];

frame = new JFrame("FreshPos baza podataka");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

//Main panel
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
frame.getContentPane().add(panel);

//West panel;
JPanel panelWest = new JPanel(new GridBagLayout());
panel.add(panelWest, BorderLayout.WEST);

GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.CENTER;

for (int i = 0; i < MAX_TABLES; i++) {
buttonsTables[i] = new JButton(tables[i]);
buttonsTables[i].setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonsTables[i].getMinimumSize().height));
panelWest.add(buttonsTables[i], c);
c.gridy++;
panelWest.add(Box.createVerticalStrut(10));

}

最佳答案

如果您希望按钮大小相同,请考虑使用 GridLayout,它带有一个在组件之间添加空间的构造函数:

new GridLayout(0, 1, 0, 5); // variable number of rows, 1 column, 5 pixels between

如果您绝对必须使用 GridBagLayout,请将约束的填充属性设置为 GridBagConstraints.HORIZONTALGridBagConstraints.BOTH,并将 anchor 属性设置为 GridBagConstraints。西

有关创建一堆 JButton 的示例,将它们放在屏幕左侧的网格中:

![enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class ButtonsOnSide extends JPanel {
private static final String[] BTN_TEXTS = {
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
"Sunday", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October","November", "December" };
private static final int PREF_W = 1000;
private static final int PREF_H = 800;
private static final int GAP = 4;

public ButtonsOnSide() {
JPanel btnPanel = new JPanel(new GridLayout(0, 1, 0, GAP));
for (String btnText : BTN_TEXTS) {
JButton btn = new JButton(btnText);
btn.addActionListener(e -> System.out.println(e.getActionCommand()));
btnPanel.add(btn);
}
// wrapper panel to help center the button panel vertically
JPanel wrapperPanel = new JPanel(new GridBagLayout());
wrapperPanel.add(btnPanel);

setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout());
add(wrapperPanel, BorderLayout.LINE_START); // add to the left side
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

private static void createAndShowGui() {
JFrame frame = new JFrame("ButtonsOnSide");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ButtonsOnSide());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

关于java - GridBagLayout 从屏幕中间启动 Jbuttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46553775/

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