微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用BoxLayout将3个按钮居中而不切断它们

如何解决使用BoxLayout将3个按钮居中而不切断它们

我遇到了另外两个问题。但让我一一解释。

首先,我尝试使用Box.createVerticalglue()和Box.createRigidArea()将3个Button的BoxLayout居中放置在jframe的中间,并且与每个按钮的距离相同:

    panel.add(Box.createVerticalglue());
    panel.add(Box.createRigidArea(new Dimension(0,100)));
    panel.add(DrawSysButton);
    panel.add(Box.createRigidArea(new Dimension(0,195)));
    panel.add(PickBanButton);
    panel.add(Box.createRigidArea(new Dimension(0,195)));
    panel.add(ExitButton);
    panel.add(Box.createRigidArea(new Dimension(0,100)));
    panel.add(Box.createVerticalglue());

将其放入代码后,按钮如下所示:

enter image description here

按钮在右侧被切断,按钮1和2之间的距离似乎与按钮2和关闭间的距离不同。

当我移除glue和RidigAreas时,按钮就位于框架的顶部中心,下面有许多空白空间:

enter image description here

我尝试了一些方法解决此问题,但没有任何帮助。也许你可以帮我。我将在此处写下您自己运行此问题所需的全部代码。我试图尽可能地创建它。

预先感谢。

import javax.swing.*;
import java.awt.*;

public class main extends JFrame {


    //this method loads the first screen that the user sees
    public void frameLoader() {
        setTitle("3 Centered Buttons");
        setResizable(false);
        setLocationRelativeto(null);
        setDefaultCloSEOperation(WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));

        //Setting up all buttons and panels we need later on
        JPanel panel = new JPanel();

        //The Draw-System one
        JButton DrawSysButton = new JButton("Button 1");
        DrawSysButton.setFocusPainted(false);
        DrawSysButton.setPreferredSize(new Dimension(120,30));

        //The Pick&Ban one
        JButton PickBanButton = new JButton("Button 2");
        PickBanButton.setFocusPainted(false);
        PickBanButton.setPreferredSize(new Dimension(120,30));

        //The Exit one
        JButton ExitButton = new JButton("Close");
        ExitButton.setPreferredSize(new Dimension(120,30));

        //Adding all buttons to the panel
        panel.add(Box.createVerticalglue());
        panel.add(Box.createRigidArea(new Dimension(0,100)));
        panel.add(DrawSysButton);
        panel.add(Box.createRigidArea(new Dimension(0,195)));
        panel.add(PickBanButton);
        panel.add(Box.createRigidArea(new Dimension(0,195)));
        panel.add(ExitButton);
        panel.add(Box.createRigidArea(new Dimension(0,100)));
        panel.add(Box.createVerticalglue());

        //set the Frame visible & add the panel to the frame
        panel.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.setPreferredSize(new Dimension(120,500));
        panel.setMaximumSize(new Dimension(120,500));
        getContentPane().add(panel);
        setSize(400,500);
        setVisible(true);

    }

    public static void main(String[] args) {

        //building the Start-Screen
        main StartScreen = new main();
        StartScreen.frameLoader();
    }
}

解决方法

您的代码有两个导致此错误的问题:

  1. 您将BoxLayout添加到了contentPane,但是将元素添加到了panel。 将getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));替换为panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));,以便将按钮添加到布局中。

  2. 您使用setSize()setPreferredSize()而不是让布局管理应按here所述避免的尺寸。将setSize(400,500);替换为pack();,然后忽略对setPreferredSize()的所有呼叫。您还应该考虑为您的刚性区域找到不同的解决方案,例如对于我来说,由于空间较大,框架超出了屏幕尺寸。

通过这些更改,按钮将为我正确显示。然而,它们失去了一些特性。要使它们回到相同的大小,请查看Making all button size same,使它们居中,请查看tutorial

还请注意,如讨论的here一样,不建议扩展JFrame。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。