欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  Java

Java弹簧布局管理器使用教程

程序员文章站 2022-04-22 17:02:00
...
这篇文章主要介绍了Java弹簧布局管理器使用方法详解,需要的朋友可以参考下

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

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

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

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

e1:被参考的组件对象;

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

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

有点绕,其实很好理解。

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

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


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);
	}

}

以上就是Java弹簧布局管理器使用教程的详细内容,更多请关注其它相关文章!