![](/img/trans.png)
[英]Java - How do I prevent BorderLayout EAST from hugging the side of the screen?
[英]Swing: How do i create an east hugging component that will not move over western components
我已經嘗試了幾種方法來正確渲染它。 我希望我的徽標區域在窗口調整大小時擁抱東方,但不要與西方組件重疊。 以下代碼不會擁抱東方,我認為這是有道理的,因為logoArea框不知道它應該占用水平空間的其余部分。
如果我將logoArea直接添加到'area',那么它會向東方擁抱,但如果窗口收縮太多,它將與西方組件重疊。 有任何想法嗎?
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,我希望它向左移動直到它到達西部組件,然后停止移動並從右側消失。
我真的不知道任何標准布局會按你的意願行事,但創建一個並不難。 檢查這個小例子(十字形繪畫顯示內容和徽標邊界):
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如何操作?
PS這樣你也可以隨時修改布局(例如)另一個特定的組件位置,徽標和內容之間的差距或其他任何東西......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.