简体   繁体   English

我可以使用带有JMenuBar的JTabbedPane吗?

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

I'm trying to increase the amount of space available for tab content. 我试图增加可用于标签内容的空间量。

How can I put a menu bar, or equivalent, next to a list of tabs? 如何在选项卡列表旁边放置菜单栏或等效菜单栏? (preferably on the left of the tabs, opposite of image) (最好在标签的左侧,与图片相对)

带红色箭头的TabbedPaneDemo

You can use JideTabbedPane from jide. 您可以从jide使用JideTabbedPane。

Jide is commerical library, but this JideTabbedPane class are open source, get source code here: http://java.net/projects/jide-oss/ Jide是商业库,但是此JideTabbedPane类是开放源代码,请在此处获取源代码: http ://java.net/projects/jide-oss/

the screenshot is like below. 屏幕截图如下。 在此处输入图片说明

not, directly not possible without override whole BacisTabbedPaneUI, all examples are various quality (look and feel and native os very sensitive), very good example by aephyr , 不,如果不重写整个BacisTabbedPaneUI,就不可能直接实现,所有示例都是各种质量的(外观和感觉以及本机os非常敏感), aephyr的很好的示例

my view JTabbedPane is *** JComponent, funny example by implements GlassPane (you have set a some Borders for JMenuBar eg raised etchech & line border ??? :-) 我的观点JTabbedPane是*** JComponent,是通过GlassPane实现的有趣示例(您已经为JMenuBar设置了一些Borders,例如凸起的etchech和线条边框??? :-)

crazy and dirty hack 疯狂又肮脏的骇客

在此处输入图片说明

在此处输入图片说明

from code 从代码

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