为什么我的两个JButton设置为1时会占用2个空格(网格宽度?)

问题描述 投票:0回答:2

我目前正在使用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();
    }
}
java swing gridbaglayout
2个回答
0
投票

GridBagLayout非常复杂,有时可以完成您不期望的操作。在这种情况下,GridBagLayout折叠了第一列,因为(从技术上来说)没有任何内容,这使它“看起来”像您的其他按钮也填充在两列中,当它们没有填充时一样。

例如。我在第一列(下一行)中添加了一个空的JLabel,并能够产生以下输出

Expanded columns

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

[您需要记住,布局管理器使用组件的首选/最小/最大大小来确定单元的宽度和高度(以及其他约束),这将影响某些行/列的方式尺寸


-1
投票

您的按钮将展开以填充可用空间,因为您正在使用:

gameLayout.fill = GridBagConstraints.HORIZONTAL;

这指示布局将组件扩展为在可用空间大于组件所请求的大小时填充所有可用的水平空间。

您应该做的是为按钮设置首选大小,而不使用填充约束。

尝试删除gameLayout.fill行并将以下内容添加到guiConfig()方法中:

userName.setPreferredSize(new Dimension(100, 20));
cookieCounter.setPreferredSize(new Dimension(100, 20));
cookieClick.setPreferredSize(new Dimension(100, 20));
shopButton.setPreferredSize(new Dimension(100, 20));

您的代码还建议您不太了解GridBagConstraints背后的想法。该对象可以设置布局中所有组件的行为,因此您只需指定一次填充即可。但是,如果要更改行为,则可以在此时更改约束。每个组件均根据最后一个约束进行布局,因此仅在需要更改行为时才添加不同的约束。

这意味着,如果您希望组件使用水平填充,请使用:

gameLayout.fill = GridBagConstraints.HORIZONTAL;

下一个组件不应填满所有可用的水平空间后,使用:

gameLayout.fill = GridBagConstraints.NONE;
© www.soinside.com 2019 - 2024. All rights reserved.