Java利用弹簧控制组件大小

package com.han;

import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.SpringLayout.Constraints;  // Constraints为SpringLayout的内部类

/**
 * 利用弹簧控制组件大小
 * @author HAN
 *
 */
public class SpringLayout_2 extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5240773865528158419L;

	public SpringLayout_2() {
		// TODO Auto-generated constructor stub
		Container container = getContentPane();
		SpringLayout springLayout = new SpringLayout();
		container.setLayout(springLayout);
		JButton buttonL = new JButton("buttonL");
		JButton buttonR = new JButton("buttonR");
		container.add(buttonL);
		container.add(buttonR);
		Spring vST = Spring.constant(5);
		Spring hSP = Spring.constant(10, 60, 100);
		
		springLayout.putConstraint(SpringLayout.NORTH, buttonR, 0, 
				SpringLayout.NORTH, buttonL);
		springLayout.putConstraint(SpringLayout.NORTH, buttonL, vST, 
				SpringLayout.NORTH, container);
		springLayout.putConstraint(SpringLayout.WEST, buttonL, hSP, 
				SpringLayout.WEST, container);
		springLayout.putConstraint(SpringLayout.EAST, container, hSP, 
				SpringLayout.EAST, buttonR);
		springLayout.putConstraint(SpringLayout.WEST, buttonR, Spring.scale(hSP, 2), 
				SpringLayout.EAST, buttonL);
		
		Spring heightST = Spring.constant(60);
		Spring widthSP = Spring.constant(60, 300, 600); 
		Constraints buttonLConstraints = springLayout.getConstraints(buttonL);
		buttonLConstraints.setHeight(heightST);
		buttonLConstraints.setWidth(widthSP);
		Constraints buttonRConstraints = springLayout.getConstraints(buttonR);
		buttonRConstraints.setHeight(heightST);
		buttonRConstraints.setWidth(widthSP);
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringLayout_2 frame = new SpringLayout_2();
		frame.setTitle("利用弹簧控制组件大小");
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(100, 100, 600, 300);
	}

}


你可能感兴趣的:(Java利用弹簧控制组件大小)