Swing和AWT 是 java 开发 GUI 常用的技术 . 但是由于外观不太美观 , 组件数量偏少 , 并且运行需要 JRE 环境。
(了解MVC架构和监听器)
一、AWT
- AWT(Abstract Window Toolkit)包括了很多类和接口,用于Java Application的GUI(Graphics User Interface 图形用户界面)编程。
- GUI的各种元素(如:窗口,按钮,文本框等)由Java类来实现。
- 使用AWT所涉及的类一般在Java.AWT包及其子包中。
- Container和Component是AWT中的两个核心类。
二、组件和容器
java.util包下

1、frame
public class TestFrame {
    public static void main(String[] args) {
        //新建第一个java图形窗口
        Frame frame = new Frame("一个JAVAFrame");
        //设置窗口可见
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(new Color(85,150,68));
        //初始位置
        frame.setLocation(200,200);
        //设置frame固定大小
        frame.setResizable(false);
    }
}设置多个窗口
public class TestFrame2 {
    public static void main(String[] args) {
        MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.green);
        MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.blue);
    }
}
class MyFrame extends Frame{
    static int id =0;
    public MyFrame(int x,int y,int w,int h,Color color) throws HeadlessException {
        super("MyFrame"+(++id));
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);
    }
}
2、Panel
Paner(面板)是一个空间,不能单独存在
public class TetsPanel {
    public static void main(String[] args) {
        //新建一个窗口
        Frame frame = new Frame();
        //新建一个面板
        Panel panel = new Panel();
        //设置布局
        frame.setLayout(null);
        //设置坐标,背景颜色
        frame.setBounds(300,300,500,500);
        frame.setBackground(Color.blue);
        //设置pannel坐标,背景颜色
        panel.setBounds(50,50,400,400);
        panel.setBackground(Color.green);
        frame.add(panel);
        //设置窗口可见
        frame.setVisible(true);
        //监听事件System.exit(0)
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}3、布局管理器(FlowLayout、BorderLayout 、GridLayout)
- FlowLayout(流式布局
public class TestFlowlayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button1 = new Button("hello");
        Button button2 = new Button("world");
        Button button3 = new Button("好的");
        frame.setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setSize(400,400);
        frame.setVisible(true);
    }
}- BorderLayout
public class TextBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button1 = new Button("1");
        Button button2 = new Button("2");
        Button button3 = new Button("3");
        Button button4 = new Button("4");
        Button button5 = new Button("5");
        frame.setLayout(new BorderLayout());
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.SOUTH);
        frame.add(button3,BorderLayout.WEST);
        frame.add(button4,BorderLayout.NORTH);
        frame.add(button5,BorderLayout.CENTER);
        frame.setSize(500,500);
        frame.setVisible(true);
    }
}
- GridLayout












