我目前正在使用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();
}
}
答案 0 :(得分:0)
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);
}
}
}
您需要记住,布局管理器正在使用组件的首选/最小/最大大小来确定单元格的宽度和高度(以及其他约束),这将影响某些行/列大小