java 我可以使用 FlowLayout 在 JPanel 中获得多行吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10210222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I get multiple rows in JPanel with FlowLayout
提问by Daniel Jab?oński
I need solution similar to GridLayout
but without resizing components in JPanel
.
我需要类似于GridLayout
但不调整JPanel
.
Everything works great with JFrame
, but I need to put those components into JPanel
instead JFrame
.
一切的伟大工程同JFrame
,但我需要把这些组件集成到JPanel
代替JFrame
。
回答by trashgod
回答by wattostudios
The following link might help you to choose the most appropriate layout for your needs. Its the Java Tutorial called "A Visual Guide to Layout Manager", which shows nice pictures of each layout and what they look like...
以下链接可能会帮助您选择最适合您需要的布局。它的 Java 教程称为“布局管理器的可视化指南”,其中显示了每个布局的精美图片及其外观......
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Otherwise, if you say GridLayout
is similar to what you need, you could always write your own MyGridLayout
class (that extends GridLayout
) and overwrite the method that does the autoresizing.
否则,如果您说GridLayout
的与您需要的相似,您始终可以编写自己的MyGridLayout
类(即扩展类GridLayout
)并覆盖执行自动调整大小的方法。
回答by Ravinda lakshan
Just Override preferredLayoutSize() in flowlayout and set Maximum size to it. set Alignment as LEADING and set it to your JPanel. You'll get what you want
只需覆盖 flowlayout 中的 preferredLayoutSize() 并将最大大小设置为它。将 Alignment 设置为 LEADING 并将其设置为您的 JPanel。你会得到你想要的
private FlowLayout getFlowLayout(int maximumSize)
{
if (flowLayout == null)
{
flowLayout = new FlowLayout()
{
@Override
public Dimension preferredLayoutSize(Container target)
{
Dimension dimension = super.preferredLayoutSize(target);
dimension.width = Math.min(maximumSize, dimension.width);
return dimension;
}
};
flowLayout.setAlignment(FlowLayout.LEADING);
}
return flowLayout;
}