简体   繁体   English

如何在垂直模式下以不同的对齐方式向JPanel添加不同的JComponent?

[英]how to add different JComponent to JPanel in vertical mode with different alignment?

I have a JPanel(which has scrollbar) and I want to add some JTextPans and JPanel dynamically to it. 我有一个JPanel(具有滚动条),我想向其中动态添加一些JTextPans和JPanel。 and represent them in vertical mode I have try BoxLayout.Y_AXIS, but the problem is it respects the component's maximum size. 并以垂直模式表示它们,我尝试使用BoxLayout.Y_AXIS,但问题是它尊重组件的最大大小。 and when I add more JComponent their size become smaller in way that their content is not viewable. 当我添加更多的JComponent时,它们的大小会变小,以致其内容无法查看。

how can I make BoxLayout respects the component's correct size (fix one), or is there another solution to represent different component in vertical mode i jpanel? 我怎样才能使BoxLayout尊重组件的正确大小(固定一个),或者是否有另一种解决方案可以在垂直模式下显示不同的组件?

You can override 您可以覆盖

public Dimension minimumLayoutSize(Container target)

method of BoxLayout to return preferredLayoutSize() BoxLayout的方法以返回preferredLayoutSize()

OR 要么

You can override getMinimumSize() method of all the added component to return getPreferedSize() 您可以重写所有添加的组件的getMinimumSize()方法以返回getPreferedSize()

OR 要么

choose another layout manager - eg GridBagLayout 选择另一个布局管理器-例如GridBagLayout

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

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