摆动动态(自动)拟合布局 [英] Swing dynamic(auto) fit-layouting

查看:112
本文介绍了摆动动态(自动)拟合布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑100(动态)JLabel对象,我想将它们显示在可调整大小的JPanel中.
目前,我使用网格袋布局(2列50行),但是当用户调整大小并扩展表格时,我希望拥有(例如)4列25行,而对于小尺寸(1列)也是如此和100行),以其他方式填满整个面板(没有任何空格).
我知道这应该手动完成,目前我会在用户调整表单大小后重新绘制(重新绘制)所有成员,但是我只是想知道是否有准备好的解决方案.
谢谢你.

Considering 100(dynamic) JLabel Object, and I want to show them inside a resizable JPanel.
Currently I use grid bag layout (2 columns and 50 rows), but when user resizes and expands the form, I want to have(for example) 4 columns and 25 rows, and same for small form(1 column, and 100 rows), in other way fill up the whole panel(no any white spaces).
I know this is should be done manually, currently I redraw(repaint) all members once user resize the form, but I just was wondering if there is any prepared solution to do so.
Thanks in advanced.

推荐答案

看看Rob Camick的 WrapLayout .

Have a look at Rob Camick's WrapLayout.

示例用法

import java.awt.*;
import javax.swing.*;

public class TestWrapLayout {
    public TestWrapLayout () {
        ImageIcon icon = new ImageIcon(getClass().getResource("/resources/stackoverflow2.png"));
        JPanel panel = new JPanel(new WrapLayout());
        for (int i = 1; i <= 250; i++) {
            JLabel iconlabel = new JLabel(icon);
            iconlabel.setLayout(new BorderLayout());
            JLabel textlabel = new JLabel(String.valueOf(i));
            textlabel.setHorizontalAlignment(JLabel.CENTER);
            textlabel.setForeground(Color.WHITE);
            textlabel.setFont(new Font("impact", Font.PLAIN,20));
            iconlabel.add(textlabel);
            panel.add(iconlabel);
        }
        JFrame frame = new JFrame();
        frame.add(new JScrollPane(panel));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TestWrapLayout();
            }
        });
    }
}

这篇关于摆动动态(自动)拟合布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆