• Swing布局 - SpringLayout


    简介

    SpringLayout是弹性布局,适用于表单的展示。弹性针对的是该布局中组件的高度、宽度会随着Container的变化自动变化。
    弹性距离在SpringLayout中使用javax.swing.Spring接口来表示,该接口主要提供以下方法:

    • getMinimumValue : 最小值
    • getPreferredValue : 偏好值
    • getMaximumValue : 最大值
    • getValue : 当前值

    往使用了Spring布局的Container中添加Component后,需要指定元素的坐标 (x, y),高度与宽度。与绝对布局不同的是,Spring布局指定坐标,高宽等信息不是用代表像素的int值,而是需要传递一个代表弹性距离的Spring对象。

    使用方式

    常用API

    // 创建固定长度的Spring对象
    Spring x = Spring.constant(10);
    // 为Container指定使用SpringLayout布局
    JPanel panel = new JPanel(new SpringLayout());
    // 从container中获取SpringLayout
    SpringLayout layout = (SpringLayout)panel.getLayout();
    // 获取组件的Constraints对象
    panel.add(new JLabel());
    SpringLayout.Constraints constraints = layout.getConstraints(panel.getComponent(0));
    // constraint对象常用操作
    Spring width = constraints.getWidth();
    constraints.setX(x);
    // 两个Spring对象求和
    x = Spring.sum(x, width);
    // 两个Spring对象求最大值
    Spring maxWidth = Spring.max(width1, width2);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    对于表单布局,jdk提供了工具类直接帮你完成Constraints 的设置。

    JPanel panel = new JPanel(new SpringLayout());
    String[] labels = {"用户名", "密码", "手机号"};
    for (String label : labels) {
    	// 文本右对齐
    	JLabel label = new JLabel(label, SwingConstants.TRAILING);
    	panel.add(label);
    	JTextField textField = new JTextField(30);
    	label.setLabelFor(textField);
        panel.add(textField);
    }
    // jdk提供的工具类,完成表单使用Spring布局
    SpringUtilities.makeCompactGrid(dialogPanel, labels.length, 2, 10, 10, 10, 10);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    SpringUtilities类定义如下:

    public class SpringUtilities {
        public static void makeCompactGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad) {
            SpringLayout layout = (SpringLayout) parent.getLayout();
            // 设置元素的x坐标和宽度
            Spring x = Spring.constant(initialX);
            for (int c = 0; c < cols; c++) {
                // 取每一行该列的宽度,求最大值,计算得出该列的宽度
                Spring width = Spring.constant(0);
                for (int r = 0; r < rows; r++) {
                    width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
                }
                // 设置该列每个元素的x坐标和宽度
                for (int r = 0; r < rows; r++) {
                    SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
                    constraints.setX(x);
                    constraints.setWidth(width);
                }
                // 下一列的x坐标 = 当前x + 当前列宽 + xPad
                x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
            }
            // 设置元素的y坐标和高度
            Spring y = Spring.constant(initialY);
            for (int r = 0; r < rows; r++) {
                Spring height = Spring.constant(0);
                for (int c = 0; c < cols; c++) {
                    height = Spring.max(height, getConstraintsForCell(r, c, parent, cols).getHeight());
                }
                for (int c = 0; c < cols; c++) {
                    SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
                    constraints.setY(y);
                    constraints.setHeight(height);
                }
                y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
            }
            // 设置父组件的约束,子组件设置的initialX和initialY相当于设置WEST、NORTH,还需要手动设置SOUTH、EAST的间距
            SpringLayout.Constraints pConstraints = layout.getConstraints(parent);
            pConstraints.setConstraint(SpringLayout.EAST, x);
            pConstraints.setConstraint(SpringLayout.SOUTH, y);
        }
    
        private static SpringLayout.Constraints getConstraintsForCell(int row, int col, Container parent, int cols) {
            SpringLayout layout = (SpringLayout)parent.getLayout();
            Component c = parent.getComponent(row * cols + col);
            return layout.getConstraints(c);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
  • 相关阅读:
    [附源码]Python计算机毕业设计SSM开放性实验室网上预约管理(程序+LW)
    C++ Qt 学习(三):无边框窗口设计
    SpringCloud集成链路追踪Sleuth+Zipkin
    python免费调用阿里云通义千问(q-wen-max)大模型API
    “人类高质量数据”如何训练计算机视觉模型?
    Kamiya丨Kamiya艾美捷狗CRP ELISA说明书
    RUST 每日一省:模式匹配
    Git版本控制管理
    Java项目:ssm+mysql医药进销存系统
    粒子特效-Xffect
  • 原文地址:https://blog.csdn.net/liuzhenghua66/article/details/134540916