简体   繁体   English

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

[英]Change Background and size of panels in card layout

I have some panels in a card layout container (no idea if that is correct terminology). 我在卡布局容器中有一些面板(不知道这是否是正确的术语)。 I can't find a way to set the location, or size of these panels inside the container. 我找不到设置容器内这些面板的位置或大小的方法。 I tried setBounds and setLayout(null) and I still can't get anything to change. 我尝试了setBounds和setLayout(null),但仍然无法更改任何内容。 These are my fields and the constructor. 这些是我的字段和构造函数。 I've gotten my frame working and I can see and use the buttons to change cards, but I really can't change much else about the cards. 我的框架已经可以使用了,我可以看到并使用按钮来更换卡,但是我真的不能对卡进行太多更改。 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. 我将两个卡面板设置为两个具有不同的背景,但是它们仅使按钮周围带有一小块颜色,并将其放置在屏幕中央。

I also don't understand why this isn't pasting my code properly... So sorry! 我也不明白为什么这不能正确粘贴我的代码。

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

Thanks. 谢谢。 I apologise in advance. 我事先表示歉意。 I'm finding it hard to understand card layout. 我发现很难理解卡的布局。

A CardLayout respects the preferred size of the panels added to the layout. CardLayout遵循添加到布局中的面板的首选大小。 That is the size will be the size of the largest panel added to the layout. 也就是说,大小将是添加到布局中的最大面板的大小。

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. 我将两个卡面板设置为两个具有不同的背景,但是它们仅使按钮周围带有一小块颜色,并将其放置在屏幕中央。

The default layout for a panel is the FlowLayout. 面板的默认布局是FlowLayout。 A FlowLayout by default has a 5 pixel horizontal/vertical gap around each component. 默认情况下,FlowLayout在每个组件周围有5像素的水平/垂直间隙。 So the preferred size of your panel is the size of the button plus the 5 pixel gap. 因此,面板的首选大小是按钮的大小加上5个像素的间距。

The panel is displaying correctly. 面板显示正确。 When you add other components to the panel the size will change as required. 当您将其他组件添加到面板时,大小将根据需要更改。

It's not clear where you pack() the enclosing Window . 目前尚不清楚您在哪里pack()封闭的Window By default, pack() causes a panel having CardLayout to adopt the the size of the largest panel's preferred size, which is determined by the size of its contents. 默认情况下, pack()会使具有CardLayout的面板采用最大面板的首选大小,该大小由其内容的大小决定。 This example uses setPreferredSize() to specify an arbitrary size, but you can override getPreferredSize() as shown here . 这个例子使用setPreferredSize()指定任意大小,但是你可以重写getPreferredSize()如图所示这里

图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM