Java可视化窗体FlowLayout

------------恢复内容开始------------

AWT组件

1.1Frame组件与Panel组件

1.1.1显示框架窗口

复制代码
 1 package awt;
 2 
 3 import java.awt.Color;
 4 import java.awt.Frame;
 5 
 6 public class TestFrame {
 7     
 8     public static void main(String[] args) {
 9         // TODO 自动生成的方法存根
10         
11         Frame f=new Frame ("My First Test!");       //通过构造方法指定窗口标题
12         
13         f.setLocation(500,500);                      //设置窗口显示在电脑屏幕的什么位置,像素坐标值
14         
15         f.setSize(300,200);                            //设置窗口大小
16         f.setBackground(Color.blue);                 //设置窗口颜色
17         f.setResizable(true);                        //设置窗口大小是否可变
18         f.setVisible(true);                            //设置窗口是否可见
19         
20     }
21 }
复制代码

1.1.2显示多个框架窗口

复制代码
 1 package awt;
 2 
 3 import java.awt.Color;
 4 import java.awt.Frame;
 5 
 6 public class MyFrame extends Frame {
 7         static int id=0;               //定义静态成员变量记录创建的MyFrame对象的个数
 8         
 9         /*
10          * 调用父类构造方法设置窗口标题等
11          * 
12          */
13         MyFrame(int x,int y,int w,int h,Color color){
14             super("Myframe"+(++id));
15             setBackground(color);
16             setBounds(x,y,w,h);
17             setVisible(true);
18         }
19         public static void main(String args[]) {
20             MyFrame f1= new MyFrame(100,100,300,100,Color.DARK_GRAY);
21             MyFrame f2= new MyFrame(100,200,300,100,Color.green);
22             MyFrame f3= new MyFrame(400,100,300,100,Color.BLUE);
23             MyFrame f4= new MyFrame(400,200,300,100,Color.BLACK);
24         }
25         
26             
27 }
28         
29 
30   
复制代码

 1.1.3在框架窗口中显示一个面板   (Frame中显示一个Panel)

复制代码
 1 package awt;
 2 
 3 import java.awt.Color;
 4 import java.awt.Frame;
 5 import java.awt.Panel;
 6 
 7 public class TestPanel {
 8 
 9     public static void main(String[] args) {
10         // TODO 自动生成的方法存根
11         Frame f= new Frame("Java Frame with Panel");
12         Panel p = new Panel(null);
13         f.setLayout(null);     //窗口不使用布局管理器
14         
15         /*
16          * 窗口的各个组件的位置由组件自己通过方法指定,
17          * 最后调用add方法把面板加到窗口中
18          * 
19          */
20         
21         
22         
23         
24         f.setBounds(100,100,300,200);
25         f.setBackground(new Color(0,0,102));
26         p.setBackground(Color.BLUE);
27         p.setBounds(50,50,50,50);
28         f.add(p);
29         f.setVisible(true);
30     }
31 
32 }

 

 

 
复制代码

 

1.2 布局管理器

1.2.1 FlowLayout

按照组件添加到容器中的顺序从左到右,从上到下依次排列,因此也称为流式布局!

是Panel的默认布局!

复制代码
 1 package awt;
 2 import java.awt.*;
 3 public class TestFlowLayout {
 4     
 5     public static void main(String[] args) {
 6         // TODO 自动生成的方法存根
 7         Frame f=new Frame("Java Frame");
 8 //        FlowLayout l=new FlowLayout(FlowLayout.LEFT);
 9         FlowLayout l=new FlowLayout(FlowLayout.TRAILING);
10         f.setLayout(l);
11         f.setLocation(300,400);
12         f.setSize(300,200);
13         f.setBackground(new Color(204,204,255));
14         for(int i=1;i<=7;i++)
15         {
16             f.add(new Button("Button"+i));
17         }
18         f.setVisible(true);
19     }
20     /*
21      * FlowLayout 一共有3个构造方法
22      * ①没有参数 居中对齐 组件间间距默认5像素
23      * ②一个参数 FlowLayout(int align)  指定对齐方式 
24      * 有  FlowLayout l=new FlowLayout(FlowLayout.LEFT);
25      *         FlowLayout l=new FlowLayout(FlowLayout.CENTER);
26      * LEADING  (与容器的方向的开始边对齐)
27      * TRAILING (与容器的方向的结束边对齐)
28      * 
29      * ③ 三个参数的构造方法   FlowLayout(int align,int hgap, int vgap)
30      * 指定对齐方式以及组件水平和垂直间距
31      * 
32      */
33 }


 

 

 
复制代码

 

posted @   靠谱杨  阅读(210)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示