我需要类似于GridLayout
但不调整组件大小的解决方案JPanel
。
一切都很好用,但JFrame
我需要把那些组件放进去。JPanel
JFrame
我需要类似于GridLayout
但不调整组件大小的解决方案JPanel
。
一切都很好用,但JFrame
我需要把那些组件放进去。JPanel
JFrame
以下链接可能会帮助您选择最适合您需要的布局。它的 Java 教程称为“布局管理器的可视化指南”,它显示了每个布局的漂亮图片以及它们的外观......
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
否则,如果您说GridLayout
与您需要的相似,您总是可以编写自己的MyGridLayout
类(扩展GridLayout
)并覆盖执行自动调整大小的方法。
我自己偶然发现了这个问题,尽管我认为使用 z JList 可能是一个很好的解决方案,但还有一种更简单的方法可以使用自定义的 FLowLayout,称为 WrapLayout,可在此处获得:https ://tips4java.wordpress.com/2008 /11/06/包装布局/
我最终在我的项目中使用了它,并且效果很好。我遇到的唯一问题是当我将窗口设置为全屏模式时,布局没有正确更新。我使用了一个简单的解决方法,就是这样:
//int targetWidth = target.getSize().width;
int targetWidth = target.getParent().getSize().width; // FIXME: this is a hack for getting the correct size when switching between full screen modes on Mac
有了这个小技巧,它就可以完美运行。
只需在 flowlayout 中覆盖 preferredLayoutSize() 并将其设置为最大大小。将对齐设置为 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;
}