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

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

程序员文章站 2024-02-27 13:22:57
        由 springlayout 类实现的布局管理器称为弹簧布局管理器。利用该布局管理器管...

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

}

总结

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

感谢大家对的支持!