学习资料
b站狂神说:https://www.bilibili.com/video/BV1DJ411B75F
输入框与监听
package com.zy7y.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui
* @Author: zy7y
* @Date: 2020/8/15 下午8:26
* @Description:
*/
public class TestText01 {
public static void main(String[] args) {
new MyFrame1();
}
}
class MyFrame1 extends Frame{
public MyFrame1(){
TextField textField = new TextField();
add(textField);
// 监听文本框输入的文字
MyActionListener myActionListener = new MyActionListener();
// 按下enter 触发事件
textField.addActionListener(myActionListener);
// 设置替换编码; 加密操作,输入内容将显示*
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// e.getSource(); // 获得一些资源,返回的是一个对象
TextField textField = (TextField) e.getSource();
System.out.println(textField.getText()); // 获得输入框的文本
// 将文本设置为空
textField.setText("");
}
}
简易加法计算器
package com.zy7y.gui.awt;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.awt
* @Author: zy7y
* @Date: 2020/8/15 下午8:51
* @Description:
*/
public class Calc {
public static void main(String[] args) {
new CalcFrame().loadFrame();
}
}
// 布局
class CalcFrame extends Frame{
// 属性
TextField textField,textField1,textField2;
public void loadFrame(){
setTitle("只能算加法的计算器.");
textField = new TextField(10); // 10位文本框的长度
textField1 = new TextField(10); // 10位文本框的长度
textField2 = new TextField(20); // 10位文本框的长度
// 按钮
Button result = new Button("=");
// 标签
Label label = new Label("+");
// 流式布局
setLayout(new GridBagLayout());
add(textField);
add(label);
add(textField1);
add(result);
add(textField2);
// 处理 = 号的事件
ResultListens resultListens = new ResultListens();
result.addActionListener(resultListens);
closeWindows();
// 设置可见
setVisible(true);
}
// 关闭窗口的方法
public void closeWindows(){
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// 事件处理:用内部类 直接就可以获取到外部类里面的属性
private class ResultListens implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// 获得文本内容
int n1 = Integer.parseInt(textField.getText());
int n2 = Integer.parseInt(textField1.getText());
// String.valueOf, 可以转换成字符串
textField2.setText(String.valueOf(n1 + n2));
// 清除前面的框
textField.setText("");
textField1.setText("");
}
}
}
画笔
package com.zy7y.gui.awt;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.awt
* @Author: zy7y
* @Date: 2020/8/15 下午9:25
* @Description:
*/
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setTitle("画笔实操");
setBounds(200,200,600,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// 重写paint方法,并能直接加载出对应的操作
@Override
public void paint(Graphics g){
g.setColor(Color.red);
// 实心的圆
g.fillOval(100,100,100,100);
}
}
鼠标监听器、模拟画图工具
package com.zy7y.gui.awt;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.awt
* @Author: zy7y
* @Date: 2020/8/16 上午11:55
* @Description: 鼠标监听
*/
public class MouseListener {
public static void main(String[] args) {
new MyFrameMouse("画图工具");
}
}
class MyFrameMouse extends Frame{
// 存点的集合
ArrayList points;
public MyFrameMouse(String title) {
super(title);
setBounds(300,300,300,300);
// 存取鼠标点
points = new ArrayList<>();
// 鼠标监听
addMouseListener(new MyMouse());
setVisible(true);
}
// 重写画笔
@Override
public void paint(Graphics g){
// 迭代器
Iterator iterator = points.iterator();
// iterator.hasNext 还能迭代否
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.green);
g.fillOval(point.x,point.y,10,10);
}
}
// 添加点
public void addPaint(Point point){
points.add(point);
// 打印查看当前集合中有的数据:[java.awt.Point[x=162,y=157]]
//[java.awt.Point[x=162,y=157], java.awt.Point[x=158,y=212]]
//[java.awt.Point[x=162,y=157], java.awt.Point[x=158,y=212], java.awt.Point[x=229,y=143]]
System.out.println(points);
}
// 适配器模式
private class MyMouse extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
MyFrameMouse myFrameMouse = (MyFrameMouse)e.getSource();
myFrameMouse.addPaint(new Point(e.getX(),e.getY()));
// 重画
myFrameMouse.repaint();
}
}
}
效果图:
窗口监听事件
package com.zy7y.gui.awt;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.awt
* @Author: zy7y
* @Date: 2020/8/16 下午1:05
* @Description: 窗口事件
*/
public class TestWindow {
public static void main(String[] args) {
new MyWindowEvent();
}
}
//
class MyWindowEvent extends Frame {
public MyWindowEvent() {
super("窗口事件");
setBounds(300, 300, 300, 300);
setVisible(true);
addWindowListener(
// 匿名内部类
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口关闭");
}
// 窗口激活
@Override
public void windowActivated(WindowEvent e) {
super.windowActivated(e);
System.out.println("窗口拥有焦点");
}
}
);
}
}
键盘监听事件
package com.zy7y.gui.awt;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.awt
* @Author: zy7y
* @Date: 2020/8/16 下午1:17
* @Description: 键盘监听
*/
public class TestKeyListener {
public static void main(String[] args) {
new MyKeyFrame();
}
}
class MyKeyFrame extends Frame{
public MyKeyFrame(){
super("键盘监听");
setBounds(200,200,200,200);
setVisible(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
}
// 键盘按下
@Override
public void keyPressed(KeyEvent e) {
// 获得输入键的字符值,但是 上下左右箭头,空格好像不行
System.out.println(e.getKeyChar());
// 获得输入键对应的code
System.out.println(e.getKeyCode());
//
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
System.out.println("你按下了空格键!");
}
}
});
}
}