我在做一个swing程序的时候想做一个类似于文档流的界面布局:
在一个JPanel里面动态放置一些面板(如jPanel),这些面板的尺寸大小都是相同的,考虑到不同尺寸屏幕的使用方便性来说,最好是当第一行满时,会自动换行到第二行。如果超过了当前JPanle的高度,则会出现上下滚动条。
一开始我用JPanel+FlowLayout.LEFT布局,倒是可以自动换行,但是发现当面板占据空间的高度高于JPanel的高度时,没有上下方向的滚动条出现,也就是说,下面的内容都被遮住了。
然后我在JPanel外面套了一个JScrollPane,却发现内容会一直往右边加,超出JPanel的宽度则出现横向的滚动条,与预想的效果相差较大。
于是我把JScrollPane的horizontalScrollBarPolicy属性设为ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER,这回横向滚动条没了,但是图片还是一直往右边加,超出JPanle宽度部分被遮住了。
经过上网检索资料,有一个方法可以实现,即继承FLowLayout类,并重写minimumLayoutSize方法和preferredLayoutSize方法,不过帖子上的代码有点小问题,后来我仔细研究代码和源码,进行了修正,最后实现了想要的效果,下面把正确的代码贴出来,供有此类需求的人使用
package com.xxxx.swing.layout; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Insets; public class ModifiedFlowLayout extends FlowLayout { public ModifiedFlowLayout() { super(); } public ModifiedFlowLayout(int align) { super(align); } public ModifiedFlowLayout(int align, int hgap, int vgap) { super(align, hgap, vgap); } public Dimension minimumLayoutSize(Container target) { return computeSize(target, true); } public Dimension preferredLayoutSize(Container target) { return computeSize(target, false); } private Dimension computeSize(Container target, boolean minimum) { synchronized (target.getTreeLock()) { int hgap = getHgap(); int vgap = getVgap(); int w = target.getWidth(); if (w == 0) { w = Integer.MAX_VALUE; } Insets insets = target.getInsets(); if (insets == null) { insets = new Insets(0, 0, 0, 0); } int reqdWidth = 0; int maxwidth = w - (insets.left + insets.right + hgap * 2); int n = target.getComponentCount(); int x = 0; int y = insets.top; int rowHeight = 0; for (int i = 0; i < n; i++) { Component c = target.getComponent(i); if (c.isVisible()) { Dimension d = minimum ? c.getMinimumSize() : c.getPreferredSize(); if ((x == 0) || ((x + d.width) <= maxwidth)) { if (x > 0) { x += hgap; } x += d.width; rowHeight = Math.max(rowHeight, d.height); } else { x = d.width; y += vgap + rowHeight; rowHeight = d.height; } reqdWidth = Math.max(reqdWidth, x); } } y += rowHeight; return new Dimension(reqdWidth + insets.left + insets.right, y); } } }
最终的效果(不想截图了,从网上找了个类似的效果图片贴上来吧):
相关推荐
Android流式布局FlowLayout,用起来爽歪歪,好用=-=-=-=-=
主要介绍了Java Swing组件布局管理器之FlowLayout(流式布局),结合实例形式分析了Swing组件布局管理器FlowLayout流式布局的常用方法及相关使用技巧,需要的朋友可以参考下
Qt FlowLayout布局 Qt FlowLayout布局 Qt FlowLayout布局 Qt FlowLayout布局
QtFlowlayout 流式布局器 源代码,可根据QWidget控件内的数量自动布局界面
FlowLayout是一个流试布局,自动适配换行,实现了单选选中事件
Android 实现FlowLayout流式布局(类似热门标签) , 使用前请认真阅读文件顶部的使用说明
流失布局,FlowLayout,FlowLayout,FlowLayoutFlowLayout
flowlayout流布局
基于JAVA的布局管理器-FlowLayout
java FlowLayout布局管理 java FlowLayout布局管理 java FlowLayout布局管理
一个适用于Android的FlowLayout,当空间不足时,它允许子视图流到下一行。 子视图之间的间距可以通过FlowLayout计算,以便将视图均匀放置。 Gradle AndroidX: implementation 'com.nex3z:flow-layout:1.3.3' ...
FlowLayout自定义Layout, 重写onMeasure和onLayout,根据当前行是否能容下子view, 来判定是否换行Preview
FlowLayout - 适用于iOS的双向布局框架
Android实现FlowLayout流式布局(仿热门标签)
flowLayout,顺序布局,自动换行。
流式布局,自动计算高度,自动换行,具体效果(可以参考微信收藏的标签编辑页)
compile "com.wefika:flowlayout:" Maven com.wefika flowlayout [version] Usage License Copyright 2013 Blaž Šolar Licensed under the Apache License, Version 2.0 (the "License"); you
Android 流式布局FlowLayout 实现关键字标签