未显示面板内的swing面板

qgelzfjb  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(190)

我想创造一个 JFrame 一个主面板包含两个面板:左侧子面板有添加和删除按钮,可以动态添加和删除组件;右面板包含常规组件。我的代码在只有一个面板时工作,在子面板内部使用时失败。

public class MultiPanel extends JFrame{

    static MultiPanel myFrame;
    static int countMe = 0;
    JPanel mainPanel;
    JPanel userPanel;
    JPanel contentPanel;

    private static void iniComponents() {
        myFrame = new MultiPanel();
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.prepareUI();
        myFrame.pack();
        myFrame.setVisible(true);
    }

    private void prepareUI() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("My Title");
        setPreferredSize(new java.awt.Dimension(1280, 720));
        setSize(new java.awt.Dimension(1280, 720));

        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));

        userPanel = new JPanel();
//      userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.Y_AXIS));

        mainPanel.add(userPanel);

        JButton buttonAdd = new JButton("Add subPanel");
        buttonAdd.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                userPanel.add(new subPanel());
                myFrame.pack();
            }
        });

        JButton buttonRemoveAll = new JButton("Remove All");
        buttonRemoveAll.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                userPanel.removeAll();
                myFrame.pack();
            }
        });

        contentPanel = new JPanel();
        JLabel jLabel1 = new JLabel("Content here");
        contentPanel.add(jLabel1);
        mainPanel.add(contentPanel);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel)
        );

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel)
        );

//      getContentPane().add(userPanel, BorderLayout.WEST);
//      getContentPane().add(contentPanel, BorderLayout.EAST);       
//      getContentPane().add(buttonAdd, BorderLayout.PAGE_START);
//      getContentPane().add(buttonRemoveAll, BorderLayout.PAGE_END);
    }

    private class subPanel extends JPanel {

        subPanel me;

        public subPanel() {
            super();
            me = this;
            JLabel myLabel = new JLabel("This is  subPanel(): " + countMe++);
            add(myLabel);
            JButton myButtonRemoveMe = new JButton("remove me");
            myButtonRemoveMe.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    me.getParent().remove(me);
                    myFrame.pack();
                }
            });
            add(myButtonRemoveMe);
        }              
    }

    public static void main(String args[]) {

        SwingUtilities.invokeLater(() -> {
            iniComponents();
        });
    }
}

我也想知道什么是正确的方式来写布局,对齐和组织组件似乎是痛苦的我。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题