歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java使用彈簧布局管理器

Java使用彈簧布局管理器:

  1. package com.han;  
  2.   
  3. import java.awt.Container;  
  4. import java.awt.event.WindowAdapter;  
  5. import java.awt.event.WindowEvent;  
  6.   
  7. import javax.swing.JButton;  
  8. import javax.swing.JFrame;  
  9. import javax.swing.JLabel;  
  10. import javax.swing.JScrollPane;  
  11. import javax.swing.JTextArea;  
  12. import javax.swing.JTextField;  
  13. import javax.swing.Spring;  
  14. import javax.swing.SpringLayout;  
  15.   
  16. /** 
  17.  * 使用彈簧布局管理器 
  18.  * @author HAN 
  19.  * 
  20.  */  
  21. public class SpringLayout_3 extends JFrame {  
  22.   
  23.     /** 
  24.      *  
  25.      */  
  26.     private static final long serialVersionUID = -6045918631932051025L;  
  27.   
  28.     public SpringLayout_3() {  
  29.         // TODO Auto-generated constructor stub   
  30.         Container container = getContentPane();  
  31.         SpringLayout springLayout = new SpringLayout();  
  32.         container.setLayout(springLayout);  
  33.         JLabel topicLabel = new JLabel("topic :");  
  34.         JLabel contentLabel = new JLabel("content :");  
  35.         final JTextField textField = new JTextField(30);  
  36.         JTextArea textArea = new JTextArea(330);  
  37.         JScrollPane scrollPane = new JScrollPane();  
  38.         scrollPane.setViewportView(textArea);  
  39.         textArea.setLineWrap(true);  
  40.         final JButton confirmButton = new JButton("confirm");  
  41.         JButton cancelButton = new JButton("cancel");  
  42.         Spring st = Spring.constant(10);  
  43.         Spring st2 = Spring.constant(30);  
  44.         container.add(topicLabel);  
  45.         springLayout.putConstraint(SpringLayout.NORTH, topicLabel, st,   
  46.                 SpringLayout.NORTH, container);  
  47.         springLayout.putConstraint(SpringLayout.WEST, topicLabel, st,   
  48.                 SpringLayout.WEST, container);  
  49.         container.add(textField);  
  50.         springLayout.putConstraint(SpringLayout.WEST, textField, st2,   
  51.                 SpringLayout.EAST, topicLabel);  
  52.         springLayout.putConstraint(SpringLayout.NORTH, textField, 0,   
  53.                 SpringLayout.NORTH, topicLabel);  
  54.         springLayout.putConstraint(SpringLayout.EAST, textField, Spring.minus(st),   
  55.                 SpringLayout.EAST, container);  
  56.         container.add(contentLabel);  
  57.         springLayout.putConstraint(SpringLayout.WEST, contentLabel, 0,   
  58.                 SpringLayout.WEST, topicLabel);  
  59.         springLayout.putConstraint(SpringLayout.NORTH, contentLabel, st,   
  60.                 SpringLayout.SOUTH, topicLabel);  
  61.         container.add(scrollPane);  
  62.         springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 0,   
  63.                 SpringLayout.NORTH, contentLabel);  
  64.         springLayout.putConstraint(SpringLayout.WEST, scrollPane, 0,   
  65.                 SpringLayout.WEST, textField);  
  66.         springLayout.putConstraint(SpringLayout.EAST, scrollPane, Spring.minus(st),   
  67.                 SpringLayout.EAST, container);  
  68.         container.add(confirmButton);  
  69.         springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, Spring.minus(st),   
  70.                 SpringLayout.NORTH, confirmButton);  
  71.         springLayout.putConstraint(SpringLayout.EAST, confirmButton, Spring.minus(st),   
  72.                 SpringLayout.EAST, container);  
  73.         springLayout.putConstraint(SpringLayout.SOUTH, confirmButton, Spring.minus(st),   
  74.                 SpringLayout.SOUTH, container);  
  75.         container.add(cancelButton);  
  76.         springLayout.putConstraint(SpringLayout.EAST, cancelButton, Spring.minus(st),   
  77.                 SpringLayout.WEST, confirmButton);  
  78.         springLayout.putConstraint(SpringLayout.NORTH, cancelButton, 0,   
  79.                 SpringLayout.NORTH, confirmButton);   
  80.           
  81.         // make the text field focused every time the window is activated   
  82.         addWindowFocusListener(new WindowAdapter() {  
  83.   
  84.             @Override  
  85.             public void windowGainedFocus(WindowEvent e) {  
  86.                 // TODO Auto-generated method stub   
  87.                 textField.requestFocus();  
  88.             }  
  89.   
  90.         });  
  91.     }  
  92.   
  93.     /** 
  94.      * @param args 
  95.      */  
  96.     public static void main(String[] args) {  
  97.         // TODO Auto-generated method stub   
  98.         SpringLayout_3 frame = new SpringLayout_3();  
  99.         frame.setTitle("使用彈簧布局管理器");  
  100.         frame.setVisible(true);  
  101.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  102.         frame.setBounds(100100600300);  
  103.     }  
  104.   
  105. }  
Copyright © Linux教程網 All Rights Reserved