AWT展开目录
AWT(Abstract Window Toolkit)包含了很多类和接口,用于 Java Application 的 GUI(Graphics User Interface 图形用户界面)编程。使用 AWT 所涉及的类一般在 java.awt 包及其子包中。Container 和 Component 是 AWT 中的两个核心包
Component&Container展开目录
- java 的图形用户界面最基本组成部分是 Component,Component 类及其子类的对象用来描述以图形化的方式显示在屏幕上并能与其用户进行交互的 GUI 元素,例如,一个按钮,一个标签等。
- 一般的 Component 对象不能独立地显示出来,必须 “放在” 某一 Container 对象中才可以显示出来
- Container 是 Component 子类,Container 子类对象可以 “容纳” 别的 Component 对象
- Container 对象可使用方法 add (...) 向其中添加其他 Component 对象
- Container 是 Component 的子类,因此 Container 对象也可被当作 Component 对象添加到其他 Container 对象中
有两种常用的 Container:
- Window:其对象表示自由停泊的顶级窗口
- Panel:其对象可作为容纳其它 Componet 对象,但不能独立存在,必须被添加到其它 Container 中(如 Window 或 Applet)
Frame展开目录
- Frame 是 Window 的子类,由 Frame 或其子类创建的对象为一个窗体
Frame 的常用构造方法:
- Frame()
- Frame (String s) 创建标题栏为字符串 s 的窗口
- setBounds(int x,int y,int width,int height)//设置窗体位置和大小,x,y是左上角坐标,width和height是宽度和高度
- setSize(int width,int height)//设置窗体的大小,width和height分别是宽度和高
- setLocation(int x,int y)//度设置窗体的位置,x,y是左上角坐标
- setBackground(Color c)//设置背景颜色,参数为Color对象
- setVisible(boolean b)//设置是否可见
- setTitle(String name)//设置窗体标题
- setResizable(boolean b)//设置是否可以调整大小
- import java.awt.*;
- public class TestFrame {
- public static void main(String[] args) {
- Frame f = new Frame("My First Test");
- f.setSize(600,600);//窗体大小,单位像素
- f.setBackground(Color.red);//窗体背景色
- f.setVisible(true);//窗体是否可见
- }
- }
上面是绘制使用 Frame 类绘制窗口的一个示例,如果不调用 setVisible 方法是无法看到窗口的。整个电脑左上角是坐标原点,往右是 x 轴增大的方向,往下是 y 轴增大的方向,setLocation (300,300) 是窗口最左上角点的坐标,显示效果见下图
- import java.awt.*;
- public class TestMultiFrame {
- public static void main(String[] args) {
- MyFrame f1 = new MyFrame(100,100,400,400,Color.red);
- MyFrame f2 = new MyFrame(500,100,400,400,Color.yellow);
- MyFrame f3 = new MyFrame(100,500,400,400,Color.blue);
- MyFrame f4 = new MyFrame(500,500,400,400,Color.green);
- }
- }
- class MyFrame extends Frame {
- private static int id = 0;
- MyFrame(int x,int y,int w,int h,Color color) {
- super("MyFrame" + (++id));
- setBackground(color);
- setBounds(x,y,w,h);
- setVisible(true);
- }
- }
Panel展开目录
- Panel 对象可以看成可以容纳 Component 的空间
- Panel 对象可以拥有自己的布局管理器
Panel 类拥有其父类继承来的
- setBounds(int x,int y,int width,int height)
- setSize(int width,int height)
- setLocation(int x,int y)
- setBackground(Color c)
- SetLayout(LayoutManager mgr)
Panel 的构造方法
- Panel ():使用默认的 FlowLayout 类布局管理器初始化
- Panel (LayoutManager layout):使用指定的布局管理器初始化
- import java.awt.*;
- public class Testpanel {
- public static void main(String[] args) {
- Frame f = new Frame("Java Frame with Panel");
- Panel p = new Panel(null);
- f.setLayout(null);
- f.setBounds(300,300,500,500);
- f.setBackground(new Color(0,0,102));
- p.setBounds(50,50,400,400);
- p.setBackground(new Color(204,204,255));
- f.add(p);
- f.setVisible(true);
- }
- }
有一点要注意,Frame 的 setBounds 方法参数中 x,y 的位置是相对于屏幕,而 Panel 装到哪个里面,x,y 就相对于哪一个窗口的位置,在这里就是相对于外层的 Frame
- import java.awt.*;
- class MyFrame2 extends Frame {
- private Panel p1,p2,p3,p4;
- MyFrame2(String s,int x,int y,int w,int h) {
- super(s);
- setLayout(null);
- p1 = new Panel(null);p2 = new Panel(null);
- p3 = new Panel(null);p4 = new Panel(null);
- p1.setBounds(0,0,w/2,h/2);
- p2.setBounds(0,h/2,w/2,h/2);
- p3.setBounds(w/2,0,w/2,h/2);
- p4.setBounds(w/2,h/2,w/2,h/2);
- p1.setBackground(Color.red);
- p2.setBackground(Color.yellow);
- p3.setBackground(Color.blue);
- p4.setBackground(Color.green);
- add(p1);add(p2);add(p3);add(p4);
- setBounds(x,y,w,h);
- setVisible(true);
- }
- }
- public class TestMultiPanel {
- public static void main(String[] args) {
- new MyFrame2("MyFrame with Panel",300,300,400,300);
- }
- }
作业展开目录
设计一个如下图所示的窗口
作业???
一个小任务而已