1

我有一系列选项卡,但我想在它显示的面板中添加更多选项卡,就像复制功能区菜单一样。我大致知道需要做什么(更改 makeTextPanel 函数的返回类型),但不知道如何做,因此将不胜感激。

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();
                    }

                });
        }
}

谢谢

4

1 回答 1

0

为什么需要更改返回类型?

而不是这个

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

利用

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

对于更多选项卡,您可以再次JTabbedPane在该面板中添加

于 2013-02-22T20:59:50.670 回答