首页 >Java >java教程 >Java弹簧布局管理器使用教程

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

巴扎黑
巴扎黑原创
2017-09-06 09:38:061799浏览

这篇文章主要介绍了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弹簧布局管理器使用教程的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn