Java使用箱式布局管理器


Java使用箱式布局管理器:

  1. package com.han;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Container;  
  5. import javax.swing.Box;  
  6. import javax.swing.JButton;  
  7. import javax.swing.JFrame;  
  8. import javax.swing.JLabel;  
  9. import javax.swing.JScrollPane;  
  10. import javax.swing.JTextArea;  
  11. import javax.swing.JTextField;  
  12.   
  13. /** 
  14.  * 使用箱式布局管理器 
  15.  * @author HAN 
  16.  * 
  17.  */  
  18. public class BoxLayout_1 extends JFrame {  
  19.   
  20.     /** 
  21.      *  
  22.      */  
  23.     private static final long serialVersionUID = 6896925750596855002L;  
  24.   
  25.     public BoxLayout_1() {  
  26.         // TODO Auto-generated constructor stub   
  27.         Container container = getContentPane();  
  28.         Box box = Box.createVerticalBox();  
  29.         container.add(box, BorderLayout.NORTH);  
  30.         box.add(Box.createVerticalStrut(5));  
  31.         Box topicBox = Box.createHorizontalBox();  
  32.         box.add(topicBox);  
  33.         topicBox.setAlignmentX(1);  
  34.         topicBox.add(Box.createHorizontalStrut(5));  
  35.         JLabel topicLabel = new JLabel("主题:");  
  36.         topicBox.add(topicLabel);  
  37.         topicBox.add(Box.createHorizontalStrut(5));  
  38.         JTextField topicTextField = new JTextField(30);  
  39.         topicBox.add(topicTextField);  
  40.           
  41.         Box box2 = Box.createVerticalBox();  
  42.         container.add(box2, BorderLayout.CENTER);  
  43.         Box contentBox = Box.createHorizontalBox();  
  44.         contentBox.setAlignmentX(1);  
  45.         box2.add(Box.createVerticalStrut(5));  
  46.         box2.add(contentBox);  
  47.         contentBox.add(Box.createHorizontalStrut(5));  
  48.         JLabel contentLabel = new JLabel("内容:");  
  49.         contentLabel.setAlignmentY(0);  
  50.         contentBox.add(contentLabel);  
  51.         contentBox.add(Box.createHorizontalStrut(5));  
  52.         StringBuilder stringBuilder = new StringBuilder();  
  53.         String contentString = new String("利用箱式布局管理器实现组件的右对齐" +  
  54.                 "和上对齐,以及控制组件之间的间距!");  
  55.         stringBuilder.append(contentString);  
  56.         stringBuilder.append("\n");  
  57.         stringBuilder.append(contentString);  
  58.         contentString = stringBuilder.toString();  
  59.         JTextArea contentTextArea = new JTextArea(contentString, 330);  
  60.         contentTextArea.setLineWrap(true);  
  61.         JScrollPane scrollPane = new JScrollPane();  
  62.         scrollPane.setAlignmentY(0);  
  63.         scrollPane.setViewportView(contentTextArea);  
  64.         contentBox.add(scrollPane);  
  65.         contentBox.add(Box.createHorizontalStrut(5));  
  66. //      System.out.println(contentTextArea.requestFocusInWindow());   
  67.         box2.add(Box.createVerticalStrut(5));  
  68.         JButton submitButton = new JButton("确定");  
  69.         box2.add(submitButton);  
  70.         submitButton.setAlignmentX(1);  
  71.         box2.add(Box.createVerticalStrut(5));  
  72.     }  
  73.   
  74.     /** 
  75.      * @param args 
  76.      */  
  77.     public static void main(String[] args) {  
  78.         // TODO Auto-generated method stub   
  79.         BoxLayout_1 frame = new BoxLayout_1();  
  80.         frame.setTitle("使用箱式布局管理器");  
  81.         frame.setVisible(true);  
  82.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  83.         frame.pack();  
  84.     }  
  85.   
  86. }  

相关内容