JavaSwing:如何将选项卡添加到JPanels中?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (98)

我有一系列选项卡,但我想在它显示的面板中添加更多选项卡,就像复制带状菜单一样,该怎样操作?

package components;

import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

public class ribbonMenu extends JPanel {
        public ribbonMenu() {
                super(new GridLayout(1,1));

                JTabbedPane tabbedPane = new JTabbedPane();

                JTabbedPane tabbedPane2 = new JTabbedPane();

                JComponent panel1 = makeTextPanel("Panel #1");
                tabbedPane.addTab("Tab 1", panel1);
                panel1.setPreferredSize(new Dimension(600, 400));

                JComponent panel2 = makeTextPanel("Panel #2");
                tabbedPane.addTab("Tab 2", panel2);

                JComponent panel3 = makeTextPanel("Panel #3");
                tabbedPane.addTab("Tab 3", panel3);

                add(tabbedPane);

                tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);           
        }

        protected JComponent makeTextPanel (String text) {
                JPanel panel = new JPanel(false);
                JLabel filler = new JLabel(text);
                filler.setHorizontalAlignment(JLabel.CENTER);
                panel.setLayout (new GridLayout(1,1));
                panel.add(filler);
                return panel;
        }

        private static void createAndShowGUI() {
            JFrame frame = new JFrame ("ribbonMenu");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.add(new ribbonMenu(), BorderLayout.CENTER);

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

        public static void main(String[] args) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        UIManager.put("swing.boldmetal", Boolean.FALSE);
                        createAndShowGUI();
                    }

                });
        }
}
提问于
用户回答回答于

将以下代码:

JComponent panel1 = makeTextPanel("Panel #1");

改为;

JPanel panel1 = (JPanel)makeTextPanel("Panel #1");

可以在小组里再次添加JTabbedPane

所属标签

可能回答问题的人

  • HKC

    红客学院 · 创始人 (已认证)

    26 粉丝7 提问5 回答
  • Dingda

    Dingda · 站长 (已认证)

    4 粉丝0 提问3 回答
  • 螃蟹居

    1 粉丝0 提问2 回答
  • 西风

    renzha.net · 站长 (已认证)

    9 粉丝1 提问2 回答

扫码关注云+社区

领取腾讯云代金券