使用BoxLayout居中显示3个按钮,而不裁剪它们。

huangapple go评论68阅读模式
英文:

Centering 3 Buttons with BoxLayout without cutting them off

问题

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

I ran into 2 other problems. But let me explain one by one.

First I tried to center 3 Buttons with the BoxLayout in the middle of a jframe with the same distance to each button by using Box.createVerticalGlue() and Box.createRigidArea():

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

As soon as I put this into the code, the buttons look like this:

使用BoxLayout居中显示3个按钮,而不裁剪它们。

The buttons are cut off on the right side and the distance between Button 1 and 2 seems to be not the same as between Button 2 and Close.

When I remove the Glue and RidigAreas, then the buttons are just in the top center of the frame with many empty space beneath:

使用BoxLayout居中显示3个按钮,而不裁剪它们。

I tried a few things to fix that, but nothing helped. Maybe you can help me. I´ll put down here the whole code you need to run this problem by yourself. I tried to create it as minimal as it´s possible.

Thanks in Advance.

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

得分: 3

你的代码存在两个问题,导致了这个错误:

  1. 你将你的 BoxLayout 添加到了 contentPane,但是将元素添加到了你的 panel。将 getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 替换为 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));,这样你的按钮将被添加到布局中。

  2. 你使用了 setSize()setPreferredSize(),而不是让布局管理大小,这是应该避免的,如这里所述。将 setSize(400, 500); 替换为 pack();,并省略所有对 setPreferredSize() 的调用。你还应该考虑为你的刚性区域找到一个不同的解决方案,因为例如对我来说,由于大的空白区域,窗口超出了屏幕大小。

经过这些更改,按钮在我的环境中得到了正确的显示。但是它们失去了一些属性。要使它们恢复到相同的大小,请查看 https://stackoverflow.com/questions/11536089/making-all-button-size-same;要使它们居中,请参阅教程

此外,请注意不推荐扩展 JFrame,详情讨论请见这里

英文:

Your code has 2 problems leading to this bug:

  1. You add your BoxLayout to you contentPane, but add the elements to your panel.
    Replace getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); with panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); so your buttons get added into the layout.

  2. You use setSize() and setPreferredSize() instead of letting the layout manage the sizes which should be avoided as explained here. Replace setSize(400, 500); with pack(); and omit all your calls to setPreferredSize(). You should also consider to find a different solution for your rigid areas, for me for example the frame exceeds my screen size due to the large spaces.

With these changes the buttons get displayed properly for me. However they lose some properties. To get them back to all having the same size take a look at https://stackoverflow.com/questions/11536089/making-all-button-size-same, to get them centered take a look at the tutorial.

Also note extending JFrame is discouraged as discussed here

huangapple
  • 本文由 发表于 2020年10月27日 19:18:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64553318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定