在 FlowLayout 中并排嵌套多个 CardLayout

Nesting multiple CardLayouts side by side in a FlowLayout

我正在尝试并排显示多个 CardLayout,所有这些都在一个 FlowLayout 中。一切运行良好,但 window 中没有显示任何内容。如何让 FlowLayout 显示 CardLayout 及其组件?

我已经阅读了我能找到的所有相关 docs,但我发现它们对解决这个问题没有太大帮助。

这是我的示例代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


@SuppressWarnings("serial")
public class RenderTest extends JPanel{
    private JFrame window;
    private FlowLayout topLevelLayout;
    private Slot[] slots; 

    public static void main(String[] args) {
        RenderTest instance = new RenderTest();
        instance.init();
    }

    private void init(){
        window = new JFrame("Render Test");

        topLevelLayout = new FlowLayout();
        window.setLayout(topLevelLayout);
        window.setResizable(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocationRelativeTo(null);

        slots = new Slot[]{new Slot(0), new Slot(2)};

        window.add(slots[0]);
        window.add(slots[1]);

        window.pack();
        window.setVisible(true);
    }

    private class Slot extends JPanel{
        JPanel panel;
        CardLayout cardLayout;
        public Slot(int index){
            RemoveButton remove = new RemoveButton(index);
            AddButton add = new AddButton(index);
            cardLayout = new CardLayout();
            panel = new JPanel();
            panel.setLayout(cardLayout);
            cardLayout.addLayoutComponent(add.getPanel(), "add");
            cardLayout.addLayoutComponent(remove.getPanel(), "show");
            topLevelLayout.addLayoutComponent("card"+index, panel);
        }
        private JPanel getPanel(){
            return this.panel;
        }
        private CardLayout getCardLayout(){
            return this.cardLayout;
        }
    }

    private class AddButton extends JPanel{
        JPanel panel;
        private AddButton(int index){
            panel = new JPanel();
            JButton addButton = new JButton("+");

            addButton.setVerticalTextPosition(AbstractButton.CENTER);
            addButton.setHorizontalTextPosition(AbstractButton.CENTER);
            addButton.setActionCommand("add"+index);
            addButton.addActionListener(new Button());

            panel.add(addButton);
        }
        private JPanel getPanel(){
            return this.panel;
        }
    }

    private class RemoveButton extends JPanel{
        JPanel panel;
        private RemoveButton(int index){
        panel = new JPanel();
        JButton removeButton = new JButton("-");

        removeButton.setVerticalTextPosition(AbstractButton.CENTER);
        removeButton.setHorizontalTextPosition(AbstractButton.CENTER);
        removeButton.setActionCommand("remove"+index);
        removeButton.addActionListener(new Button());

        panel.add(removeButton);
        }
        private JPanel getPanel(){
            return this.panel;
        }
    }

    class Button implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());

            if (e.getActionCommand().equals("add0")){
                slots[0].getCardLayout().show(getParent(), "show");
            }
            if (e.getActionCommand().equals("add1")){
                slots[1].getCardLayout().show(getParent(), "show");
            }
            if (e.getActionCommand().equals("remove0")){
                slots[0].getCardLayout().show(getParent(), "hide");
            }
            if (e.getActionCommand().equals("remove1")){
                slots[1].getCardLayout().show(getParent(), "hide");
            }
        }
    }
}

更新代码以使用 this 而不是 new JPanels:https://pastebin.com/e0fhkaen

成功了:pastebin 5XrFYarD

Slot class 中,不要创建新的 JPanel,您应该只使用

this.setLayout(cardLayout);

因为这个 class 扩展 JPanel.

就像现在一样,您只是在框架中添加了两个空的 JPanel

其他 class 也是如此(AddButtonRemoveButton