首頁  >  文章  >  Java  >  Java彈簧佈局管理器使用教學課程

Java彈簧佈局管理器使用教學課程

巴扎黑
巴扎黑原創
2017-09-06 09:38:061782瀏覽

這篇文章主要介紹了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