简体   繁体   中英

adding new jpanel with default components in swing

任何人都可以告诉我如何在我单击“添加”按钮时在jtabbedPane中添加面板。它类似于google chrome的新标签。但是问题是,生成的面板必须包含一些默认组件。在此先感谢您。

Please see the code below. It shows you how to do what you need.

public class DemoApp {
    private JTabbedPane tabPane = new JTabbedPane();

    public DemoApp() {
        initComponents();
    }

    private void initComponents() {
        JFrame frame = new JFrame("Test");
        frame.setSize(500, 400);
        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        frame.getContentPane().add(panel);

        JButton btn = new JButton("Add panel");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int index = tabPane.getTabCount() + 1;
                JPanel newPanel = new JPanel();
                newPanel.setLayout(new FlowLayout());
                newPanel.add(new JLabel("Panel " + index));
                tabPane.addTab("Tab " + index, newPanel);
            }
        });
        panel.add(tabPane, BorderLayout.CENTER);
        panel.add(btn, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new DemoApp();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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