ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java.Swing基础篇三部曲二(布局管理)

2021-12-06 21:30:52  阅读:198  来源: 互联网

标签:Java 三部曲 GridBagConstraints add Swing new jf JButton panel


FlowLayout布局管理器

布局特点

  1. 对组件逐行定位,行内从左到右,一行排满之后换行。
  2. 默认对齐方式为居中对齐
  3. 不改变组件的大小,按组件原有尺寸显示组件。
  4. FlowLayout是Panel类的默认布局管理器。

说明

可在构造方法中设置不同的组件间距,行距即对齐方式。

  • JPanel panel=new JPanel(new FlowLayout(FlowLayout.RIGHT,20,40));

右对齐,组件之间水平间距20个像素,数值间距40个像素

  • JPanel panel=new JPanel(new FlowLayout(FlowLayout.LEFT));

左对齐,水平和竖直间距为缺省值(默认值)5

  • JPanel panel=new JPanel(new FlowLayout());

默认方式,居中对齐,水平和竖直间距值均为默认值5

代码展示

import javax.swing.*;
import java.awt.*;

public class TestFlowLayout extends JFrame {
    /**首先定义8个按钮用来测试*/
    JButton[] jButtons=new JButton[8];
    /**关于布局的定义方法有很多,这里展示最常见的一种方法*/
    JPanel panel=new JPanel(new FlowLayout());
    public TestFlowLayout(){
        for (int i=0;i<jButtons.length;i++){
            jButtons[i]=new JButton("按钮"+i);
            panel.add(jButtons[i]);
        }
        this.add(panel);
        this.setTitle("流式布局测试");
        this.setSize(400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        }
       public static void main(String[] args){
        new TestFlowLayout();
       }
    }

 BorderLayout布局管理器

布局特点

  1. BorderLayout将整个容器布局划分成东南西北中,五个区域,组件智能被添加到指定的区域。
  2. 如不指定加入部位,则默认加入到Center区域。
  3. 每个区域只能加入一个,如果加入多个,则先前的组件会被遗弃。
  4. BorderLayout是Frame类的默认布局管理器。

说明

 代码展示



import javax.swing.*;
import java.awt.*;

public class TestBorderLayout {

    public static void main(String[] args) {
        JFrame jf = new JFrame("BorderLayout测试管理");
        jf.setSize(350, 250);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 创建内容面包容器,指定使用 边界布局
        JPanel panel = new JPanel(new java.awt.BorderLayout());
        JButton btnN = new JButton("North");
        JButton btnS = new JButton("South");
        JButton btnW = new JButton("West");
        JButton btnE = new JButton("East");
        JButton btnC = new JButton("Center");
        // 把 5 个按钮添加到容器中的 5 个方位,注意后面的一定要大写
        panel.add(btnN, java.awt.BorderLayout.NORTH);
        panel.add(btnS, java.awt.BorderLayout.SOUTH);
        panel.add(btnW, java.awt.BorderLayout.WEST);
        panel.add(btnE, java.awt.BorderLayout.EAST);
        panel.add(btnC, java.awt.BorderLayout.CENTER);
        jf.setContentPane(panel);
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
    }

}

GridLayout布局管理器

布局特点

  1. GridLayout型布局管理器将布局划分成规则的矩形网络,每一个单元格区域大小相等。
  2. 组件被添加到每一个单元格中,从左向右填满一行后换行,在从上往下。
  3. 在GridLayout构造方法中指定分割的行数和列数。

说明

 代码展示

import javax.swing.*;
import java.awt.*;

public class TestGridLayout extends JFrame {
    /**首先定义8个按钮用来测试*/
    JButton[] jButtons=new JButton[8];
    /**关于布局的定义方法有很多,这里展示最常见的一种方法*/
    JPanel panel=new JPanel(new GridLayout(2,4));
    public TestGridLayout(){
        for (int i=0;i<jButtons.length;i++){
            jButtons[i]=new JButton("按钮"+i);
            panel.add(jButtons[i]);
        }
        this.add(panel);
        this.setTitle("表格布局测试");
        this.setSize(400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    public static void main(String[] args){
        new TestGridLayout();
    }
}



其他布局管理说明

布局名称说明注意
CardLayout能够帮助用户处理两个以致更多的成员共享同一显示空间,就好像一叠卡片叠在一起。在一张卡片中只能显示一个组件,因此可以使用容器嵌套的方法显示多个组件
GridBagLayout最灵活,最复杂的布局管理器,各组件所占空间可以不相同且灵活规定
绝对布局流程:
  1. 对象.setLocation(x,y);
  2. 对象.setSize(80,80);(以上也可以用SetBounds同时设定四个参数)
  3. 容器.setLayout(null);//很重要,否则你会发现自己组件会显示不出来
  4. 容器.add(对象);

其他布局代码示例

CardLayout代码


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCardLayout {
    public static void main(String[] args) {
        JFrame jf = new JFrame("卡片布局");
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setSize(300, 200);
        // 创建卡片布局,卡片间水平和竖直间隔为 10
        final java.awt.CardLayout layout = new java.awt.CardLayout(10, 10);
        // 创建内容面板容器,指定布局管理器
        final JPanel panel = new JPanel(layout);
        JButton btn01 = new JButton("哈哈");
        JButton btn02 = new JButton("嘿嘿");
        JButton btn03 = new JButton("呼呼");
        panel.add(btn01, "btn01");
        panel.add(btn02, "btn02");
        panel.add(btn03, "btn03");
        // 先显示第二个
        layout.show(panel, "btn02");
        jf.setContentPane(panel);
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
        // 每间隔2秒切换显示下一个
        new Timer(2000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                layout.next(panel);
            }
        }).start();
    }

}

 

GridBagLayout布局代码



import javax.swing.*;
import java.awt.*;

public class GridBagLayout {

    public static void main(String[] args) {
        JFrame jf = new JFrame("测试窗口");
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        java.awt.GridBagLayout gridBag = new java.awt.GridBagLayout();
        GridBagConstraints c = null;                    // 约束
        JPanel panel = new JPanel(gridBag);
        JButton btn01 = new JButton("1");
        JButton btn02 = new JButton("2");
        JButton btn03 = new JButton("3");
        JButton btn04 = new JButton("4");
        JButton btn05 = new JButton("5");
        JButton btn06 = new JButton("6");
        JButton btn07 = new JButton("7");
        JButton btn08 = new JButton("8");
        JButton btn09 = new JButton("9");
        JButton btn10 = new JButton("0");
        /* 添加 组件 和 约束 到 布局管理器 */
        // Button01
        c = new GridBagConstraints();
        gridBag.addLayoutComponent(btn01, c); // 内部使用的仅是 c 的副本
        // Button02
        c = new GridBagConstraints();
        gridBag.addLayoutComponent(btn02, c);
        // Button03
        c = new GridBagConstraints();
        gridBag.addLayoutComponent(btn03, c);
        // Button04 显示区域占满当前行剩余空间(换行),组件填充显示区域
        c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.BOTH;
        gridBag.addLayoutComponent(btn04, c);
        // Button05 显示区域独占一行(换行),组件填充显示区域
        c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.BOTH;
        gridBag.addLayoutComponent(btn05, c);
        // Button06 显示区域占到当前尾倒车第二个单元格(下一个组件后需要手动换行),组件填充显示区域
        c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.fill = GridBagConstraints.BOTH;
        gridBag.addLayoutComponent(btn06, c);
        // Button07 放置在当前行最后一个单元格(换行)
        c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        gridBag.addLayoutComponent(btn07, c);
        // Button08 显示区域占两列,组件填充显示区域
        c = new GridBagConstraints();
        c.gridheight = 2;
        c.fill = GridBagConstraints.BOTH;
        gridBag.addLayoutComponent(btn08, c);
        // Button09 显示区域占满当前行剩余空间(换行),组件填充显示区域
        c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.BOTH;
        gridBag.addLayoutComponent(btn09, c);
        // Button10 显示区域占满当前行剩余空间(换行),组件填充显示区域
        c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.BOTH;
        gridBag.addLayoutComponent(btn10, c);
        /* 添加 组件 到 内容面板 */
        panel.add(btn01);
        panel.add(btn02);
        panel.add(btn03);
        panel.add(btn04);
        panel.add(btn05);
        panel.add(btn06);
        panel.add(btn07);
        panel.add(btn08);
        panel.add(btn09);
        panel.add(btn10);
        jf.setContentPane(panel);
        //自动调整大小
        jf.pack();
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
    }

}

 

标签:Java,三部曲,GridBagConstraints,add,Swing,new,jf,JButton,panel
来源: https://blog.csdn.net/qq_58510930/article/details/121754681

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有