gpt4 book ai didi

java - 布局组件卡片布局

转载 作者:行者123 更新时间:2023-12-02 06:38:48 24 4
gpt4 key购买 nike

所以我只是创建一个简单的游戏屏幕,我想像这样布置两个按钮:

hello

有人建议我使用 CardLayout(我以前没有使用过布局),所以我阅读了 java 文档并创建了这个:

canvas.setLayout(new CardLayout());
JPanel card1 = new JPanel();
JPanel card2 = new JPanel();

//buttons
final JButton btn1 = new JButton(play);
btn1.setBorder(BorderFactory.createEmptyBorder());
btn1.setContentAreaFilled(false);
final JButton btn2 = new JButton(instructions);
btn2.setBorder(BorderFactory.createEmptyBorder());
btn2.setContentAreaFilled(false);
card1.add(btn1);
card2.add(btn2);

canvas.add(card1);
canvas.add(card2);

但是,它会产生以下结果:

enter image description here

我似乎找不到任何有关卡片布局定位的信息。我的猜测是我根本不应该使用这个布局,那么我应该使用什么布局?

最佳答案

卡片布局用于仅显示布局中多个组件之一。您可以通过编程方式选择要显示的卡,其余卡将隐藏在 View 中。

如果您在卡片布局中添加两个按钮,则只能看到一个。您不应该将它用于您的特定情况。您可以使用带 X 轴的 Box 布局

The BoxLayout manager is constructed with an axis parameter thatspecifies the type of layout that will be done. There are fourchoices:

X_AXIS - Components are laid out horizontally from left to right.

Y_AXIS - Components are laid out vertically from top to bottom.

LINE_AXIS - Components are laid out the way words are laid out in aline, based on the container's ComponentOrientation property. If thecontainer's ComponentOrientation is horizontal then components arelaid out horizontally, otherwise they are laid out vertically. Forhorizontal orientations, if the container's ComponentOrientation isleft to right then components are laid out left to right, otherwisethey are laid out right to left. For vertical orientations componentsare always laid out from top to bottom.

PAGE_AXIS - Components arelaid out the way text lines are laid out on a page, based on thecontainer's ComponentOrientation property. If the container'sComponentOrientation is horizontal then components are laid outvertically, otherwise they are laid out horizontally. For horizontalorientations, if the container's ComponentOrientation is left to rightthen components are laid out left to right, otherwise they are laidout right to left. For vertical orientations components are alwayslaid out from top to bottom.

关于java - 布局组件卡片布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19358632/

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