使用布局管理器

  FlowLayout管理器

  面板的默认布局管理器是java.awt包中的FlowLayout类。使用FlowLayout时,像在页面中排列英文单词那样排组件:从左到右排列,当前行没有空间后进入下一行。

  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class Crisis extends JFrame {
  5. JButton panicButton;
  6. JButton dontPanicButton;
  7. JButton blameButton;
  8. JButton mediaButton;
  9. JButton saveButton;
  10.  
  11. public Crisis() {
  12. super("Crisis");
  13. setLookAndFeel();
  14. setSize(348, 128);
  15. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. // 1.默认布局
  17. FlowLayout flo = new FlowLayout();
  18. setLayout(flo);
  19. panicButton = new JButton("Panic");
  20. dontPanicButton = new JButton("Don't Panic");
  21. blameButton = new JButton("Blame Others");
  22. mediaButton = new JButton("Notify the Media");
  23. saveButton = new JButton("save yourself");
  24. add(panicButton);
  25. add(dontPanicButton);
  26. add(blameButton);
  27. add(mediaButton);
  28. add(saveButton);
  29. setVisible(true);
  30. }
  31.  
  32. private void setLookAndFeel() {
  33. try {
  34. UIManager.setLookAndFeel(
  35. "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  36. } catch (Exception exc) {
  37. // ignore error
  38. }
  39. }
  40.  
  41. public static void main(String[] arguments) {
  42. Crisis frame = new Crisis();
  43. }
  44. }

  GridLayout管理器

  GridLayout类位于java.awt包中,它将容器中所有的组件组织为指定的行数和列数。分配给每个组件的显示区域都相同。

  当组件加入到容器中时,GridLayout将所有的组件放置到网格中的某个位置,而且组件是从左到右依次添加,当这一行满了之后,在从下一行的最左边开始添加。

  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class Crisis extends JFrame {
  5. JButton panicButton;
  6. JButton dontPanicButton;
  7. JButton blameButton;
  8. JButton mediaButton;
  9. JButton saveButton;
  10.  
  11. public Crisis() {
  12. super("Crisis");
  13. setLookAndFeel();
  14. setSize(348, 128);
  15. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.  
  17. // 2.GridLayout布局
  18. GridLayout grid = new GridLayout(2, 3);
  19. setLayout(grid);
  20. panicButton = new JButton("Panic");
  21. dontPanicButton = new JButton("Don't Panic");
  22. blameButton = new JButton("Blame Others");
  23. mediaButton = new JButton("Notify the Media");
  24. saveButton = new JButton("save yourself");
  25. add(panicButton);
  26. add(dontPanicButton);
  27. add(blameButton);
  28. add(mediaButton);
  29. add(saveButton);
  30. setVisible(true);
  31. }
  32.  
  33. private void setLookAndFeel() {
  34. try {
  35. UIManager.setLookAndFeel(
  36. "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  37. } catch (Exception exc) {
  38. // ignore error
  39. }
  40. }
  41.  
  42. public static void main(String[] arguments) {
  43. Crisis frame = new Crisis();
  44. }
  45. }

BorderLay管理器

  BorderLayout类也位于java.awt包中,它将容器中的组件放置在特定的位置,该位置有5个方位:东、西、南、北、中。

BorderLayout管理器将组件放置到5个位置:其中4个位置由罗盘方向指定,另外一个由中心区域指定。当在该布局下添加组件时,add()方法会包含第2个参数,用于指示组件应该放置的位置。该参数应该是BorderLayout类的5个类变量之一:NORTH、SOUTH、EAST、WEST和CENTER。

  与GridLayout类相同,BorderLayout也会将所有可用空间都分配给组件。在周围放置4个边界组件后,余下的空间都分配给中央的组件,因此它通常是最大的。

  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class Crisis extends JFrame {
  5. JButton panicButton;
  6. JButton dontPanicButton;
  7. JButton blameButton;
  8. JButton mediaButton;
  9. JButton saveButton;
  10.  
  11. public Crisis() {
  12. super("Crisis");
  13. setLookAndFeel();
  14. setSize(348, 128);
  15. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. BorderLayout crisisLayout = new BorderLayout();
  17. setLayout(crisisLayout);
  18.  
  19. panicButton = new JButton("Panic");
  20. dontPanicButton = new JButton("Don't Panic");
  21. blameButton = new JButton("Blame Others");
  22. mediaButton = new JButton("Notify the Media");
  23. saveButton = new JButton("save yourself");
  24. add(panicButton, BorderLayout.NORTH);
  25. add(dontPanicButton, BorderLayout.SOUTH);
  26. add(blameButton, BorderLayout.EAST);
  27. add(mediaButton, BorderLayout.WEST);
  28. add(saveButton, BorderLayout.CENTER);
  29. setVisible(true);
  30. }
  31.  
  32. private void setLookAndFeel() {
  33. try {
  34. UIManager.setLookAndFeel(
  35. "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  36. } catch (Exception exc) {
  37. // ignore error
  38. }
  39. }
  40.  
  41. public static void main(String[] arguments) {
  42. Crisis frame = new Crisis();
  43. }
  44. }

BoxLayout管理器

  BoxLayout类位于javax.swing包中,它可以将组件排列成一行或一列。

  使用该布局时,先创建一个放置组件的面板,然后再创建一个布局管理器,它带有2个参数:

  1.以框式布局组织的组件;

  2.BoxLayout.Y_AXIS指定垂直排列,BoxLayout.X_AXIS指定水平排列;

  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class Crisis extends JFrame {
  5. JButton panicButton;
  6. JButton dontPanicButton;
  7. JButton blameButton;
  8. JButton mediaButton;
  9. JButton saveButton;
  10.  
  11. public Crisis() {
  12. super("Crisis");
  13. setLookAndFeel();
  14. setSize(348, 128);
  15. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. JPanel pane = new JPanel();
  17. BoxLayout box = new BoxLayout(pane, BoxLayout.Y_AXIS);
  18. pane.setLayout(box);
  19. panicButton = new JButton("Panic");
  20. dontPanicButton = new JButton("Don't Panic");
  21. blameButton = new JButton("Blame Others");
  22. mediaButton = new JButton("Notify the Media");
  23. saveButton = new JButton("save yourself");
  24. pane.add(panicButton);
  25. pane.add(dontPanicButton);
  26. pane.add(blameButton);
  27. pane.add(mediaButton);
  28. pane.add(saveButton);
  29. add(pane);
  30.  
  31. setVisible(true);
  32. }
  33.  
  34. private void setLookAndFeel() {
  35. try {
  36. UIManager.setLookAndFeel(
  37. "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  38. } catch (Exception exc) {
  39. // ignore error
  40. }
  41. }
  42.  
  43. public static void main(String[] arguments) {
  44. Crisis frame = new Crisis();
  45. }
  46. }

使用Insets将组件隔开

  Insets类位于java.awt包中,它有一个接受4个参数的构造函数:在容器上、下、左、右留出的空间。每个参数都以像素为单位,像素是定义框架大小时使用的度量单位。

  1. public Insets getInsets() {
  2. Insets squeeze = new Insets(60, 15, 10, 15);
  3. return squeeze;
  4. }

 布局测试

  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class LottoMadness extends JFrame {
  5. // set up row1
  6. JPanel row1 = new JPanel();
  7. ButtonGroup option = new ButtonGroup();
  8. JCheckBox quickpick = new JCheckBox("Quick Pick", false);
  9. JCheckBox personal = new JCheckBox("Personal", true);
  10. // set up row2
  11. JPanel row2 = new JPanel();
  12. JLabel numbersLabel = new JLabel("Your picks: ", JLabel.RIGHT);
  13. JTextField[] numbers = new JTextField[6];
  14. JLabel winnersLabel = new JLabel("Winners: ", JLabel.RIGHT);
  15. JTextField[] winners = new JTextField[6];
  16. // set up row3
  17. JPanel row3 = new JPanel();
  18. JButton stop = new JButton("Stop");
  19. JButton play = new JButton("play");
  20. JButton reset = new JButton("Reset");
  21. // set up row4
  22. JPanel row4 = new JPanel();
  23. JLabel got3Label = new JLabel("3 of 6: ", JLabel.RIGHT);
  24. JTextField got3 = new JTextField("0");
  25. JLabel got4Label = new JLabel("4 of 6: ", JLabel.RIGHT);
  26. JTextField got4 = new JTextField("0");
  27. JLabel got5Label = new JLabel("5 of 6: ", JLabel.RIGHT);
  28. JTextField got5 = new JTextField("0");
  29. JLabel got6Label = new JLabel("6 of 6: ", JLabel.RIGHT);
  30. JTextField got6 = new JTextField("0");
  31. JLabel drawingsLabel = new JLabel("Drawings", JLabel.RIGHT);
  32. JTextField drawings = new JTextField("0");
  33. JLabel yearsLabel = new JLabel("Years: ", JLabel.RIGHT);
  34. JTextField years = new JTextField();
  35.  
  36. public LottoMadness() {
  37. super("Lotto Madness");
  38.  
  39. setSize(550, 400);
  40. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41. GridLayout layout = new GridLayout(5, 1, 10, 10);
  42. setLayout(layout);
  43.  
  44. FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
  45. option.add(quickpick);
  46. option.add(personal);
  47. row1.setLayout(layout1);
  48. row1.add(quickpick);
  49. row1.add(personal);
  50. add(row1);
  51.  
  52. GridLayout layout2 = new GridLayout(2, 7, 10, 10);
  53. row2.setLayout(layout2);
  54. row2.add(numbersLabel);
  55. for (int i = 0; i < 6; i++) {
  56. numbers[i] = new JTextField();
  57. row2.add(numbers[i]);
  58. }
  59. row2.add(winnersLabel);
  60. for (int i = 0; i < 6; i++) {
  61. winners[i] = new JTextField();
  62. winners[i].setEditable(false);
  63. row2.add(winners[i]);
  64. }
  65. add(row2);
  66.  
  67. FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10, 10);
  68. row3.setLayout(layout3);
  69. stop.setEnabled(false);
  70. row3.add(stop);
  71. row3.add(play);
  72. row3.add(reset);
  73. add(row3);
  74.  
  75. GridLayout layout4 = new GridLayout(2, 3, 20, 10);
  76. row4.setLayout(layout4);
  77. row4.add(got3Label);
  78. got3.setEditable(false);
  79. row4.add(got3);
  80. row4.add(got4Label);
  81. got4.setEditable(false);
  82. row4.add(got4);
  83. row4.add(got5Label);
  84. got5.setEditable(false);
  85. row4.add(got5);
  86. row4.add(got6Label);
  87. got6.setEditable(false);
  88. row4.add(got6);
  89. row4.add(drawingsLabel);
  90. drawings.setEditable(false);
  91. row4.add(drawings);
  92. row4.add(yearsLabel);
  93. years.setEditable(false);
  94. row4.add(years);
  95. add(row4);
  96.  
  97. setVisible(true);
  98. }
  99.  
  100. private static void setLookAndFeel() {
  101. try {
  102. UIManager.setLookAndFeel(
  103. "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  104. } catch (Exception exc) {
  105. // ignore error
  106. }
  107. }
  108.  
  109. public static void main(String[] arguments) {
  110. LottoMadness.setLookAndFeel();
  111. LottoMadness frame = new LottoMadness();
  112. }
  113. }

    

Java学习——用户界面的布局的更多相关文章

  1. java代码用户界面网格布局GridLayout.划分为格子区域

    总结:网格布局.很简单,首先要new一个   this.setlayout(new GriedLayout(3,5));里面是行数和列数 package clientFrame; //网格布局练习 i ...

  2. java图形用户界面BorderLayout布局。冲突

    总结:在使用边界布局发现,把所有的按钮组件都放入了panel.但是在中部的按钮组件找不到了.发现自己重复用了组件 1.this.add(bt4,BorderLayout.North); 2.panel ...

  3. java图形用户界面边界布局管理器

    总结:不同方向的组件,所用的板是不同的: package com.moc; //用布局写一个界面 ///运用边界布局 //2个按钮在北,2个按钮在南 //中央一个大按钮 //将同一方向的组件封装后布局 ...

  4. Java学习知识体系大纲梳理

    感悟 很奇怪,我怎么会想着写这么一篇博客——Java语言的学习体系,这不是大学就已经学过的课程嘛.博主系计算机科班毕业,大学的时候没少捧着Java教程来学习,不管是为了学习编程还是为了期末考个高分,都 ...

  5. Java学习笔记--Swing用户界面组件

    很多与AWT类似. 事件处理参考:Java学习笔记--AWT事件处理 1.设计模式: 模型:存储内容视图:显示内容控制器:处理用户输入· 2. 文本输入常用组件 2.1 文本域: JLabel lab ...

  6. Java学习笔记4

    Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...

  7. 201671010140. 2016-2017-2 《Java程序设计》java学习第十五周

    java学习第十五周 Java的GUI界面设计,框架以及主要部件填充,归置,布局管理,在第十一章和第十二章进行了系统的学习,在这两章的知识奠基下,可以简单的构造一个GUI用户界面,在两周的学习后,可以 ...

  8. 201671010140. 2016-2017-2 《Java程序设计》java学习第十四周

      java学习第十四周       本周,主要精力放在了第十二章swing用户界面组件知识的学习,swing是一个用于开发Java应用程序用户界面的开发工具包.它以抽象窗口工具包(AWT)为基础使跨 ...

  9. 第十二周Java学习总结

    学习总结: 本周主要学习了其他容器和事件处理 1.窗体事件(WindowListener)常用接口方法voidwindowActivated/windowDeactivated(WindowEvent ...

随机推荐

  1. java系列-JDBC的封装

    参考:http://blog.csdn.net/liuhenghui5201/article/details/16369773 一. 1.加载驱动-->>封装    --->> ...

  2. 批量转换引擎为innodb

    [root@HE1 ~]# catconvert_Storage_Engine.sh #/bin/bash #FileName:Convert_Storage_Engine.sh #Desc:Conv ...

  3. Usermod:user oracle is currently logged in 家目录不能改变解决方法

    [root@HE1 ~]# usermod -u 200 -g oinstall -G dba,asmdba,oper oracle[root@HE1 ~]# id oracleuid=200(ora ...

  4. redhat6.4下安装Oracle11g

    一.在Root用户下执行以下步骤: 1)修改用户的SHELL的限制,修改/etc/security/limits.conf文件 *               soft    nproc  2047 ...

  5. 如何估算网站日承受最大访问PV

    每个老板或客户都会问架构师这个问题. 你设计的网站能承受多大的日访问量. 程序员都会拍胸脯说出一个心理最大数字.说的时候很有信心.其实这个数字大多是猜的.作为一个理性的程序员怎么能用猜呢? 这里就介绍 ...

  6. 如何使excel表格的内容自动添加前缀

    一.假设是要在一列的单元格内容前加上固定的内容,则 方法一在原单元格实现,分两种情况 如果原单元格的内容是数字内容,要在原数字前添加"ABC"这样的前缀则选中这些单元格----右键 ...

  7. ABP入门系列(7)——分页实现

    ABP入门系列目录--学习Abp框架之实操演练 完成了任务清单的增删改查,咱们来讲一讲必不可少的的分页功能. 首先很庆幸ABP已经帮我们封装了分页实现,实在是贴心啊. 来来来,这一节咱们就来捋一捋如何 ...

  8. 使用Typescript来写javascript

    使用Typescript来写javascript 前几天尝试使用haxejs来写javascript,以获得静态类型带来的益处.虽然成功了,但很快发现将它与angularjs一起使用,有一些不太顺畅的 ...

  9. Professional C# 6 and .NET Core 1.0 - What’s New in C# 6

    本文为转载,学习研究 What's New in C# 6 With C# 6 a new C# compiler is available. It's not only that a source ...

  10. [CSS3] 学习笔记--CSS盒子模型

    1.CSS盒子模型概述 盒子模型的内容范围包括:margin(外边距).border(边框).padding(内边距).content(内容)部分组成. 2.内边距 内边距在content外,bord ...