AWT
1. AWT介绍
AWT:抽象的窗口工具,包含很多类和接口。
元素:窗口、按钮、文本框
存在于java.awt包下。
2. 组件和容器
2.1. Frame
public class Demo1 {
public static void main(String[] args) {
// 新建Frame在内存中
Frame frame = new Frame("Java图形界面窗口");
// 默认窗口大小为0
// 设置窗口大小
frame.setSize(400, 400);
// 设置背景颜色
frame.setBackground(Color.WHITE);
// 弹出的初始位置
frame.setLocation(200, 200);
// 设置不可改变大小
frame.setResizable(false);
// 设置可见性
frame.setVisible(true);
}
}
public class Demo2 {
public static void main(String[] args) {
// 展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.CYAN);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.YELLOW);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.RED);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.PINK);
}
}
class MyFrame extends Frame {
static int id = 0; // 可能存在多个窗口,需要一个计数器
public MyFrame(int x, int y, int w, int h, Color color) {
super("MyFrame" + (++id));
// 设置窗口位置及大小
setBounds(x, y, w, h);
setBackground(color);
setVisible(true);
}
}
2.2. Panel
Panel可以内嵌,不可单独存在,在容器里面。需要放在Frame上。
布局嵌套时,需要使用面板来设置布局。
解决了关闭事件。
public class Demo3 {
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(new Color(44, 166, 44));
// panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(80, 93, 105));
// frame添加panel
frame.add(panel);
frame.setVisible(true);
// 监听事件 监听窗口关闭事件
// 适配器模式
frame.addWindowListener(new WindowAdapter() {
// 窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
// 结束程序
System.exit(0);
}
});
}
}
2.3. 布局管理器
流式布局FlowLayout
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(FlowLayout.LEFT));
frame.setSize(200, 200);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
东南西北中BorderLayout
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
frame.setSize(200, 200);
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");
// 设置布局
frame.setLayout(new BorderLayout());
// 添加组件
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.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
网格布局GridLayout
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
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");
// 行数 列数 垂直间隔 水平间隔
frame.setLayout(new GridLayout(3, 2, 0, 0));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
// Java函数,自动选择最优秀的位置
frame.pack();
frame.setVisible(true);
}
}
2.4. 练习
public class TestLayout {
public static void main(String[] args) {
Frame frame = new Frame("My Layout");
frame.setLayout(new GridLayout(2, 1));
// 上
Panel panel1 = new Panel(new BorderLayout());
panel1.add(new Button("button"), BorderLayout.WEST);
panel1.add(new Button("button"), BorderLayout.EAST);
// 上中
Panel panel3 = new Panel(new GridLayout(2, 1));
panel3.add(new Button("button"));
panel3.add(new Button("button"));
panel1.add(panel3, BorderLayout.CENTER);
// 下
Panel panel2 = new Panel(new BorderLayout());
panel2.add(new Button("button"), BorderLayout.WEST);
panel2.add(new Button("button"), BorderLayout.EAST);
// 下中
Panel panel4 = new Panel(new GridLayout(2, 2));
panel4.add(new Button("button"));
panel4.add(new Button("button"));
panel4.add(new Button("button"));
panel4.add(new Button("button"));
panel2.add(panel4, BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel2);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(400, 400);
frame.setVisible(true);
}
}
2.5. 总结
- Frame是一个顶级窗口。
- Panel无法单独显示,必须添加到某个容器中。
- 布局管理器
- 流式
- 东西南北中
- 网格
- 大小,定位,背景,颜色,可见性。
- 关闭窗口。
3. 事件监听
public class Demo4 {
public static void main(String[] args) {
Frame frame = new Frame();
// 按下按钮,触发一些事件
Button button = new Button("button");
// 因为addActionListener需要一个ActionListener所以需要构建一个ActionListener
button.addActionListener(new MyListener());
frame.add(button, BorderLayout.CENTER);
windowClose(frame);
frame.pack();
frame.setVisible(true);
}
// 关闭窗体事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("clicked");
}
}
多个按钮共享一个事件
public class Demo5 {
public static void main(String[] args) {
// 两个按钮,实现同一个监听
Frame frame = new Frame("开始-停止");
Button btn1 = new Button("start");
// 可以显式定义出发会返回的命令。如果不显示定义则会走默认的值。
// 可以多个按钮只写一个监听类
btn1.setActionCommand("start");
Button btn2 = new Button("stop");
btn2.setActionCommand("stop");
MyMonitor myMonitor = new MyMonitor();
btn1.addActionListener(myMonitor);
btn2.addActionListener(myMonitor);
frame.add(btn1, BorderLayout.NORTH);
frame.add(btn2, BorderLayout.SOUTH);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了:" + e.getActionCommand());
// if (e.getActionCommand().equals("start")) {
//
// }
}
}
4. 输入框TextField监听
public class Demo6 {
public static void main(String[] args) {
// 启动
new MyFrame1();
}
}
class MyFrame1 extends Frame {
public MyFrame1() {
TextField textField = new TextField();
add(textField);
// 监听这个文本框输入的文字
// 按下enter就会触发这个输入框的事件
MyActionListener myActionListener = new MyActionListener();
textField.addActionListener(myActionListener);
// 设置替换编码
textField.setEchoChar('*');
pack();
setVisible(true);
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// 获得一个资源 返回一个对象 监听了谁就是谁
TextField source = (TextField) e.getSource();
// 获得输入框中的文本
System.out.println(source.getText());
// 清空文本
source.setText("");
}
}
5. 简易计算器,组合+内部类回顾复习
oop原则:组合大于继承,优先使用组合
// A继承B的全部特性
class A extends B{
}
// 组合
class A {
public B b;
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 简易计算器
*
* @author admin
* @date 2021/8/7 8:30 下午
*/
public class Calc {
public static void main(String[] args) {
new Calculator();
}
}
// 计算器类
class Calculator extends Frame {
public Calculator() {
this.setTitle("Calculator");
// 3个文本框
// 最大可写10个字符
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
// 1个按钮
Button button = new Button("=");
MyCalculatorListener myCalculatorListener = new MyCalculatorListener(num1, num2, num3);
button.addActionListener(myCalculatorListener);
// 1个标签
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
closeWindow(this);
}
private void closeWindow(Calculator calculator) {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
// 监听器类
class MyCalculatorListener implements ActionListener {
private TextField num1;
private TextField num2;
private TextField num3;
// 获取三个变量
public MyCalculatorListener(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
// 获得两个加数
int a = Integer.parseInt(num1.getText());
int b = Integer.parseInt(num2.getText());
// 加法运算后放到第三个框
num3.setText(Integer.toString(a + b));
// 清除前两个框
num1.setText("");
num2.setText("");
}
}
完全改造为面向对象写法,使用了组合
/**
* 简易计算器
*
* @author admin
* @date 2021/8/7 8:30 下午
*/
public class Calc {
public static void main(String[] args) {
new Calculator().load();
}
}
// 计算器类
class Calculator extends Frame {
private TextField num2;
private TextField num3;
private Button button;
private Label label;
private TextField num1;
public TextField getNum1() {
return num1;
}
public void setNum1(TextField num1) {
this.num1 = num1;
}
public TextField getNum2() {
return num2;
}
public void setNum2(TextField num2) {
this.num2 = num2;
}
public TextField getNum3() {
return num3;
}
public void setNum3(TextField num3) {
this.num3 = num3;
}
public Button getButton() {
return button;
}
public void setButton(Button button) {
this.button = button;
}
public Label getLabel() {
return label;
}
public void setLabel(Label label) {
this.label = label;
}
public void load() {
this.setTitle("Calculator");
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
button = new Button("=");
label = new Label("+");
MyCalculatorListener myCalculatorListener = new MyCalculatorListener(this);
button.addActionListener(myCalculatorListener);
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
closeWindow(this);
}
private void closeWindow(Calculator calculator) {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
// 监听器类
class MyCalculatorListener implements ActionListener {
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
// 获得两个加数
int num1 = Integer.parseInt(calculator.getNum1().getText());
int num2 = Integer.parseInt(calculator.getNum2().getText());
// 加法运算后放到第三个框
calculator.getNum3().setText(Integer.toString(num1 + num2));
// 清除前两个框
calculator.getNum1().setText("");
calculator.getNum2().setText("");
}
}
内部类
- 更好的包装。
- 最大的好处,就是可以畅通无阻的访问外部类的属性和方法。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 简易计算器
*
* @author admin
* @date 2021/8/7 8:30 下午
*/
public class Calc {
public static void main(String[] args) {
new Calculator().load();
}
}
// 计算器类
class Calculator extends Frame {
private TextField num1;
private TextField num2;
private TextField num3;
private Button button;
private Label label;
public TextField getNum1() {
return num1;
}
public void setNum1(TextField num1) {
this.num1 = num1;
}
public TextField getNum2() {
return num2;
}
public void setNum2(TextField num2) {
this.num2 = num2;
}
public TextField getNum3() {
return num3;
}
public void setNum3(TextField num3) {
this.num3 = num3;
}
public Button getButton() {
return button;
}
public void setButton(Button button) {
this.button = button;
}
public Label getLabel() {
return label;
}
public void setLabel(Label label) {
this.label = label;
}
public void load() {
this.setTitle("Calculator");
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
button = new Button("=");
label = new Label("+");
MyCalculatorListener myCalculatorListener = new MyCalculatorListener();
button.addActionListener(myCalculatorListener);
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
closeWindow(this);
}
private void closeWindow(Calculator calculator) {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// 监听器内部类
private class MyCalculatorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// 获得两个加数
int a = Integer.parseInt(num1.getText());
int b = Integer.parseInt(num2.getText());
// 加法运算后放到第三个框
num3.setText(Integer.toString(a + b));
// 清除前两个框
num1.setText("");
num2.setText("");
}
}
}
6. 画笔
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author admin
* @date 2021/8/7 11:45 下午
*/
public class Demo7 {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame {
public void loadFrame() {
setBounds(200, 200, 600, 500);
setVisible(true);
windowClose(this);
}
private void windowClose(MyPaint myPaint) {
myPaint.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
// super.print(g);
// 画笔需要有颜色,可以画画
g.setColor(Color.CYAN);
g.drawOval(100, 100, 100, 100);
g.setColor(Color.GREEN);
g.fillRect(200, 200, 100, 100);
// 养成习惯 画笔用完要还原到最初的颜色
g.setColor(null);
}
}
画笔写字
g.setColor(Color.BLACK);
g.setFont(new Font("微软雅黑", Font.BOLD, 40));
g.drawString("按下空格开始游戏", 300, 300);
7. 监听
7.1. 鼠标监听
目的:实现鼠标点击
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @author admin
* @date 2021/8/8 12:06 上午
*/
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame2("画图");
}
}
class MyFrame2 extends Frame {
// 画画需要画笔 需要监听鼠标当前的位置
// 需要集合来存储这个点
private ArrayList<Point> points;
public MyFrame2(String title){
super(title);
setBounds(200, 200, 400, 300);
setVisible(true);
// 存放鼠标点击的点
points = new ArrayList<>();
// 鼠标监听器 针对这个窗口
this.addMouseListener(new MyMouseListener());
closeWindow(this);
}
private void closeWindow(MyFrame2 myFrame2) {
myFrame2.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
// super.paint(g);
// 画画 需要监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.CYAN);
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) {
// super.mousePressed(e);
MyFrame2 frame = (MyFrame2) e.getSource();
// 这里点击的时候,就会在界面上产生一个点
frame.addPaint(new Point(e.getX(), e.getY()));
// 每次点击鼠标都需要重新画一次
frame.repaint();
}
}
}
7.2. 窗口监听
public class Demo8 {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setBounds(100, 100, 200, 200);
setBackground(Color.BLUE);
// 匿名内部类
addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
}
);
setVisible(true);
}
}
7.3. 键盘监听
public class Demo9 {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame {
public KeyFrame() {
super();
setBounds(100, 100, 200, 200);
setVisible(true);
addKeyListener(
new KeyAdapter() {
// 键盘按下
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
// 键盘按下的键是哪一个 当前键盘的码
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("你按下了上键");
}
// 根据按下不同的操作,产生不同的结果
}
}
);
}
}