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

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 }


 

最新文章

  1. vs c++中读取数据流并存储
  2. protected 和default的区别
  3. 百万程序员的苦恼-选择VB.NET还是C#
  4. Java jdk环境搭建
  5. memcache 在php存取中的应用
  6. Struts2之初识篇(一)——与struts的区别和基本配置
  7. nodejs爬虫笔记(二)---代理设置
  8. LVM基本应用,扩展及缩减实现!
  9. 004.Heartbeat+HAProxy+MySQL半复制高可用架构
  10. centos linux查看硬盘型号
  11. Python XML操作
  12. cpp与其他语言相比较
  13. getting-started-with-mqtt
  14. linux git:fatal: HTTP request failed
  15. SSH 学习笔记
  16. NHibernate 数据查询之Linq to NHibernate
  17. Atitit.struts2体系结构大总结
  18. FineReport——自定义登录页
  19. Kafka自我学习3-Scalable
  20. ubuntu16.04.3安装MinDoc

热门文章

  1. 【Usaco 2009 Silver】JZOJ2020年9月19日提高B组T1 音乐节拍
  2. day4(编写注册接口)
  3. 第7.12节 可共享的Python类变量
  4. 2、tensorflow 变量的初始化
  5. Tomcat是如何加载Spring和SpringMVC及Servlet相关知识
  6. Day3 【Scrum 冲刺博客】
  7. javascript编写原则
  8. 最简 Spring AOP 源码分析!
  9. 题解-[SDOI2014]数数
  10. cocosCreator微信小游戏排行榜思路