我目前正在使用 Java Swing 和 GridBagLayout 来尝试组织一些按钮。我遇到一个问题,我的“cookieClick”和“shopButton”按钮占用了两个空间(gridx 0 和 1),首先,我将它们的网格 x 设置为 1,并将weightx 设置为 1.0。任何帮助都会很棒,谢谢。
Swing 设置类
package com.jetbrains;
import javax.swing.*;
import java.awt.*;
public class GUI {
final int screenX = 1280, screenY = 720;
JFrame mainGui = new JFrame();
JPanel gamePanel = new JPanel();
JPanel shopPanel = new JPanel();
JButton userName = new JButton("a"); // this is the area for the user's username they input in
JButton cookieCounter = new JButton("b"); // this is the counter for how many cookies the user currently has
JButton cookieClick = new JButton("c"); // this is the label for the cookie we click on
JButton shopButton = new JButton("d"); // this takes us to the shop, probably some if statement and set visible etc.
GridBagConstraints gameLayout = new GridBagConstraints();
public void guiConfig() {
mainGui.setSize(screenX, screenY);
mainGui.setTitle("journey");
mainGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainGui.setResizable(false);
mainGui.setBackground(Color.WHITE);
mainGui.add(gamePanel);
gamePanel.setSize(screenX, screenY);
gamePanel.setLayout(new GridBagLayout());
}
public void labelPositioning() {
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 0;
gameLayout.gridwidth = 2;
gameLayout.gridy = 0;
gameLayout.weightx = 2.0;
gameLayout.weighty = 1.0;
gamePanel.add(cookieCounter, gameLayout);
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 2;
gameLayout.gridwidth = 1;
gameLayout.gridy = 0;
gameLayout.weightx = 1.0;
gameLayout.weighty = 1.0;
gamePanel.add(userName, gameLayout);
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 1;
gameLayout.gridwidth = 1;
gameLayout.gridy = 1;
gameLayout.weightx = 1.0;
gameLayout.weighty = 1.0;
gamePanel.add(cookieClick, gameLayout);
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 1;
gameLayout.gridwidth = 1;
gameLayout.gridy = 2;
gameLayout.weightx = 1.0;
gameLayout.weighty = 1.0;
gamePanel.add(shopButton, gameLayout);
}
public void frameVisibility() {
mainGui.setVisible(true);
}
}
主类
package com.jetbrains;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
GUI run = new GUI();
run.guiConfig();
run.labelPositioning();
run.frameVisibility();
}
}
最佳答案
GridBagLayout
非常复杂,有时会做一些你意想不到的事情。在这种情况下, GridBagLayout 正在折叠第一列,因为(技术上)其中没有任何内容,这使得它“看起来”就像您的其他按钮也填充了两列,但实际上并非如此。
例如。我向第一列(下一行)添加了一个空的 JLabel
并能够生成以下输出
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GUI gui = new GUI();
gui.guiConfig();
gui.labelPositioning();
gui.frameVisibility();
}
});
}
public class GUI {
final int screenX = 1280, screenY = 720;
JFrame mainGui = new JFrame();
JPanel gamePanel = new JPanel();
JPanel shopPanel = new JPanel();
JButton userName = new JButton("userName"); // this is the area for the user's username they input in
JButton cookieCounter = new JButton("cookieCounter"); // this is the counter for how many cookies the user currently has
JButton cookieClick = new JButton("cookieClick"); // this is the label for the cookie we click on
JButton shopButton = new JButton("shopButton"); // this takes us to the shop, probably some if statement and set visible etc.
GridBagConstraints gameLayout = new GridBagConstraints();
public void guiConfig() {
mainGui.setSize(screenX, screenY);
mainGui.setTitle("journey");
mainGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// mainGui.setResizable(false);
mainGui.setBackground(Color.WHITE);
mainGui.add(gamePanel);
// gamePanel.setSize(screenX, screenY);
gamePanel.setLayout(new GridBagLayout());
}
public void labelPositioning() {
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 0;
gameLayout.gridwidth = 2;
gameLayout.gridy = 0;
gameLayout.weightx = 2.0;
gameLayout.weighty = 1.0;
gamePanel.add(cookieCounter, gameLayout);
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 0;
gameLayout.gridwidth = 1;
gameLayout.gridy = 0;
gameLayout.weightx = 1.0;
gameLayout.weighty = 1.0;
gamePanel.add(new JLabel(), gameLayout);
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 2;
gameLayout.gridwidth = 1;
gameLayout.gridy = 0;
gameLayout.weightx = 1.0;
gameLayout.weighty = 1.0;
gamePanel.add(userName, gameLayout);
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 1;
gameLayout.gridwidth = 1;
gameLayout.gridy = 1;
gameLayout.weightx = 1.0;
gameLayout.weighty = 1.0;
gamePanel.add(cookieClick, gameLayout);
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 1;
gameLayout.gridwidth = 1;
gameLayout.gridy = 2;
gameLayout.weightx = 1.0;
gameLayout.weighty = 1.0;
gamePanel.add(shopButton, gameLayout);
}
public void frameVisibility() {
mainGui.setVisible(true);
}
}
}
您需要记住,布局管理器正在使用组件的首选/最小/最大尺寸来确定单元格的宽度和高度(以及其他约束),这将影响某些行/列大小已调整
关于java - 当设置为 1 时,为什么我的 2 个 JButton 占用 2 个空格(网格宽度)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59483102/