簡體   English   中英

我可以使用帶有JMenuBar的JTabbedPane嗎?

[英]Can I have a JTabbedPane with a JMenuBar?

我試圖增加可用於標簽內容的空間量。

如何在選項卡列表旁邊放置菜單欄或等效菜單欄? (最好在標簽的左側,與圖片相對)

帶紅色箭頭的TabbedPaneDemo

您可以從jide使用JideTabbedPane。

Jide是商業庫,但是此JideTabbedPane類是開放源代碼,請在此處獲取源代碼: http ://java.net/projects/jide-oss/

屏幕截圖如下。 在此處輸入圖片說明

不,如果不重寫整個BacisTabbedPaneUI,就不可能直接實現,所有示例都是各種質量的(外觀和感覺以及本機os非常敏感), aephyr的很好的示例

我的觀點JTabbedPane是*** JComponent,是通過GlassPane實現的有趣示例(您已經為JMenuBar設置了一些Borders,例如凸起的etchech和線條邊框??? :-)

瘋狂又骯臟的駭客

在此處輸入圖片說明

在此處輸入圖片說明

從代碼

import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class TabbedPaneWithManuBar {

    public void makeUI() {
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        for (int i = 0; i < 20; i++) {
            JPanel panel = new JPanel();
            panel.setName("tab" + (i + 1));
            panel.setPreferredSize(new Dimension(600, 100));
            tabbedPane.add(panel);
        }
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(tabbedPane);
        frame.pack();
        Rectangle tabBounds = tabbedPane.getBoundsAt(0);
        Container glassPane = (Container) frame.getRootPane().getGlassPane();
        glassPane.setVisible(true);
        glassPane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(tabBounds.y + 23, 0, 0, 5);
        gbc.anchor = GridBagConstraints.NORTHEAST;
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(createMenu("Menu Example 1"));
        menuBar.add(createMenu("Menu Example 1"));
        menuBar.add(createMenu("Menu Example 1"));
        menuBar.add(Box.createHorizontalGlue());
        menuBar.add(createMenu("About"));
        menuBar.setPreferredSize(new Dimension(menuBar.getPreferredSize().width , (int) tabBounds.getHeight() - 2));
        glassPane.add(menuBar, gbc);
        //JButton button = new JButton("My Button Position");
        //button.setPreferredSize(new Dimension(button.getPreferredSize().width, (int) tabBounds.getHeight() - 2));
        //glassPane.add(button, gbc);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JMenu createMenu(String title) {
        JMenu m = new JMenu(title);
        m.add("Menu item #1 in " + title);
        m.add("Menu item #2 in " + title);
        m.add("Menu item #3 in " + title);
        if (title.equals("About")) {
            m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }
        return m;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TabbedPaneWithManuBar().makeUI();
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM