Java弹簧布局管理器使用方法详解

时间:2022-11-21 00:20:43

        由 SpringLayout 类实现的布局管理器称为弹簧布局管理器。利用该布局管理器管理组件,当改变窗体的大小时,能够在不改变组件间相对位置的前提下自动调整组件大小,使组件依旧布满整个窗体,从而保证了窗体的整体效果。下面,通过一个实例来看看弹簧布局管理器的使用方法和效果。

        弹簧布局管理器以容器和组件的边缘为操作对象,通过为组件和容器边缘以及组件和组件边缘建立约束,实现对组件布局的管理。通过方法 putConstraint(String e1,Conponet c1,int pad,String e2,Componet c2)可以为各边之间建立约束,该方法的入口参数说明如下:

c1:需要参考的组件对象;

c2:需要参考的组件对象的具体需要参考的边;

e1:被参考的组件对象;

e2:被参考的组件对象的具体被参考的边;

 pad:两条边之间的距离,即两个组件之间的间距。

        有点绕,其实很好理解。

        实例1:使用弹簧布局管理器。

        本实例利用弹簧布局管理器实现窗体,在调整窗体的大小后,组件仍会布满整个窗体,并且组件间的相对位置并不会改变。源码:

?
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Spring;
import javax.swing.SpringLayout;
 
 
public class SpringLayout_3 extends JFrame {
 
    private static final long serialVersionUID = -6045918631932051025L;
 
    public SpringLayout_3() {
        // TODO Auto-generated constructor stub
        Container container = getContentPane();
        SpringLayout springLayout = new SpringLayout();
        container.setLayout(springLayout);
        JLabel topicLabel = new JLabel("主题 :");
        JLabel contentLabel = new JLabel("内容 :");
        final JTextField textField = new JTextField(30);
        JTextArea textArea = new JTextArea(3, 30);
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(textArea);
        textArea.setLineWrap(true);
        final JButton confirmButton = new JButton("取消");
        JButton cancelButton = new JButton("确认");
        Spring st = Spring.constant(10);
        Spring st2 = Spring.constant(30);
        container.add(topicLabel);
        springLayout.putConstraint(SpringLayout.NORTH, topicLabel, st,
                SpringLayout.NORTH, container);
        springLayout.putConstraint(SpringLayout.WEST, topicLabel, st,
                SpringLayout.WEST, container);
        container.add(textField);
        springLayout.putConstraint(SpringLayout.WEST, textField, st2,
                SpringLayout.EAST, topicLabel);
        springLayout.putConstraint(SpringLayout.NORTH, textField, 0,
                SpringLayout.NORTH, topicLabel);
        springLayout.putConstraint(SpringLayout.EAST, textField, Spring.minus(st),
                SpringLayout.EAST, container);
        container.add(contentLabel);
        springLayout.putConstraint(SpringLayout.WEST, contentLabel, 0,
                SpringLayout.WEST, topicLabel);
        springLayout.putConstraint(SpringLayout.NORTH, contentLabel, st,
                SpringLayout.SOUTH, topicLabel);
        container.add(scrollPane);
        springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 0,
                SpringLayout.NORTH, contentLabel);
        springLayout.putConstraint(SpringLayout.WEST, scrollPane, 0,
                SpringLayout.WEST, textField);
        springLayout.putConstraint(SpringLayout.EAST, scrollPane, Spring.minus(st),
                SpringLayout.EAST, container);
        container.add(confirmButton);
        springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, Spring.minus(st),
                SpringLayout.NORTH, confirmButton);
        springLayout.putConstraint(SpringLayout.EAST, confirmButton, Spring.minus(st),
                SpringLayout.EAST, container);
        springLayout.putConstraint(SpringLayout.SOUTH, confirmButton, Spring.minus(st),
                SpringLayout.SOUTH, container);
        container.add(cancelButton);
        springLayout.putConstraint(SpringLayout.EAST, cancelButton, Spring.minus(st),
                SpringLayout.WEST, confirmButton);
        springLayout.putConstraint(SpringLayout.NORTH, cancelButton, 0,
                SpringLayout.NORTH, confirmButton);
        
        // make the text field focused every time the window is activated
        addWindowFocusListener(new WindowAdapter() {
 
            @Override
            public void windowGainedFocus(WindowEvent e) {
                // TODO Auto-generated method stub
                textField.requestFocus();
            }
 
        });
    }
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringLayout_3 frame = new SpringLayout_3();
        frame.setTitle("使用弹簧布局管理器");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 600, 300);
    }
 
}

总结

以上就是弹簧布局管理器的使用方法及实例,喜欢的同学快去试试效果吧。

感谢大家对服务器之家的支持!

原文链接:http://www.xuebuyuan.com/2007237.html