简体   繁体   English

Swing:我如何创建一个不会移动西方组件的东方拥抱组件

[英]Swing: How do i create an east hugging component that will not move over western components

I've tried several ways to get this to render correctly. 我已经尝试了几种方法来正确渲染它。 I want my logo area to hug east as the window is resized, but not to overlap the Western components. 我希望我的徽标区域在窗口调整大小时拥抱东方,但不要与西方组件重叠。 The following code does not hug east, which i think makes sense because the the logoArea box does not know it should take up the rest of the horizontal space. 以下代码不会拥抱东方,我认为这是有道理的,因为logoArea框不知道它应该占用水平空间的其余部分。

If i add the logoArea directly to the 'area' then it hugs east, but will overlap the western components if the window shrinks too much. 如果我将logoArea直接添加到'area',那么它会向东方拥抱,但如果窗口收缩太多,它将与西方组件重叠。 Any ideas? 有任何想法吗?

 Box box = Box.createHorizontalBox();
 box.add( main );
 for( JComponent item : items ) //western stuff
 {
    box.add( Box.createHorizontalStrut( 8 ) );
    box.add( item );
 }

 //eastern stuff
 Box logoArea= Box.createHorizontalBox();
 logoArea.add( new JLabel( LAF.Icon.png( "CompanyLogo" ) ), BorderLayout.EAST );

 box.add( Box.createHorizontalStrut( 8 ) );
 box.add( logoArea, BorderLayout.EAST );

 JPanel area = new JPanel( new BorderLayout() );
 area.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) );
 area.add( box, BorderLayout.WEST );
 return area;  //dashboard is built

EDIT 编辑

To answer @Nitin i'd like it to move left until it reaches the western components, and then stop moving and disapear from the right side. 要回答@Nitin,我希望它向左移动直到它到达西部组件,然后停止移动并从右侧消失。

I don't really know any standart layout that would act as you want, but its not hard to create one. 我真的不知道任何标准布局会按你的意愿行事,但创建一个并不难。 Check this small example (cross-shaped paintings displays content and logo bounds): 检查这个小例子(十字形绘画显示内容和徽标边界):

public static void main ( String[] args )
{
    JFrame frame = new JFrame ();

    LogoLayout layout = new LogoLayout ();
    frame.setLayout ( layout );

    frame.add ( new JLabel ( "Label with same preferred size as text length" )
    {
        protected void paintComponent ( Graphics g )
        {
            super.paintComponent ( g );

            g.setColor ( Color.BLACK );
            g.drawLine ( 0, 0, getWidth (), getHeight () );
            g.drawLine ( getWidth (), 0, 0, getHeight () );
        }
    }, layout.CONTENT );

    frame.add ( new JComponent ()
    {
        protected void paintComponent ( Graphics g )
        {
            g.setColor ( Color.BLACK );
            g.drawLine ( 0, 0, getWidth (), getHeight () );
            g.drawLine ( getWidth (), 0, 0, getHeight () );
        }

        public Dimension getPreferredSize ()
        {
            return new Dimension ( 100, 100 );
        }
    }, layout.LOGO );

    frame.setSize ( 700, 500 );
    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}

public static class LogoLayout implements LayoutManager
{
    public String CONTENT = "Content";
    public String LOGO = "Logo";

    private Map<Component, String> constraints = new HashMap<Component, String> ();

    public void addLayoutComponent ( String name, Component comp )
    {
        constraints.put ( comp, name );
    }

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

    public void layoutContainer ( Container parent )
    {
        Insets bi = parent.getInsets ();
        int contentSize = 0;
        int logoSize = 0;
        int maxHeight = 0;
        for ( Component component : parent.getComponents () )
        {
            Dimension ps = component.getPreferredSize ();
            if ( constraints.get ( component ).equals ( CONTENT ) )
            {
                contentSize = Math.max ( ps.width, contentSize );
            }
            else if ( constraints.get ( component ).equals ( LOGO ) )
            {
                logoSize = Math.max ( ps.width, logoSize );
            }
            maxHeight = Math.max ( ps.height, maxHeight );
        }

        int width = parent.getWidth () - bi.left - bi.right;
        int height = parent.getHeight () - bi.top - bi.bottom;
        for ( Component component : parent.getComponents () )
        {
            if ( constraints.get ( component ).equals ( CONTENT ) )
            {
                if ( contentSize + logoSize < width )
                {
                    component.setBounds ( bi.left, bi.top, width - logoSize, height );
                }
                else
                {
                    component.setBounds ( bi.left, bi.top, contentSize, height );
                }
            }
            else if ( constraints.get ( component ).equals ( LOGO ) )
            {
                if ( contentSize + logoSize < width )
                {
                    component
                            .setBounds ( bi.left + width - logoSize, bi.top, logoSize, height );
                }
                else
                {
                    int scaledLogoSize = width - contentSize;
                    if ( scaledLogoSize > 0 )
                    {
                        component.setBounds ( bi.left + width - scaledLogoSize, bi.top,
                                scaledLogoSize, height );
                    }
                }
            }
        }
    }

    public Dimension preferredLayoutSize ( Container parent )
    {
        Insets bi = parent.getInsets ();
        int contentSize = 0;
        int logoSize = 0;
        int maxHeight = 0;
        for ( Component component : parent.getComponents () )
        {
            Dimension ps = component.getPreferredSize ();
            if ( constraints.get ( component ).equals ( CONTENT ) )
            {
                contentSize = Math.max ( ps.width, contentSize );
            }
            else if ( constraints.get ( component ).equals ( LOGO ) )
            {
                logoSize = Math.max ( ps.width, logoSize );
            }
            maxHeight = Math.max ( ps.height, maxHeight );
        }
        return new Dimension ( bi.left + contentSize + logoSize + bi.right,
                bi.top + maxHeight + bi.bottom );
    }

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

Is that how you want your LOGO to act when window is getting sized? 当窗口大小变化时,您希望LOGO如何操作?

PS This way you can also modify the layout anytime you need to add (for example) another specific component location, some gap between logo and content or anything else... PS这样你也可以随时修改布局(例如)另一个特定的组件位置,徽标和内容之间的差距或其他任何东西......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM