繁体   English   中英

如何使用CardLayout调整JPanel的大小

[英]How to resize the JPanel with CardLayout

我只是想知道是否有一种方法可以调整以CardLayout作为其布局的jpanel的大小。 我这里有一张图片,您可以看到我有5个面板。

在此处输入图片说明

这是我的代码段:

    JPanel container = new JPanel();
JPanel panel_1 = new JPanel(); // red
JPanel panel_2 = new JPanel(); // violet
JPanel panel_3 = new JPanel(); // violet
JPanel panel_4 = new JPanel(); // blue
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT,1,1);
CardLayout cl = new CardLayout();

这是给我的第一个拳头面板用的。 图片中的3。

  panel_1 = new JPanel();
    panel_1.setBackground(Color.WHITE);
    panel_1.setBorder(new EmptyBorder(0, 0, 0, 0));
    panel_1.setLayout(flow);

    JButton btnGroup1 = new JButton("<html><p align=center>Group<br>01</p></html>");
    btnGroup1.setIcon(new ImageIcon("more_buttons\\green.png"));
    btnGroup1.setBorderPainted(false);  
        btnGroup1.setFocusPainted(false);
        btnGroup1.setContentAreaFilled(false); 
        btnGroup1.setHorizontalTextPosition(JButton.CENTER);
    btnGroup1.setFont(new Font("Calibri", Font.BOLD, 18));
    btnGroup1.setPreferredSize(new Dimension(80, 80));
    panel_1.add(btnGroup1);

这是我小组的其余部分。

 panel_2 = new JPanel();
    panel_2.setBackground(Color.BLUE);
    panel_2.setBorder(new EmptyBorder(0, 0, 0, 0));
    panel_2.setLayout(flow);

    JButton button_2 = new JButton("<html><p align=center> </p></html>");
    button_2.setIcon(new ImageIcon("more_buttons\\lightblue.png"));
    button_2.setBorderPainted(false);  
        button_2.setFocusPainted(false);
        button_2.setContentAreaFilled(false); 
        button_2.setHorizontalTextPosition(JButton.CENTER);
    button_2.setFont(new Font("Calibri", Font.BOLD, 12));
    button_2.setPreferredSize(new Dimension(80, 80));
    button_2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            cl.show(container, "4");
        }
    });

    panel_2.add(button_2);

    panel_3 = new JPanel();
    panel_3.setBackground(Color.WHITE);
    panel_3.setBorder(new EmptyBorder(0, 0, 0, 0));
    panel_3.setLayout(flow);        

    JButton button_no_7 = new JButton("7");
    button_no_7.setIcon(new ImageIcon("more_buttons\\gray.png"));
    button_no_7.setBorderPainted(false);  
        button_no_7.setFocusPainted(false);
        button_no_7.setContentAreaFilled(false); 
        button_no_7.setHorizontalTextPosition(JButton.CENTER);
    button_no_7.setFont(new Font("Calibri", Font.BOLD, 30));
    button_no_7.setPreferredSize(new Dimension(80, 80));
    panel_3.add(button_no_7);


    add(container);
    container.add(panel_1, "1");
    container.add(panel_2, "2");
    container.add(panel_3, "3");
    container.add(panel_4, "4");

我想要做的是我必须用上一个面板替换上图中的第4个面板,这就是为什么我使用CardLayout的原因,但是我得到的只是显示1、2和3。 它占用了4和5面板的空间。 这就是为什么我想问一下是否可以通过任何方式将面板的大小调整为3,以便可以看到面板4和5。 我知道.setPreferredSize(new Dimension(355,10)); 不起作用,因为它被布局管理器覆盖。 那么有没有人可以帮助我呢?

使用CardLayout的要点是,面板全部占据相同的空间,一次只能显示一个面板。

如果要显示多个面板,则需要使用不同的布局管理器,以在父面板中显示多个子面板。

阅读Swing教程中有关使用布局管理器的部分,以获取有关布局管理器的更多信息。 然后,您可以使用嵌套面板来实现所需的布局。

另外,您的代码不应使用setPreferredSize()。 布局管理器的工作是设置组件的大小/位置。

因此,也许您从具有BorderLayout的主面板开始。 然后将panel1添加到NORTH,将pan2l2添加到WEST。 然后创建另一个面板,将其添加到CENTER。 然后,为该面板设置适当的布局,然后将面板3、4添加到面板中。 这是您逻辑上嵌套面板以获取所需布局的方式。

@camickr已经清楚地说了。 我认为您错误地理解了如何使用CardLayout

添加此行:

panel_4.setLayout(new CardLayout());

并将要切换的2个面板添加到panel_4 假设它们是AB

panel_4.add(A, "panelA");
panel_4.add(B, "panelB");

要显示它们,请添加一个按钮进行切换。

CardLayout cc = (CardLayout)(panel_4.getLayout());
cc.show(panel_4, "panelA");

此方法的第一个参数是父容器,第二个是要显示的面板的名称。

在您的代码中,我看到您希望使container显示panel_4 ,但是具有CardLayout的面板将在其所有空间中显示该面板。 它不能在左上角显示一个面板,而其余部分保持完整。 全部,或一无所有。

因此,此行:

container.add(panel_4, "4"); 

将显示在其所有区域panel_4中。

暂无
暂无

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

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