java - Swing:如何创建一个不会移动到西部组件上的东部组件

标签 java swing docking

我尝试了多种方法来使其正确渲染。我希望我的 Logo 区域在调整窗口大小时向东靠拢,但不与西方组件重叠。下面的代码不拥抱东,我认为这是有道理的,因为 logoArea 框不知道它应该占据其余的水平空间。

如果我将 Logo 区域直接添加到“区域”,那么它会向东延伸,但如果窗口缩小太多,则会与西部组件重叠。有什么想法吗?

 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

编辑

要回答@Nitin,我希望它向左移动,直到到达西部组件,然后停止移动并从右侧消失。

最佳答案

我真的不知道有什么标准布局可以满足您的需求,但创建一个并不难。检查这个小示例(十字形绘画显示内容和 Logo 边界):

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

这就是您希望 LOGO 在窗口大小变化时的表现吗?

附注这样,您还可以在需要添加(例如)另一个特定组件位置、 Logo 和内容之间的间隙或其他任何内容时随时修改布局...

关于java - Swing:如何创建一个不会移动到西部组件上的东部组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11214418/

相关文章:

Java for和if循环代码优化

java - 带有 https 的 tomcat 在直接 TCP 连接时获得 CLOSE_WAIT

java - 无法创建记录器 Tomcat 错误

java - 即使将 Border 设置为 EmptyBorder,JButton 的边框也不会消失

wpf - 如何在WPF中以均匀间距水平排列控件

java - 如何在相机捕获的图像上叠加图像或 View

java - 显示文件名并在 JTree 中的文件夹下选择它

java - 如何将 JTable 添加到从 JScrollPane 扩展的类中?

c# - 顶部对接控制32768像素以上

apache-flex - Adobe Flex 与 Silverlight