1.输入框 TextField监听
package com.kuang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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,null为一个对象,""为字符串
}
}
2 简易计算器,组合+内部类回顾复习
oop原则:组合,大于继承
面向过程:
package com.kuang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Caluctor(){
//3个文本框
TextField num1 = new TextField(10);//括号里表示最大能填的字符数
TextField num2 = new TextField(10);//字符数
TextField num3 = new TextField(20);//字符数
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//获取三个变量
private TextField num1, num2, num3;
public MyCalculatorListener(TextField num1, TextField num2, TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e){
//1.获得加数和背加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.将这个值 加法运算后,放到第三个框
num3.setText(""+(n1 + n2));
//3.清除前两个框
num1.setText("");
num2.setText("");
}
}
完全改造为面向对象写法:
package com.kuang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//括号里表示最大能填的字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
//1个按钮
Button button = new Button("=");
//1个标签
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener(this));//this代表自己
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//获取计算器这个对象,在一个类中组合另外一个类
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e){
//1.获得加数和背加数
//2.将这个值 加法运算后,放到第三个框
//3.清除前两个框
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
calculator.num3.setText(""+(n1+n2));
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类:
更好的包装:
package com.kuang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//括号里表示最大能填的字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
//1个按钮
Button button = new Button("=");
//1个标签
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener());//this代表自己
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
//内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和背加数
//2.将这个值 加法运算后,放到第三个框
//3.清除前两个框
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText("" + (n1 + n2));
num1.setText("");
num2.setText("");
}
}
}
3 画笔
package com.kuang.lesson03;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
@Override
public void paint(Graphics g) {
//画笔,需要有颜色,画笔可以画画
// g.setColor(Color.red);
//g.drawOval(100,100,100,100);
g.fillOval(100,100,100,100);//实心的圆
// g.setColor(Color.blue);
g.fillRect(150,200,200,200);
//养成习惯,画笔用完,将他还原到最初的颜色
}
}
4鼠标监听
package com.kuang.lesson03;
//鼠标监听事件
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
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,400);
//存鼠标点击的点
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) {//e获取鼠标的资源
MyFrame frame = (MyFrame) e.getSource();
//这里我们点击的时候,就会在界面上产生一个点!画
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();//刷新
}
}
}
5窗口监听
package com.kuang.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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");
}
}
);
}
}
6键盘监听
package com.kuang.lesson03;
//键
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
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();
System.out.println(keyCode);//不需要去记录这个数值,直接使用静态属性VK_XXX
if(keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
//根据按下不同操作,产生不同结果
}
});
}
}