在 swing 中居中组件和调整 JFrame 的大小

Centering components and resizing JFrame in swing

我正在制作一个 java GUI,其中有一些垂直框。在那些盒子里面,有一些按钮和标签。我试图将按钮和标签放在中间但不起作用! 我正在使用此代码在中心设置标签。

JLabel update = new JLabel("update");
update.setHorizontalTextPosition(CENTER);

其中 update 是我的垂直框的最后一个组件。

另一个问题是我需要 window 根据我的 GUI 中的变化自动调整大小(因为它是动态的)! 我怎么也能做这个?

I am trying to put the buttons and labels in the center but doesn't work! I am using this code to set the label in the center.

有几种方法可以做到这一点,但对我来说最简单的是使用 GridBagLayout。 如果 boxes/container(希望从 JPanel 或 JComponent 扩展)使用 GridBagLayout,并且您使用 GridBagConstraints 添加组件到容器中:gridX 和 gridY 设置,但 weightX 和 weightY 设置为默认值 0,这些添加的组件将在容器的中心。

我无法显示代码,因为我不知道您当前使用的代码或您的 observed/desired GUI 的图像。如果您需要更多帮助,请编辑您的问题并提供更多相关信息。

The other problem is that I need the window to resize automatically depending on the changes in my GUI (since it is a dynamic one)! How can I make this too?

这将完全取决于您的 GUI 使用的布局管理器,这是我们目前还不知道的。同样,如果您仍然遇到困难,请创建并 post 您的 Minimal, Complete, and Verifiable Example Program.

例如,以下带有居中按钮和 JLabel 文本的可调整大小的 GUI:

由以下代码创建:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class VertBoxes extends JPanel {
    private static final String[] LABEL_TEXTS = { "A", "One", "Two", "Monday",
            "Tuesday", "January", "Fourth of July",
            "Four score and seven years ago" };
    public static final int PREF_W = 260;
    public static final int PREF_H = 80;

    public VertBoxes() {
        setLayout(new GridLayout(0, 1, 5, 5));
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        for (String labelTxt : LABEL_TEXTS) {
            add(new InnerBox(labelTxt));
        }
    }

    private class InnerBox extends JPanel {
        public InnerBox(String labelTxt) {
            setLayout(new GridBagLayout());
            setBorder(BorderFactory.createLineBorder(Color.black, 4));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(new JButton("Button"), gbc);
            gbc.gridy++;
            add(new JLabel(labelTxt), gbc);
        }

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

    private static void createAndShowGui() {
        VertBoxes mainPanel = new VertBoxes();

        JFrame frame = new JFrame("Vertical Boxes");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}