Java spring layout manager usage details

  • 2020-10-23 20:06:33
  • OfStack

The layout manager implemented by the SpringLayout class is called the spring layout manager. Using this layout manager to manage components, when changing the size of the form, it can automatically adjust the size of components without changing the relative position between components, so that components still cover the whole form, thus ensuring the overall effect of the form. Here is an example of how the spring layout manager is used and how it works.

The spring layout manager takes the edge of container and component as the operating object, and realizes the management of component layout by establishing constraints for components and container edges as well as components and component edges. By means of putConstraint(String e1,Conponet c1,int pad,String e2,Componet c2), constraints can be established for each side. The inlet parameters of the method are explained as follows:

c1: Component objects that need to be referenced;

c2: The specific edges of the component object to be referenced;

e1: Component objects to be referenced;

e2: The specific referenced edge of the referenced component object;

pad: The distance between two edges, that is, the space between two components.

It's a little convoluted, but it makes sense.

Example 1: Using the spring layout manager.

This example USES the spring layout manager to implement the form. After resizing the form, components will still cover the entire form, and the relative positions between components will not change. Source:


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(" The theme  :");
		JLabel contentLabel = new JLabel(" content  :");
		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(" cancel ");
		JButton cancelButton = new JButton(" confirm ");
		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(" Use the spring layout manager ");
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(100, 100, 600, 300);
	}

}

conclusion

Above is the spring layout manager of the use of methods and examples, like the students go to try the effect.

Thank you for your support!


Related articles: