事件监听
事件监听:当某个事情发生的时候,干什么。
addActionListener() 设置监听
案例1 :点击一个button ,显示台打印相关信息
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TsetActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button("please!");
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button);
frame.pack();
windowClose(frame);
frame.setBounds(300,300,500,500);
frame.setVisible(true);
}
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("叮叮叮当");
}
}
案例2 : 多个按钮,实现同一个接口
- setActionCommand( ) 可以设置,显示的自定义,触发会返回的命令.
- 如果不显示定义,则会走默认的值!可以多个按钮只写一个监听器
- getActionCommand(),获取按钮上的信息
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TsetActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("start");
Button button2 = new Button("stop");
button2.setActionCommand("stop!!!!!!!!!!!!!");
mymoniter mymoniter = new mymoniter();
button1.addActionListener(mymoniter);
button2.addActionListener(mymoniter);
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
}
}
class mymoniter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了:"+e.getActionCommand());
}
}
输入框
new TextField()
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Testfield {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
myFrame.setVisible(true);
}
}
class MyFrame extends Frame {
public MyFrame(){
TextField textField = new TextField();
add(textField);
ActionListener1 actionListener1 = new ActionListener1();
textField.addActionListener(actionListener1);
textField.setEchoChar('*');
}
}
class ActionListener1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField fisld =(TextField) e.getSource();
System.out.println(fisld.getText());
fisld.setText("");
}
}
计算器
oop原则:组合,大于继承!
面向过程
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestCalculator {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame {
public Calculator(){
TextField textField1 = new TextField(10);
TextField textField2 = new TextField(10);
TextField textField3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(textField1, textField2, textField3));
Label label = new Label("+");
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyCalculatorListener implements ActionListener {
private TextField textField1;
private TextField textField2;
private TextField textField3;
public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3) {
this.textField1 = textField1;
this.textField2 = textField2;
this.textField3 = textField3;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(textField1.getText());
int n2 = Integer.parseInt(textField2.getText());
textField3.setText(""+(n1+n2));
textField1.setText("");
textField2.setText("");
}
}
改造为面向对象
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestCalculator {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame {
TextField textField1 ;
TextField textField2 ;
TextField textField3 ;
public Calculator(){
load();
}
public void load(){
textField1 = new TextField(10);
textField2 = new TextField(10);
textField3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));
Label label = new Label("+");
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyCalculatorListener implements ActionListener {
private Calculator calculator;
public MyCalculatorListener( Calculator calculator) {
this.calculator=calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(calculator.textField1.getText());
int n2 = Integer.parseInt(calculator.textField2.getText());
calculator.textField3.setText(""+(n1+n2));
calculator.textField1.setText("");
calculator.textField2.setText("");
}
}
内部类
内部类好处,畅通无阻的访问外部类
package awt;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestCalculator {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame {
TextField textField1 ;
TextField textField2 ;
TextField textField3 ;
public Calculator(){
load();
}
public void load(){
textField1 = new TextField(10);
textField2 = new TextField(10);
textField3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());
Label label = new Label("+");
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private class MyCalculatorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(textField1.getText());
int n2 = Integer.parseInt(textField2.getText());
textField3.setText(""+(n1+n2));
textField1.setText("");
textField2.setText("");
}
}
}
画笔 paint
package awt;
import java.awt.*;
public class TestPanint {
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.green);
g.fillRect(150,200,200,200);
}
}
鼠标监听
- 当点击鼠标按钮时,有三个监听器方法可以调用:①鼠标按下时调用mousePressed;②鼠标被释放时调用mouseReleased;③关注鼠标的最终点击事件时调用mouseClicked
- 鼠标点击可以由mouseClicked过程报告,该方法是MouseListener接口的一部分。通常大部分应用程序只对鼠标点击事件感兴趣,而对鼠标移动及拖动并不感兴趣,但是鼠标移动及拖动事件又经常发生,因此,鼠标移动与拖动事件定义在一个MouseMotionListener接口中。
目的:实现鼠标画画
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouse {
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<>();
this.addMouseListener(new MyMouse());
setVisible(true);
}
@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,5,5);
}
}
public void addPoint(Point point){
points.add(point);
}
private class MyMouse extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
myFrame.addPoint(new Point(e.getX(),e.getY()));
myFrame.repaint();
}
}
}
窗口监听
窗口退出
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(){
setBounds(100,100,300,300);
setBackground(Color.CYAN);
setVisible(true);
addWindowListener(new MyWindowListener());
}
class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
点击右上角的x,窗口关闭
匿名内部类
package awt;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame(){
setBounds(100,100,300,300);
setBackground(Color.CYAN);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
WindowAdapter 相关方法
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBounds(100,100,300,300);
setBackground(Color.CYAN);
setVisible(true);
this.addWindowListener(
new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("windowIconified");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("windowDeiconified");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
}
);
}
}
键盘监听
import javafx.animation.KeyFrame;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKey {
public static void main(String[] args) {
new KeyFrame1();
}
}
class KeyFrame1 extends Frame {
public KeyFrame1(){
setBounds(100,100,300,300);
setBackground(Color.CYAN);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if(keyCode == KeyEvent.VK_UP){
System.out.println("上键");
}
}
});
}
}