事件监听是指当某个事件发生的时候干一些什么。
例如之前在关闭frame窗口时就写过一个window窗口监听,当点击左上角❌时调用System.exit进行程序关闭。
1.按钮监听
下面的例子是监听按钮Button被点击时触发的事件
同时我们将窗口关闭监听事件进行了优化,将windowCloseing写为一个方法,在使用时直接调用不用再进行方法的重写。
package YMP.GUI; 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("Button"); //通过addActionListener()给button添加监听 //addActionListener需要ActionListener接口,所以构造一个ActionListener //button添加监听,当button被点击时调用MyActionListener() button.addActionListener(new MyActionListener()); frame.add(button,BorderLayout.CENTER); frame.setSize(200,200); frame.setVisible(true); //调用windowClose方法关闭窗口 windoeClose(frame); } //关闭窗口事件 private static void windoeClose(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("aaa");//当监听到行为时的事件:输出aaa } }
2.两个按钮共用一个监听
package YMP.GUI.ActionListener; 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 TestActionListener2 { public static void main(String[] args) { Frame frame=new Frame(); Button button1=new Button("Start" ); Button button2=new Button("Stop" ); button1.addActionListener(new MyMonitor()); button2.addActionListener(new MyMonitor()); button2.setActionCommand("bton2-stop");//这里设置了ActionCommand frame.add(button1,BorderLayout.NORTH); frame.add(button2,BorderLayout.SOUTH); frame.setSize(200,200); frame.setVisible(true); windowClose(frame); } private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class MyMonitor implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //e.getActionCommand(); 获得按钮信息ActionCommand,若不加修改则默认为按钮label System.out.println(e.getActionCommand()); //这里在控制台输出了Command //通过if语句判断两个按钮,使得两个按钮共用一个监听 if(e.getActionCommand().equals("Start")){ System.out.println("Start按钮"); } if(e.getActionCommand().equals("bton2-stop")){ System.out.println("Stop按钮"); } } }
3.输入框监听事件(TextField)
package YMP.GUI.ActionListener; 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 TestTextField { public static void main(String[] args) { new MyFrame(); windowClose(new MyFrame()); } private static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class MyFrame extends Frame { public MyFrame() { TextField textField = new TextField();//创建文本框对象 add(textField); textField.addActionListener(new MyTextActionListener()); setSize(500, 80); setVisible(true); //设置置换编码 // textField.setEchoChar('*'); } } class MyTextActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { TextField field = (TextField) e.getSource();//获得资源,返回对象 System.out.println(field.getText());//输出文本框内容 field.setText("");//设置回车后文本框清空 } }
4.简易计算器
制作以下图片所示简易计算器
过程设计
1.图示计算器是由3个text field,一个label和一个button组合
2.第一,二个文本框用于接收用户输入信息,button按下在第三个文本框输出结果
代码实现
![屏幕截图 2024-06-12 203920](C:\Users\youmu\Pictures\Screenshots\屏幕截图 2024-06-12 203920.png)package YMP.GUI.ActionListener; 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 TestCalc { public static void main(String[] args) { new Calculator(); } } class Calculator extends Frame{ public Calculator(){ TextField T1=new TextField(10);//columns 可输入最大个数 TextField T2=new TextField(10); TextField T3=new TextField(20); Button button=new Button("="); Label label=new Label("+"); setLayout(new FlowLayout()); add(T1); add(label); add(T2); add(button); add(T3); pack(); setVisible(true); button.addActionListener(new MyCalculatorListenner(T1,T2,T3)); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class MyCalculatorListenner implements ActionListener{ private TextField T1,T2,T3; public MyCalculatorListenner(TextField T1,TextField T2,TextField T3){ this.T1=T1; this.T2=T2; this.T3=T3; } @Override public void actionPerformed(ActionEvent e) { int n1=Integer.parseInt(T1.getText()); int n2=Integer.parseInt(T2.getText()); T3.setText(""+(n1+n2)); T1.setText(""); T2.setText(""); } }
代码优化,完全面向对象
既然我们使用的是java,就该运用java的特长面向对象
package YMP.GUI.ActionListener; 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 TestCalc { public static void main(String[] args) { new Calculator().loadFrame(); } } class Calculator extends Frame{ //属性 TextField T1,T2,T3; //方法 public void loadFrame(){ T1=new TextField(10);//columns 可输入最大个数 T2=new TextField(10); T3=new TextField(20); Button button=new Button("="); Label label=new Label("+"); setLayout(new FlowLayout()); add(T1); add(label); add(T2); add(button); add(T3); pack(); setVisible(true); button.addActionListener(new MyCalculatorListenner(this)); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } public Calculator(){ } } class MyCalculatorListenner implements ActionListener{ //获得计算器对象,在一个类中组合另一个类 Calculator calculator=null; public MyCalculatorListenner(Calculator calculator){ this.calculator=calculator; } @Override public void actionPerformed(ActionEvent e) { int n1=Integer.parseInt(calculator.T1.getText()); int n2=Integer.parseInt(calculator.T2.getText()); calculator.T3.setText(""+(n1+n2)); calculator.T1.setText(""); calculator.T2.setText(""); } }
两个代码效果都是一样的,但是第二个完全面向对象,第一个面向过程。
高级!!!