在JTabbedPane标头中添加一个JLabel [英] Add a JLabel in the JTabbedPane header

查看:56
本文介绍了在JTabbedPane标头中添加一个JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有JTabbedPane的JDialog:

I have this JDialog with a JTabbedPane:

我只想在JTabbedPane的右上角添加一个JLabel,这样我就可以触发一些事件(例如,关闭JDialog). 而且我不想使其成为JFrame.

I just want to add a JLabel in the right-top area of the JTabbedPane so i can fire some events (e.g close the JDialog). And i don't want to make it a JFrame.

PS:我知道这可能是重复的,但是没有一个答复给我解决方案.

PS: I know that is may be duplicated, but none of the responses give me a solution.

推荐答案

实际上,Swing不支持JTabbedPane内部的定位.但是,您始终可以使用布局和图层来制作一些街头魔术:

Well, actually that positioning inside JTabbedPane is not supported in Swing. But you can always make some street magic with layouts and layers:

public static void main ( String[] args )
{
    try
    {
        UIManager.setLookAndFeel ( UIManager.getSystemLookAndFeelClassName () );
    }
    catch ( Throwable e )
    {
        e.printStackTrace ();
    }

    JFrame frame = new JFrame ();
    frame.setUndecorated ( true );

    frame.setLayout ( new LayoutManager ()
    {
        private List<Component> special = new ArrayList<Component> ();

        public void addLayoutComponent ( String name, Component comp )
        {
            if ( name != null )
            {
                special.add ( comp );
            }
        }

        public void removeLayoutComponent ( Component comp )
        {
            special.remove ( comp );
        }

        public Dimension preferredLayoutSize ( Container parent )
        {
            Dimension ps = new Dimension ();
            for ( Component component : parent.getComponents () )
            {
                if ( !special.contains ( component ) )
                {
                    Dimension cps = component.getPreferredSize ();
                    ps.width = Math.max ( ps.width, cps.width );
                    ps.height = Math.max ( ps.height, cps.height );
                }
            }
            return ps;
        }

        public Dimension minimumLayoutSize ( Container parent )
        {
            return preferredLayoutSize ( parent );
        }

        public void layoutContainer ( Container parent )
        {
            Insets insets = parent.getInsets ();
            for ( Component component : parent.getComponents () )
            {
                if ( !special.contains ( component ) )
                {
                    component.setBounds ( insets.left, insets.top,
                            parent.getWidth () - insets.left - insets.right,
                            parent.getHeight () - insets.top - insets.bottom );
                }
                else
                {
                    Dimension ps = component.getPreferredSize ();
                    component.setBounds ( parent.getWidth () - insets.right - 2 - ps.width,
                            insets.top + 2, ps.width, ps.height );
                }
            }
        }
    } );

    final JTabbedPane tabbedPane = new JTabbedPane ();
    tabbedPane.addTab ( "Tab1", new JLabel () );
    tabbedPane.addTab ( "Tab2", new JLabel () );
    tabbedPane.addTab ( "Tab3", new JLabel () );
    frame.add ( tabbedPane );

    final JLabel label = new JLabel ( "Close X" );
    label.addMouseListener ( new MouseAdapter ()
    {
        public void mousePressed ( MouseEvent e )
        {
            System.exit ( 0 );
        }
    } );
    frame.add ( label, "special", 0 );

    frame.setSize ( 200, 150 );
    frame.setLocationRelativeTo ( null );
    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.setVisible ( true );
}

这将在任何地方都可以正常工作.请注意,标签将始终位于右上角,并且不会自行定位(除非您修改代码以支持更多功能")在其他OS LaF(如Mac OS Laf)上,其中选项卡可能位于中间.如果标签与标签交叉,也将被涂在标签上.

This will work anywhere without any problems. Just be aware that label will be always in top right corner and will not position itself (unless you modify the code to support more "features") on other OS LaF's like Mac OS laf where tabs might be in the middle. Also it will be painted over tabs if they will cross with label.

所以您会得到这样的东西:

So you will get something like this:

无论如何,您可以轻松地修改应用于布局内部的JLabel边界以进行一些侧面间距,例如...

Anyway, you can easily modify applied to JLabel bounds inside the layout to make some side-spacing and such...

这篇关于在JTabbedPane标头中添加一个JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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