JavaDailyReports10_08
2024-09-04 02:18:06
------------恢复内容开始------------
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 }
最新文章
- vs c++中读取数据流并存储
- protected 和default的区别
- 百万程序员的苦恼-选择VB.NET还是C#
- Java jdk环境搭建
- memcache 在php存取中的应用
- Struts2之初识篇(一)——与struts的区别和基本配置
- nodejs爬虫笔记(二)---代理设置
- LVM基本应用,扩展及缩减实现!
- 004.Heartbeat+HAProxy+MySQL半复制高可用架构
- centos linux查看硬盘型号
- Python XML操作
- cpp与其他语言相比较
- getting-started-with-mqtt
- linux git:fatal: HTTP request failed
- SSH 学习笔记
- NHibernate 数据查询之Linq to NHibernate
- Atitit.struts2体系结构大总结
- FineReport——自定义登录页
- Kafka自我学习3-Scalable
- ubuntu16.04.3安装MinDoc