天天看点

GUI编程核心技术之一(AWT)

什么是GUI?

在电脑的操作系统中实现图形化的用户界面(Graphical User Interface,简称GUI)

GUI组件分为两大类:基本组件和容器。

注意:需要 jre 环境!

1.什么是AWT技术?

AWT(Abstract Window Toolkit)抽象窗口工具集

特点:

  • AWT是重量级组件,因为用了大量的Windows函数
  • AWT只能在Windows平台下执行
  • 相比于Swing,AWT的代码较少且执行速度更快

java.awt包的结构:

GUI编程核心技术之一(AWT)

2.容器

容器(Container)也是一个类,实际上是Component的子类,因此容器本身也是一个组件,具有组件的所有性质,但是它的主要功能是容纳其它组件和容器。容器可以简化图形化界面的设计,以整体结构来布置界面。所有的容器都可以通过add()方法向容器中添加组件。

(1)框架Frame

public class TestFrame {
    public static void main(String[] args) {
        //Frame,JDK, 看源码!
        Frame frame = new Frame("我的第一个Java图像界面窗口");
        //设置窗口大小
        frame.setSize(400,400);
        //弹出的初始位置
        frame.setLocation(200,200);
        //设置背景颜色  Color
        frame.setBackground(new Color(85, 150, 68));
        //设置大小固定
        frame.setResizable(false);
        //需要设置可见性  
        frame.setVisible(true);
    }
}
           

注意:窗口不能关闭,如果关闭需要关闭程序

(2)面板Panel

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Panel 可以看成是一个空间,但是不能单独存在
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();
        //设置布局
        frame.setLayout(null);//绝对布局
        //坐标
        frame.setBounds(300,300,500,500);   // x,y:窗口位置
        frame.setBackground(new Color(40, 161, 35));
        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400); //设置画板位置大小
        panel.setBackground(new Color(193, 15, 60));//设置画板颜色
        frame.add(panel);     //将画板添加到窗口中
        frame.setVisible(true);
        //监听事件,监听窗口关闭事件  System.exit(0)
        //适配器模式 :
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}
           

注意:Panel 无法单独显示,必须添加到某个容器中。

3.布局管理器

GUI编程核心技术之一(AWT)

(1)流式布局

import java.awt.*;
public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //组件-按钮
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        //设置为流式布局
        //frame.setLayout(new FlowLayout());
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//将三个按钮放在窗口的最右边
        frame.setSize(200,200);
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
    }
}
           

(2)边界布局-东南西北中

import java.awt.*;
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");
        //设置4个按钮
        Button east = new Button("East");
        Button west = new Button("West");
        Button south = new Button("South");
        Button north = new Button("North");
        Button center = new Button("Center");
       // 将4个按钮放到窗口的东南西北中处
        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(north,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);
        frame.setSize(200,200);//窗口尺寸
        frame.setVisible(true);//窗口可见
    } 
}
           

(3)表格式布局

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");
        //设置6个按钮
        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");
        Button btn3 = new Button("btn3");
        Button btn4 = new Button("btn4");
        Button btn5 = new Button("btn5");
        Button btn6 = new Button("btn6");
        //设置表格式布局,参数1行数,参数2列数
        frame.setLayout(new GridLayout(3,2));
        //将6个按钮添加到窗口中
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);
        frame.pack(); //适应性窗口
        frame.setVisible(true);//窗口可见
    }
}
           

4.事件监听

(1)窗口监听:实现窗口的关闭,激活等监听

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}
class WindowFrame extends Frame{
    public WindowFrame(){
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        setVisible(true);
        //addWindowListener(new MyWindowListener());
        this.addWindowListener(
                //匿名内部类
            new WindowAdapter() {
                //关闭窗口
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("windowClosing");
                    System.exit(0);  
                }
                //激活窗口
                @Override
                public void windowActivated(WindowEvent e) {
                    WindowFrame source = (WindowFrame) e.getSource();
                    source.setTitle("被激活了");//被激活后窗口的抬头
                    System.out.println("windowActivated");
                }
            }
        );
    }
}
           

(2)鼠标监听:实现鼠标画画

//鼠标监听事件
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}
//自己的类
class MyFrame extends Frame{
    //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
    ArrayList points;
    public MyFrame(String title) {
        super(title);
        setBounds(200,200,400,300);
        //存鼠标点击的点
        points = new ArrayList<>();
        setVisible(true);
        //鼠标监听器,正对这个窗口
        this.addMouseListener(new MyMouseListener());
    }
    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
           Point point = (Point) iterator.next();
           g.setColor(Color.BLUE);
           g.fillOval(point.x,point.y,10,10);
    }
    }
    //添加一个点到界面上
    public void addPaint(Point point){
        points.add(point);
    }
    //适配器模式
    private class MyMouseListener extends MouseAdapter{
        //鼠标 按下,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame frame = (MyFrame) e.getSource();
            //这个我们点击的时候,就会在界面上产生一个点!画
            //这个点就是鼠标的点;
            frame.addPaint(new Point(e.getX(),e.getY()));
            //每次点击鼠标都需要重新画一遍
            frame.repaint();//刷新   30帧  60帧
        }
    }
}
           

(3)键盘监听:监听键盘输入的东西

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(1,2,300,400);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘下的键是哪一个,当前的码
                int keyCode = e.getKeyCode(); //不需需要去记录这个数值,直接使用静态属性 VK_XXX
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_UP) {
                    System.out.println("你按下了上键");
                }
                //根据按下不同操作,产生不同结果;
            }
        });
    }
}
           

(4)输入框 TextField 监听

public class TestText01 {
    public static void main(String[] args) {
        //启动!
        new MyFrame();
    }
}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter 就会触发这个输入框的事件
        textField.addActionListener(myActionListener2);
        //设置替换编码
        textField.setEchoChar('*');//输入后被替换为*
        setVisible(true);
        pack();
    }
}
class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();     //获得一些资源,返回的一个对象
        System.out.println(field.getText()); //获得输入框的文本
        field.setText(""); //null  ""
    }
}