java - 如何使用 CardLayout 调整 JPanel 的大小

标签 java css swing layout-manager cardlayout

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

enter image description here

这是我的代码片段:

    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 的要点是面板都占用相同的空间,一次只能显示一个面板。

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

阅读 Using Layout Managers 上的 Swing 教程部分有关布局管理器的更多信息。然后您可以使用嵌套面板来实现您想要的布局。

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

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

关于java - 如何使用 CardLayout 调整 JPanel 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24413279/

相关文章:

java - 当原始请求用 HttpServletRequestWrapper 包装时,jsp:param 不再设置参数

java - java中循环连续播放音乐

java - 相互左递归 ANTLR 4

javascript - Angular 2 如何将单选按钮的检查属性绑定(bind)到组件的 bool 属性?

java - 当 JSpinner 获得焦点时如何选择 JSpinner 日期字段

Java Swing - 如何使关闭对话框不会关闭整个程序?

java - AWS Lambda用于将excel转储到数据库中

html - 在 div 中垂直对齐 span

html - div 中的背景图片不起作用(包括视频)

java - 未修饰的 JFrame 阴影