事件监听
①窗口监听
源代码:
package cn.ecut.awt.listenner;
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);
setVisible(true);
setBounds(100,100,400,400);
// addWindowListener(new MyWindowListener());
//匿名内部类
this.addWindowListener(new WindowAdapter() {
//常用就两个
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source=(WindowFrame) e.getSource();
source.setTitle("被激活了!");
System.out.println("窗口已激活!");
}
});
}
// private class MyWindowListener extends WindowAdapter{
// @Override
// public void windowClosing(WindowEvent e) {
// setVisible(false);
// System.exit(0);
// }
// }
}
运行结果:
②鼠标监听
源代码:
package cn.ecut.awt.listenner;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListenner {
public static void main(String[] args) {
new MyFrame("画画");
}
}
class MyFrame extends Frame{
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(200,200,400,300);
setVisible(true);
//存鼠标点击的点
points =new ArrayList<>();
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 addPoint(Point point){
points.add(point);
}
//监听类(适配器模式)
private class MyMouseListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame=(MyFrame) e.getSource();
frame.addPoint(new Point(e.getX(),e.getY()));
frame.repaint();
}
}
}
运行结果:
③键盘监听
源代码:
package cn.ecut.awt.listenner;
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(){
setBackground(Color.blue);
setVisible(true);
setBounds(10,10,400,500);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键!");
}
}
});
}
}
运行结果:
④画笔paint
源代码:
package cn.ecut.awt.listenner;
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,500,600);
setVisible(true);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.green);
g.drawOval(100,100,100,100);
g.fillOval(200,200,100,100);
g.setColor(Color.red);
g.fillRect(400,400,100,100);
}
}
运行结果:
⑤输入框事件监听
源代码:
package cn.ecut.awt.event;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField=new TextField();
textField.setEchoChar('*');//设置替换编码
add(textField);
pack();
setVisible(true);
MyActionListenner3 myActionListenner3=new MyActionListenner3();//监听文本框输入的文字
textField.addActionListener(myActionListenner3);//按下enter,就会触发这个输入框的事件
}
}
class MyActionListenner3 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField textField=(TextField) e.getSource();//获取资源,返回一个对象
System.out.println(textField.getText());//获得输入框的文本
textField.setText("");//null
}
}
运行结果:
⑥简单事件监听1
源代码:
package cn.ecut.awt.event;
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 TestActionEvent {
public static void main(String[] args) {
Frame frame=new Frame();
Button button=new Button();
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
//按下按钮,触发一些事件
MyActionListenner myActionListenner=new MyActionListenner();
button.addActionListener(myActionListenner);
//关闭窗体
windowClose(frame);
}
//关闭窗体事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听类
class MyActionListenner implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("你点击了按钮!");
}
}
运行结果:
⑦简单事件监听2
源代码:
package cn.ecut.awt.event;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionEvent2 {
public static void main(String[] args) {
Frame frame=new Frame("开始-停止");
Button button1=new Button("start");
Button button2=new Button("stop");
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
button2.setActionCommand("button2-stop");//设置按钮的信息,若没有设置则默认为初始设置的
MyActionListenner2 myActionListenner2=new MyActionListenner2();
button1.addActionListener(myActionListenner2);
button2.addActionListener(myActionListenner2);
}
}
class MyActionListenner2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()获取按钮的信息
System.out.println("按钮被点击了:msg=>"+e.getActionCommand());
}
}
运行结果: