Java GUI面板的布局方式

70 篇文章 3 订阅
7 篇文章 1 订阅

有四种布局方式:

  • FlowLayout  
    //流式布局,即一种横向的布局,以行为基础,逐个进行组件的排列,当一行排列满时,自动排列到下一行

    流式布局示例:

    package javaGUI;
    ​
    import javax.swing.*;
    import java.awt.*;
    ​
    public class JFrameDemo2_1 extends JFrame {
    ​
        public JFrameDemo2_1(){
    ​
            this.setSize(500,300);
            this.setLocationRelativeTo(null);
            this.setTitle("登录界面");
            this.setIconImage(new ImageIcon("微信图片_20211117191028.jpg").getImage());
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setResizable(false);
    ​
            JPanel jp = new JPanel();   //使用JPanel创建一个面板
    ​
            jp.setLayout(new FlowLayout(FlowLayout.LEFT));  //创建一个匿名流式布局的对象
            JButton jButton1 = new JButton("按钮1");   //使用JButton创建一个按钮对象
            JButton jButton2 = new JButton("按钮2");
            JButton jButton3 = new JButton("按钮3");
            JButton jButton4 = new JButton("按钮4");
            jp.add(jButton1);
            jp.add(jButton2);
            jp.add(jButton3);
            jp.add(jButton4);
            this.add(jp);
            this.setVisible(true);
    ​
        }
        public static void main(String[] args) {
    ​
           JFrame jFrame = new JFrameDemo2_1();
           
        }
    }
    ​

    效果图如下:

  • BorderLayout   //边界布局,一种以东南西北和中间为布局的方式

    边界布局示例:

    package javaGUI;
    ​
    import javax.swing.*;
    import javax.swing.border.Border;
    import java.awt.*;
    ​
    public class JFrameDemo2_2 extends JFrame {
    ​
        public JFrameDemo2_2(){
    ​
            this.setSize(500,300);
            this.setLocationRelativeTo(null);
            this.setTitle("登录界面");
            this.setIconImage(new ImageIcon("微信图片_20211117191028.jpg").getImage());
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setResizable(false);
    ​
            JPanel jp = new JPanel();
            jp.setLayout(new BorderLayout());   //使用BorderLayout()创建一个边界布局方式的匿名对象
            JButton jButton1 = new JButton("按钮1");
            JButton jButton2 = new JButton("按钮2");
            JButton jButton3 = new JButton("按钮3");
            JButton jButton4 = new JButton("按钮4");
            JButton jButton5 = new JButton("按钮5");
            jp.add(jButton1, BorderLayout.NORTH);   //分别设置每一个按钮的位置
            jp.add(jButton2, BorderLayout.EAST);
            jp.add(jButton3, BorderLayout.SOUTH);
            jp.add(jButton4, BorderLayout.WEST);
            jp.add(jButton5, BorderLayout.CENTER);
    ​
            this.add(jp);
            this.setVisible(true);
        }
        public static void main(String[] args) {
    ​
           JFrame jFrame = new JFrameDemo2_2();
        }
    }
    ​

    效果图如下:

  • GridLayout    //网格布局,一种以网格形式布局的方式

    网格布局示例:

    package javaGUI;
    ​
    import javax.swing.*;
    import java.awt.*;
    ​
    public class JFrameDemo2_3 extends JFrame {
    ​
        public JFrameDemo2_3(){
    ​
            this.setSize(500,300);
            this.setLocationRelativeTo(null);
            this.setTitle("登录界面");
            this.setIconImage(new ImageIcon("微信图片_20211117191028.jpg").getImage());
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setResizable(false);
    ​
            JPanel jp = new JPanel(new GridLayout(2,2));   //使用GridLayout(),创建一个网格布局的方式,括号中为所需的行列数
            JButton jButton1 = new JButton("按钮1");
            JButton jButton2 = new JButton("按钮2");
            JButton jButton3 = new JButton("按钮3");
            JButton jButton4 = new JButton("按钮4");
            jp.add(jButton1);
            jp.add(jButton2);
            jp.add(jButton3);
            jp.add(jButton4);
            this.add(jp);
            this.setVisible(true);
        }
        public static void main(String[] args) {
    ​
           JFrame jFrame = new JFrameDemo2_3();
        }
    }
    ​

    效果图如下:

  • setLayout(null);   //自定义位置,即自己定义组件的位置

    自定义布局示例:

    package javaGUI;
    ​
    import javax.swing.*;
    import java.awt.*;
    ​
    public class JFrameDemo2_4 extends JFrame {
    ​
        public JFrameDemo2_4(){
    ​
            this.setSize(500,300);
            this.setLocationRelativeTo(null);
            this.setTitle("登录界面");
            this.setIconImage(new ImageIcon("微信图片_20211117191028.jpg").getImage());
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setResizable(false);
            this.setLayout(null);
            
            //自定义布局方式,需要使用原始的窗口进行布局
            Container container = this.getContentPane();
            JButton jButton2 = new JButton("按钮2");
            //两面两行即为原始窗口的创建方式
            jButton2.setLocation(100,50);
            jButton2.setSize(80,50);
            container.add(jButton2);
            this.setVisible(true);
        }
        public static void main(String[] args) {
    ​
           JFrame jFrame = new JFrameDemo2_4();
        }
    }
    ​

    效果图如下:

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会写代码的菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值