0


Java程序设计——Swing UI 布局管理器(四)

布局管理器

布局是指组件在容器中的排列方式,主要有:

  1. FlowLayout 流式布局
  2. BorderLayout 边界布局
  3. GridLayout 网格布局
  4. CardLayout 卡片布局
  5. BoxLayout 盒式布局
  6. GridBagLayout 网格包布局
  7. null 空布局(不使用布局)

注:对于一些复杂的情况,往往需要使用容器的嵌套,各容器可使用不同的布局。当容器的尺寸改变时,布局管理器会自动调整组件的排列

4.1.FlowLayout

  • 该布局以行为单位依次排列各组件,一行排不下时,另起一行

  • JPanel的默认布局是FlowLayout
  • 构造方法

** FlowLayout();
FlowLayout(int align); //align一般取值有:CENTER、LEFT、RIGHT
FlowLayout(int align, int hgap, int vgap);//hgap和vgap指定组件与容器起始边界以及组件间的水平和垂直间距,默认值为5个像素**

  • 例如:FlowLayout layout = new FlowLayout(FlowLayout.LEFT, 10, 10);
  • 缺点:当用户对由FlowLayout布局管理的区域进行缩放时,布局发生变化

  • 该布局适用于组件个数较少的情况
  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class JFrame_MainClass {
  4. public static void main(String[] args) {
  5. new MyJFrame();
  6. }
  7. }
  8. class MyJFrame extends JFrame{
  9. private JPanel panel;
  10. private JButton button1,button2;
  11. public MyJFrame(){
  12. super("JPanel默认布局:FlowLayout");
  13. // panel = new JPanel(); // 默认居中对齐
  14. panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 设置左对齐
  15. button1 = new JButton("button1");
  16. button2 = new JButton("button2");
  17. panel.add(button1);
  18. panel.add(button2);
  19. this.add(panel);
  20. this.setSize(300,200);
  21. this.setLocation(200,100);
  22. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23. this.setVisible(true);
  24. }
  25. }


4.2.BorderLayout

  • 按照东、西、南、北、中5个方位排列各组件
  • 顶层容器JFrame、JApplet、JDialog、JWindow的默认布局都是BorderLayout

  • 构造方法:

BorderLayout( );
BorderLayout(int hgap,int vgap);//hgap和vgap指定组件间的水平和垂直间距,默认值为0个像素

  • 例如:

BorderLayout lay1 = new BorderLayout( );
BorderLayout lay2 = new BorderLayout(10, 10);

  • 方位的取值为:

BorderLayout.EAST 或 “East”
BorderLayout.WEST 或 “West”
BorderLayout.SOUTH 或 “South”
BorderLayout.NORTH 或 “North”
BorderLayout.CENTER 或 “Center”(默认)
设置容器布局:setLayout(方位);

  • 缺点:当加入的组件超过5个时,就必须使用容器的嵌套或其它布局。
  • 优点:当容器缩放时,组件相应的位置不变化,但大小改变。
  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class MainClass{
  4. public static void main(String[] args) {
  5. new BorderLayout_3();
  6. }
  7. }
  8. class BorderLayout_3 extends JFrame {
  9. public BorderLayout_3(){
  10. super("JFrame默认布局:BorderLayout");
  11. JPanel panel = new JPanel(new BorderLayout());
  12. JButton button1 = new JButton("按钮1");
  13. JButton button2 = new JButton("按钮2");
  14. JButton button3 = new JButton("按钮3");
  15. JButton button4 = new JButton("按钮4");
  16. JButton button5 = new JButton("按钮5");
  17. panel.add(button1,BorderLayout.EAST);
  18. panel.add(button2,BorderLayout.WEST);
  19. panel.add(button3,BorderLayout.SOUTH);
  20. panel.add(button4,BorderLayout.NORTH);
  21. panel.add(button5,BorderLayout.CENTER);
  22. this.add(panel);
  23. this.setSize(400,360);
  24. this.setLocation(300,200);
  25. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. this.setVisible(true);
  27. }
  28. }


4.3.GridLayout

  • 按照二维网格以相同大小依次排列各组件

  • 构造方法:

** GridLayout();//一行、每个组件一列
GridLayout(int rows,int cols);//行列数
GridLayout(int rows, int cols, int hgap, int vgap);//行行、列列的间距,默认值为0个像素**

  • 例如:

**** GridLayout lay1 = new GridLayout(3,3);
GridLayout lay2 = new GridLayout(5,2,10,10);

  • 优点:组件的相应位置不随区域的缩放而改变,只是组件的大小改变。

  • 该布局适用于组件个数较多的情况。
  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class MainClass{
  4. public static void main(String[] args) {
  5. new GridLayout_5();
  6. }
  7. }
  8. class GridLayout_5 extends JFrame {
  9. private JPanel panel;
  10. public GridLayout_5(){
  11. super("GridLayout布局");
  12. panel = new JPanel(new GridLayout(2,3));
  13. for (int i = 0; i < 6; i++) {
  14. panel.add(new JButton("按钮"+(i+1)));
  15. }
  16. this.add(panel);
  17. this.setSize(400,360);
  18. this.setLocation(300,200);
  19. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20. this.setVisible(true);
  21. }
  22. }


4.4.CardLayout

  • 该布局以一叠卡片的形式依次排列各组件

构造方法

  • CardLayout( );
  • CardLayout(int hgap,int vgap);//组件与容器边界间距,默认值为0个像素

例如:

  • ** CardLayout layout1 = new CardLayout();**
  • ** CardLayout layout2 = new CardLayout(10,10);**

显示方法****功能first(Container con)显示容器中的第一张卡片last(Container con)显示容器中的最后一张卡片previous(Container con)显示容器中当前卡片的上一张卡片next(Container con)显示容器中当前卡片的下一张卡片show(Container con, String name)显示容器中指定名称的卡片

  1. import javax.swing.*;
  2. import java.awt.*;
  3. class MainClass{
  4. public static void main(String[] args) {
  5. new CardLayout_4();
  6. }
  7. }
  8. class CardLayout_4 extends JFrame {
  9. private JPanel panel;
  10. private CardLayout cardLayout;
  11. public CardLayout_4(){
  12. super("CardLayout布局");
  13. panel = new JPanel();
  14. cardLayout = new CardLayout();
  15. panel.setLayout(cardLayout);
  16. for (int i = 0; i < 5; i++) {
  17. panel.add(new JButton("按钮"+(i+1)));
  18. }
  19. cardLayout.last(panel); // 显示最后一张卡片(按钮)
  20. this.add(panel);
  21. this.setSize(400,360);
  22. this.setLocation(300,200);
  23. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. this.setVisible(true);
  25. }
  26. }


4.5.BoxLayout

  • 以一行或一列的方式依次排列各组件

构造方法:

  • new BoxLayout(Container con, int axis):创建一个指定容器和方向的BoxLayout对象
    常量描述BorderLayout.X_AXISX轴(横轴)排列BorderLayout.Y_AXISY轴(纵轴)排列LINE_AXIS根据容器的Component Oriebtation属性,按照文字在一行中的排列方式布置组件PAGE_AXIS根据容器的Component Oriebtation属性,按照文本行在一页中的排列方式布置组件
    ```
    import javax.swing.*;

public class MainClass{
public static void main(String[] args) {
new BoxLayout_6();
}
}
class BoxLayout_6 extends JFrame {
private JPanel panel;
public BoxLayout_6(){
super("BoxLayout布局");
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 沿Y轴布置组件

  1. for (int i = 0; i < 5; i++) {
  2. panel.add(new JButton("按钮"+(i+1)));
  3. }
  4. this.add(panel);
  5. this.setSize(400,360);
  6. this.setLocation(300,200);
  7. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  8. this.setVisible(true);
  9. }

}

  1. ![](https://img-blog.csdnimg.cn/0fa91334f829481aadecaedad497bd86.png)
  2. ---
  3. ## 4.6.NULL
  4. - **不使用任何布局管理器**
  5. **空布局的使用:**
  6. - **创建容器对象:JPanel panel = new JPanel( );**
  7. - **设置容器对象的布局为nullpanel.setLayout(null);**
  8. - **设置组件在容器中的位置:组件对象.setBounds(x, y, width, height);**

import javax.swing.*;

public class MainClass{
public static void main(String[] args) {
new null_7();
}
}
class null_7 extends JFrame {
private Jpanel panel;
private JButton button1,button2,button3,button4,button5;
public null_7(){
super("null布局");
panel = new JPanel();
panel.setLayout(null);
button1 = new JButton("按钮1");
button2 = new JButton("按钮2");
button3 = new JButton("按钮3");
button4 = new JButton("按钮4");
button5 = new JButton("按钮5");

  1. button1.setBounds(35,25,75,30);
  2. button2.setBounds(135,25,75,30);
  3. button3.setBounds(235,25,75,30);
  4. button4.setBounds(35,65,75,30);
  5. button5.setBounds(135,65,75,30);
  6. panel.add(button1);
  7. panel.add(button2);
  8. panel.add(button3);
  9. panel.add(button4);
  10. panel.add(button5);
  11. this.add(panel);
  12. this.setSize(400,360);
  13. this.setLocation(300,200);
  14. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. this.setVisible(true);
  16. }

}

```


(27条消息) Swing UI——容器(一)_Stuttering Guy的博客-CSDN博客https://blog.csdn.net/Mr_Morgans/article/details/125109643?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125109643%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=l9JMT(27条消息) Swing UI——基本组件(二)_Stuttering Guy的博客-CSDN博客https://blog.csdn.net/Mr_Morgans/article/details/125110881?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125110881%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=Kzbcx(27条消息) Swing UI——高级组件(三)_Stuttering Guy的博客-CSDN博客https://blog.csdn.net/Mr_Morgans/article/details/125115383?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125115383%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=81Bbq(27条消息) Swing UI——事件处理(五)_Stuttering Guy的博客-CSDN博客https://blog.csdn.net/Mr_Morgans/article/details/125115417?spm=1001.2014.3001.5501

标签: linq wpf p2p

本文转载自: https://blog.csdn.net/Mr_Morgans/article/details/125115409
版权归原作者 来得晚一些也行 所有, 如有侵权,请联系我们删除。

“Java程序设计——Swing UI 布局管理器(四)”的评论:

还没有评论