2.4事件监听
package com.ym.lesson02;
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 ActionEventTest {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button();
//因为addActionListener()需要一个ActionListener,
//所以构造了一个MyActionListener类
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
windowClose(frame);
}
//关闭窗口方法
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("111");
}
}
2.4.1 多个按钮共享一个事件
package com.ym.lesson02;
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 ActionEventTest2{
public static void main(String[] args) {
Frame frame = new Frame();
Button btn1 = new Button("start");
Button btn2 = new Button("stop");
btn1.setActionCommand("btn1_start");//设置标签值
MyListener myListener = new MyListener();
btn1.addActionListener(myListener);
btn2.addActionListener(myListener);
frame.add(btn1,BorderLayout.NORTH);
frame.add(btn2,BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}
static class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());//getActionCommand()得到按钮上的label
if(e.getActionCommand().equals("btn1_start")){
System.out.println("点击了开始btn1_start");
}else{
System.out.println("点击了结束stop");
}
}
}
}
2.5,输入框TextField
package com.ym.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextFieldTest {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
MyTextListener myTextListener = new MyTextListener();
//按下回车键就会触发事件
textField.addActionListener(myTextListener);
//输入框中的文本替换为*,但控制台显示的是文本,不是*
textField.setEchoChar('*');
pack();
setVisible(true);
}
}
class MyTextListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource());//e.getSource()得到资源,返回object
TextField textField = (TextField) e.getSource();
System.out.println(textField.getText());//获得输入框中的文本
textField.setText("");//按下回车就会清空输入框中的内容
}
}
2.6,简易计算器
package com.ym.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class CalcTest {
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
//计算器类
class Calculator extends Frame {
public Calculator(){
//三个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
//一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//一个标签
Label label = new Label("+");
//流式布局,FlowLayout
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.ym.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class CalcTest {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.loadFrame();
}
}
//计算器类
class Calculator extends Frame {
//属性
TextField num1;
TextField num2;
TextField num3;
//方法
public void loadFrame(){
//三个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
//一个按钮
Button button = new Button("=");
//为按钮添加监听事件
button.addActionListener(new MyCalculatorListener(this));
//一个标签
Label label = new Label("+");
//流式布局,FlowLayout
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,获得加数和被加数
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
//2,加法运算后将得数放到第三个框中
calculator.num3.setText(""+(n1+n2));
//3,清除前两个框中的内容
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类
package com.ym.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class CalcTest {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.loadFrame();
}
}
//计算器类
class Calculator extends Frame {
//属性
TextField num1;
TextField num2;
TextField num3;
//方法
public void loadFrame(){
//三个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
//一个按钮
Button button = new Button("=");
//为按钮添加监听事件
button.addActionListener(new MyCalculatorListener());
//一个标签
Label label = new Label("+");
//流式布局,FlowLayout
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,获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2,加法运算后将得数放到第三个框中
num3.setText(""+(n1+n2));
//3,清除前两个框中的内容
num1.setText("");
num2.setText("");
}
}
}