gpt4 book ai didi

java - 更改卡片布局中面板的背景和大小

转载 作者:行者123 更新时间:2023-12-01 17:23:33 24 4
gpt4 key购买 nike

我在卡片布局容器中有一些面板(不知道这是否是正确的术语)。我找不到设置容器内这些面板的位置或大小的方法。我尝试了 setBounds 和 setLayout(null) 但仍然无法进行任何更改。这些是我的字段和构造函数。我的框架已经正常工作,我可以看到并使用按钮来更改卡片,但我真的无法更改卡片的其他内容。我将两个卡片面板设置为具有不同的背景,但它们仅在按钮周围制作一个小边框颜色并将其保留在屏幕中央。

我也不明白为什么这不能正确粘贴我的代码...很抱歉!

public class TestPanel extends JPanel implements ActionListener {

CardLayout cl = new CardLayout();

private JPanel panelCont = new JPanel();

private JPanel panel1 = new JPanel();

private JPanel panel2 = new JPanel();

private static JButton but1 = new JButton("Change panels");

private static JButton but2 = new JButton("Change back");

public TestPanel() {

panelCont.setLayout(cl);
panel1.add(but1);
panel2.add(but2);
panel1.setBackground(Color.black);
panel2.setBackground(Color.blue);
panelCont.add(panel1, "1");
panelCont.add(panel2, "2");
cl.show(panelCont, "1");
but1.addActionListener(this);
but2.addActionListener(this);
add(panelCont);
}
}

谢谢。我提前道歉。我发现很难理解卡片布局。

最佳答案

CardLayout 遵循添加到布局中的面板的首选大小。也就是说,该尺寸将是添加到布局中的最大面板的尺寸。

I set the two card panels two have different backgrounds, but they only make a small boarder of color around the button and leave it in the centre of the screen.

面板的默认布局是 FlowLayout。默认情况下,FlowLayout 每个组件周围有 5 像素的水平/垂直间隙。因此,面板的首选尺寸是按钮的尺寸加上 5 像素间隙。

面板显示正确。当您向面板添加其他组件时,尺寸将根据需要更改。

关于java - 更改卡片布局中面板的背景和大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16774600/

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