英文:
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:
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:
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
你的代码存在两个问题,导致了这个错误:
-
你将你的
BoxLayout
添加到了contentPane
,但是将元素添加到了你的panel
。将getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
替换为panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
,这样你的按钮将被添加到布局中。 -
你使用了
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:
-
You add your
BoxLayout
to youcontentPane
, but add the elements to yourpanel
.
ReplacegetContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
withpanel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
so your buttons get added into the layout. -
You use
setSize()
andsetPreferredSize()
instead of letting the layout manage the sizes which should be avoided as explained here. ReplacesetSize(400, 500);
withpack();
and omit all your calls tosetPreferredSize()
. 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论