JAVA——45.flowlayout和gridlayout布局

本文介绍Java Swing中的两种常见布局管理器:FlowLayout和GridLayout,并演示如何使用这两种布局来组织GUI组件,包括设置组件间的间距。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习一、flowlayout布局(JPanel的默认布局就是流式布局)
FlowLayout:流式布局(顺着放下来)比如按钮一个接一个的放在面板上,当然是居中显示,一行放满了放到下一行

package org.zhaiyujia.test1;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GuiTest1 extends JFrame {
          JButton b1,b2,b3,b4,b5;
          JPanel p;
          public GuiTest1() {
          //FlowLayout:流式布局(顺着放下来)比如按钮一个接一个的放在面板上,当然是居中显示,一行放满了放到下一行
          b1=new JButton("Button1");
          b2=new JButton("Button2");
          b3=new JButton("Button3");
          b4=new JButton("Button4");
          b5=new JButton("Button5");
          p=new JPanel();
          this.getContentPane().add(p);//获取窗口对象的内容面板对象,再添加p
          FlowLayout layout=new FlowLayout();
          p.setLayout(layout);
          p.add(b1);
          p.add(b2);
          p.add(b3);
          p.add(b4);
          p.add(b5);
          this.setSize(300, 300);
          this.setVisible(true);
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
          public static void main(String[] args) {
              new GuiTest1();
          }
}

这里写图片描述
练习二、GridLayout:网格布局,几行几列的模式
构造方法:GridLayout(int rows, int cols) :创建具有指定行数和列数的网格布局。

package org.zhaiyujia.test1;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GuiTest1 extends JFrame {
          JButton b1,b2,b3,b4,b5;
          JPanel p;
          public GuiTest1() {
          //FlowLayout:流式布局(顺着放下来)比如按钮一个接一个的放在面板上,当然是居中显示,一行放满了放到下一行
          b1=new JButton("Button1");
          b2=new JButton("Button2");
          b3=new JButton("Button3");
          b4=new JButton("Button4");
          b5=new JButton("Button5");
          p=new JPanel();
          this.getContentPane().add(p);//获取窗口对象的内容面板对象,再添加p
          //FlowLayout layout=new FlowLayout();
          //p.setLayout(layout);

          //GridLayout:网格布局
          GridLayout layout=new GridLayout(2,3);//两行三列
          p.setLayout(layout);
          p.add(b1);
          p.add(b2);
          p.add(b3);
          p.add(b4);
          p.add(b5);
          this.setSize(300, 300);
          this.setVisible(true);
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
          public static void main(String[] args) {
              new GuiTest1();
          }
}

这里写图片描述
练习三、如何让按钮不充满整个网格空间
setHgap(int hgap) :将组件之间的水平间距设置为指定的值。
setVgap(int vgap) :将组件之间的垂直间距设置为指定值。

package org.zhaiyujia.test1;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GuiTest1 extends JFrame {
          JButton b1,b2,b3,b4,b5;
          JPanel p;
          public GuiTest1() {
          //FlowLayout:流式布局(顺着放下来)比如按钮一个接一个的放在面板上,当然是居中显示,一行放满了放到下一行
          b1=new JButton("Button1");
          b2=new JButton("Button2");
          b3=new JButton("Button3");
          b4=new JButton("Button4");
          b5=new JButton("Button5");
          p=new JPanel();
          this.getContentPane().add(p);//获取窗口对象的内容面板对象,再添加p
          //FlowLayout layout=new FlowLayout();
          //p.setLayout(layout);

          //GridLayout:网格布局
          GridLayout layout=new GridLayout(2,3);//两行三列
          p.setLayout(layout);
          layout.setHgap(10);
          layout.setVgap(10);
          p.add(b1);
          p.add(b2);
          p.add(b3);
          p.add(b4);
          p.add(b5);
          this.setSize(300, 300);
          this.setVisible(true);
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
          public static void main(String[] args) {
              new GuiTest1();
          }
}

这里写图片描述
为了更美观,我们可以在面板上叠加面板,就比如第一个面板设置一种布局,比如说两行三列的面板布局,每一个单元格我都放一个面板,然后再在这个子面板上继续设置布局放其他组件。
【java如何设置布局】1.得到布局类的对象2.通过调用容器类的组件setlayout方法将布局对象绑定到这个容器组件3.最后,添加组件即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值