java - 在 FlowLayout 中并排嵌套多个 CardLayout

标签 java layout nested cardlayout flowlayout

我试图让多个 CardLayout 并排显示,全部位于 FlowLayout 内。一切运行正常,但窗口中没有显示任何内容。如何使 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 JPanel:https://pastebin.com/e0fhkaen

开始工作:pastebin 5XrFYarD

最佳答案

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

this.setLayout(cardLayout);

因为此类扩展了JPanel

现在,您只需向框架添加两个空的 JPanel 即可。

其他类(AddButtonRemoveButton)也是如此

关于java - 在 FlowLayout 中并排嵌套多个 CardLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46625971/

相关文章:

java - 以编程方式编译 Java 类?

java - 从 xml 字符串中提取元素

java - 如何管理 JFrame 内 JPanel 的大小

java - 消失时可调整大小的布局 Swing JPanel

ruby-on-rails - Ruby on Rails : Nested named scopes

java - 如何知道给定字符串是否是Java中另一个字符串的子字符串

Android TextView 基线相关边距

python - 嵌套一个函数

python - BeautifulSoup find - 从感兴趣的 block 中排除嵌套标签

java - 扩展微软的 JDBC 驱动程序的可行性如何?