歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java使用箱式布局管理器

Java使用箱式布局管理器

日期:2017/3/1 10:16:10   编辑:Linux編程

Java使用箱式布局管理器:

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