JFrame 中心组件

JFrame center components

我想知道如何将我的组件居中,我找到了 gridBagLayout,但我找不到让它比实际更大的方法:我的按钮太小了。

我的部分代码:

private void addMainButtons() {
        JPanel container = new JPanel() {
            private static final long serialVersionUID = -424395301619105440L;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Dimension dim = this.getSize();
                DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
                Date date = new Date();
                g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
                g.setColor(Color.BLACK);
                String s = format.format(date);
                g.drawString(s, (int) ((dim.getWidth() - s.length() - 80) / 2), 20);
            }
        };
        container.setBackground(new Color(109, 69, 60));

        JButton productsButton = new JButton("Produits");
        productsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(frame, pages[1]);
            }
        });
        productsButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        productsButton.setAlignmentX(Component.CENTER_ALIGNMENT);

        // Creating grid
        container.setPreferredSize(new Dimension(400, 600));
        container.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        container.add(productsButton, gbc);

        gbc.gridy = 1;
        gbc.gridwidth = 1;
        container.add(new JButton("Entrée"), gbc);

        gbc.gridx = 1;
        container.add(new JButton("Sortie"), gbc);

        mainPage.setLayout(new BorderLayout());
        mainPage.add(container, BorderLayout.CENTER);
        // End of main page
    }

GridBagLayout 实在是太难用了,请问有什么方法可以让组件居中吗?我可以使用 NestedLayouts,但我不知道如何使用。

尝试使用以下代码增加按钮的大小,同时将每个组件添加到 gridbagconstraints。根据需要更改 weightx/weighty 的值。

gbc.weightx = 1.0;
gbc.weighty = 1.0;

要在组件之间留出更多空间,请在将每个组件添加到 gridbagconstraints 时使用以下代码。

gbc.insets = new Insets(2, 2, 2, 2);

据我所知,您使用了正确的居中组件方式。也可以通过其他布局来完成,但可能有点困难。

您可以使用weightx and/or weighty 来影响space 组件将占用的数量,例如,如果我这样做

GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;

gbc.weightx = 1;
gbc.weighty = 1;

container.add(productsButton, gbc);

gbc.gridy = 1;
gbc.gridwidth = 1;
container.add(new JButton("Entrée"), gbc);

gbc.gridx = 1;
container.add(new JButton("Sortie"), gbc);

我可以生成这个...

您可以使用两个容器,一个装有显示日期的 JLabel 和一个包含按钮的容器,但我怀疑这不是您真正想要的。

您可以使用 ipadxipady 将数量添加到组件的首选大小

GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;

gbc.ipadx = 40;
gbc.ipady = 40;

container.add(productsButton, gbc);

gbc.gridy = 1;
gbc.gridwidth = 1;
container.add(new JButton("Entrée"), gbc);

gbc.gridx = 1;
container.add(new JButton("Sortie"), gbc);

当包含在您现有的约束中时,您可以 "grow" 按钮。