Linux教程網
Java使用彈簧和支柱:
- package com.han;
-
- import java.awt.Container;
-
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.Spring;
- import javax.swing.SpringLayout;
-
- /**
- * 使用彈簧和支柱
- * @author HAN
- *
- */
- public class SpringLayout_1 extends JFrame {
-
- /**
- *
- */
- private static final long serialVersionUID = -5240773865528158419L;
-
- public SpringLayout_1() {
- // 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, 30, 40); // the values are not very significative
-
- 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);
- }
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- SpringLayout_1 frame = new SpringLayout_1();
- frame.setTitle("使用彈簧和支柱");
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setBounds(100, 100, 600, 100);
- }
-
- }
Copyright ©
Linux教程網 All Rights Reserved