package guiTemp;
/**************************************************************
用处:此种布局下,容器将组件按照添加顺序从左向右放置,达到边界时会自动将组件放到下一行的位置
构造方法:
FlowLayout(int align,int hgap,int vgap)--指定组件的对齐方式和水平、垂直间距
当其中的参数都没有时,默认组件居中对齐,水平,垂直间距默认5个单位
参数align决定组件在每行中相对于容器边界的对齐方式,分别为左对齐、右对齐、居中对齐,使用 FlowLayout.LEFT,FlowLayout.RIGHT,FlowLayout.CENTER
参数hgap,vgap分别设定组件之间的水平和垂直间距
***************************************************************/
import java.awt.*;
import javax.swing.*;
public class FlowLayoutTemp {
//创建creatshowGUI方法
public static void creatshowGUI() {
//创建一个名为FlowLayout的窗体
JFrame f=new JFrame("FlowLayout");
//设置组件左对齐,水平间距为20,垂直间距为30
f.setLayout(new FlowLayout(FlowLayout.LEFT,20,30));
f.setSize(400, 200); //设置窗体大小
f.setLocation(300, 200); //设置窗体显示的位置
//向容器中添加组件
f.add(new JButton("第1个按钮"));
f.add(new JButton("第2个按钮"));
f.add(new JButton("第3个按钮"));
f.add(new JButton("第4个按钮"));
f.add(new JButton("第5个按钮"));
f.setVisible(true); //设置窗体可见
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//使用SwingUtilities工具类调用creatshowGUI方法并显示GUI
SwingUtilities.invokeLater(FlowLayoutTemp::creatshowGUI);
}
}```
java中creatshowGUI方法实现窗口按钮方法
最新推荐文章于 2024-04-06 20:56:43 发布