简体   繁体   English

使组件居中并调整JFrame的大小

[英]Centering components and resizing JFrame in swing

I am making a java GUI in which I have some vertical boxes. 我正在制作一个Java GUI,其中有一些垂直框。 Inside those boxes, there are some buttons and Labels. 在这些框内,有一些按钮和标签。 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. 我正在使用此代码将标签设置在中间。

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

where update is the last component of my vertical box. 更新是我的垂直框的最后一个组件。

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)! 另一个问题是我需要根据我的GUI中的更改自动调整窗口的大小(因为它是动态的)! How can I make this too? 我也该怎么做?

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. 我正在使用此代码将标签设置在中间。

There are several ways to do this, but the easiest for me is to use a GridBagLayout. 有几种方法可以做到这一点,但对我来说最简单的方法是使用GridBagLayout。 If the boxes/container (which hopefully extend from JPanel or JComponent) uses a GridBagLayout, and you add components into the container with GridBagConstraints: gridX and gridY set, but with weightX and weightY set to default of 0, those added components will center in the container. 如果盒子/容器(希望从JPanel或JComponent扩展)使用GridBagLayout,并且您将具有GridBagConstraints的组件添加到容器中:gridX和gridY设置,但是weightX和weightY设置为默认值0,则这些添加的组件将居中容器。

I can't show code since I have no knowledge of the code you're currently using or the images of your observed/desired GUI's. 我无法显示代码,因为我不了解您当前正在使用的代码或您观察到的/所需的GUI的图像。 If you need more help, please edit your question and provide more pertinent information. 如果您需要更多帮助,请编辑问题并提供更多相关信息。

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)! 另一个问题是我需要根据我的GUI中的更改自动调整窗口的大小(因为它是动态的)! How can I make this too? 我也该怎么做?

This will all depend on the layout managers that your GUI is using, something that we have no knowledge of as yet. 这完全取决于您的GUI使用的布局管理器,而我们尚不了解。 Again, if you're still stuck, please create and post your Minimal, Complete, and Verifiable Example Program . 同样,如果您仍然遇到困难,请创建并发布“ 最小,完整和可验证的示例程序”

For example the following resizable GUI with centered buttons and JLabel texts: 例如,以下具有居中按钮和JLabel文本的可调整大小的GUI:

在此处输入图片说明

Is created by the following code: 由以下代码创建:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM