java事件监听机制

时间:2021-06-02 13:43:43

1.简单的事件监听

java事件监听机制

package demo2;

/*
* java事件监听机制
*/
import java.awt.*;
import javax.swing.*;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.ByteOrder; public class Demo1 extends JFrame implements ActionListener {
JPanel m;
JButton jb1, jb2; public static void main(String[] args) {
Demo1 demo = new Demo1();
} public Demo1() {
m = new JPanel();
jb1 = new JButton("黑色");
jb2 = new JButton("红色");
this.add(jb1, BorderLayout.NORTH);
m.setBackground(Color.BLACK);
this.add(m);
this.add(jb2, BorderLayout.SOUTH);
// 注册监听
jb1.addActionListener(this);
jb2.addActionListener(this);
// 指定action命令
jb1.setActionCommand("aa");
jb2.setActionCommand("bb"); this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true); } @Override
public void actionPerformed(ActionEvent e) {
// 判断哪个按钮被点击
if (e.getActionCommand().equals("aa")) {
System.out.println("点击黑色");
m.setBackground(Color.BLACK);
} else if (e.getActionCommand().equals("bb")) {
System.out.println("点击红色");
m.setBackground(Color.red);
} else {
System.out.println("点击错误");
}
}
}

2.设置普通类Cat进行监听

package demo2;

/*
* java事件监听机制
*/
import java.awt.*;
import javax.swing.*;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.ByteOrder; public class Demo1 extends JFrame implements ActionListener {
JPanel m;
JButton jb1, jb2;
Cat cat; public static void main(String[] args) {
Demo1 demo = new Demo1();
} public Demo1() {
cat = new Cat();
m = new JPanel();
jb1 = new JButton("黑色");
jb2 = new JButton("红色");
this.add(jb1, BorderLayout.NORTH);
m.setBackground(Color.BLACK);
this.add(m);
this.add(jb2, BorderLayout.SOUTH);
// 注册监听
jb1.addActionListener(cat);
jb2.addActionListener(cat);
// 指定action命令
jb1.setActionCommand("aa");
jb2.setActionCommand("bb"); this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true); } @Override
public void actionPerformed(ActionEvent e) {
// 判断哪个按钮被点击
if (e.getActionCommand().equals("aa")) {
System.out.println("点击黑色");
m.setBackground(Color.BLACK);
} else if (e.getActionCommand().equals("bb")) {
System.out.println("点击红色");
m.setBackground(Color.red);
} else {
System.out.println("点击错误");
}
} // 设计cat内部类
class Cat implements ActionListener { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("aa")) {
System.out.println("点击黑色");
m.setBackground(Color.BLACK);
} else if (e.getActionCommand().equals("bb")) {
System.out.println("点击红色");
m.setBackground(Color.red);
} else {
System.out.println("点击错误");
}
} }
}

3.上下左右移动

package demo3;

//横着x 竖着y
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; public class Demo3 extends JFrame {
Myframe mp; public static void main(String[] args) {
Demo3 demo = new Demo3();
} public Demo3() {
mp = new Myframe();
this.add(mp);
this.addKeyListener(mp);
this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
} // Myframe定义的面板用于绘图
class Myframe extends JPanel implements KeyListener {
int x = 10, y = 10; public void paint(Graphics g) {
super.paint(g);
g.fillOval(x, y, 10, 10);
} @Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
y += 2;
System.out.println("向下移动");
this.repaint();
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
y -= 2;
System.out.println("向上移动");
this.repaint();
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
x -= 2;
System.out.println("向左移动");
this.repaint();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
x += 2;
System.out.println("向右移动");
this.repaint();
}
} @Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub } @Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub } }