gpt4 book ai didi

java - BoxLayout 拒绝接受 JButton 的首选大小

转载 作者:行者123 更新时间:2023-11-30 09:28:04 25 4
gpt4 key购买 nike

我一直在做一个模拟赌博游戏的小项目。不幸的是,我在使用 BoxLayout 时遇到了一些奇怪的问题。据我所知,LayoutManager 通常遵循任何组件的首选大小。但是,在下面的代码中,BoxLayout 没有。

到目前为止,这是我的代码:

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



public class Main
{
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Suit-Up");
frame.setContentPane(makeGUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,450);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}

public static JPanel makeGUI()
{
JPanel main = new JPanel();
main.setMinimumSize(new Dimension(900,450));
main.setBackground(Color.red);

JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.LINE_AXIS));
infoPanel.setPreferredSize(new Dimension(900,60));
infoPanel.setBackground(Color.green);
main.add(infoPanel);

JPanel infoText = new JPanel();
infoText.setLayout(new BoxLayout(infoText, BoxLayout.PAGE_AXIS));
infoPanel.add(infoText);

JPanel moneyText = new JPanel();
moneyText.setLayout(new BoxLayout(moneyText, BoxLayout.LINE_AXIS));
infoText.add(moneyText);

JPanel lastGameText = new JPanel();
lastGameText.setLayout(new BoxLayout(lastGameText, BoxLayout.LINE_AXIS));
infoText.add(lastGameText);

JButton playAgain = new JButton("Play Again ($20)");
playAgain.setPreferredSize(new Dimension(200,60));
infoPanel.add(playAgain);

JButton finish = new JButton("End Session");
finish.setPreferredSize(new Dimension(200,60));
infoPanel.add(finish);

JPanel cardPanel = new JPanel();
cardPanel.setLayout(new BoxLayout(cardPanel, BoxLayout.LINE_AXIS));
main.add(cardPanel);

return main;
}
}

尽管为两个 JButton 指定了首选大小,但它们并没有改变它们的大小。我也尝试了 setMaximumSize()setMinimumSize(),但都没有任何效果。

我是否忽略了一些明显的东西,或者这是 BoxLayout 的限制?

最佳答案

“据我所知,LayoutManager 通常遵循任何组件的首选尺寸” - 这实际上不是真的。首选/最小/最大大小只是布局管理器可以用来确定如何最好地布局内容的“提示”。如果他们愿意,布局管理器可以简单地忽略它们。

来自 JavaDocs

BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout). For a horizontal layout, if not all the components are the same height, BoxLayout attempts to make all the components as high as the highest component. If that's not possible for a particular component, then BoxLayout aligns that component vertically, according to the component's Y alignment. By default, a component has a Y alignment of 0.5, which means that the vertical center of the component should have the same Y coordinate as the vertical centers of other components with 0.5 Y alignment.

Similarly, for a vertical layout, BoxLayout attempts to make all components in the column as wide as the widest component. If that fails, it aligns them horizontally according to their X alignments. For PAGE_AXIS layout, horizontal alignment is done based on the leading edge of the component. In other words, an X alignment value of 0.0 means the left edge of a component if the container's ComponentOrientation is left to right and it means the right edge of the component otherwise.

关于java - BoxLayout 拒绝接受 JButton 的首选大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14226622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com