我可以有一个带有 JMenuBar 的 JTabbedPane 吗? [英] Can I have a JTabbedPane with a JMenuBar?

查看:36
本文介绍了我可以有一个带有 JMenuBar 的 JTabbedPane 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试增加标签内容的可用空间.

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)

推荐答案

不,直接不可能不覆盖整个 BacisTabbedPaneUI,所有示例都是各种质量(外观和原生操作系统非常敏感),aephyr 很好的例子

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,

我认为 JTabbedPane 是 *** JComponent,实现 GlassPane 的有趣示例(您已经为 JMenuBar 设置了一些边框,例如凸起的蚀刻和线条边框 ??? :-)

my view JTabbedPane is *** JComponent, funny example by implements GlassPane (you have set a some Borders for JMenuBar e.g. raised etchech & line border ??? :-)

疯狂而肮脏的黑客

来自代码

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

这篇关于我可以有一个带有 JMenuBar 的 JTabbedPane 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆