事件监听
1. 通过添加 addWindowListener 和 addActionListener 监听窗口的关闭事件和按钮的click事件。
package com.langtao.base.demo3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.function.DoubleToIntFunction;
public class Application {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(300,400);
frame.setBackground(Color.GRAY);
frame.setLayout(new FlowLayout());
Button btn1 = new Button("btn1");
btn1.addActionListener(new myActionListener());
frame.add(btn1);
windowClose(frame);
}
public 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("Hello, you are clicking the button." + this.toString());
}
};
2. 多button绑定ActionListener,监听事件。
package com.langtao.base.demo4;
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 Application {
public static void main(String[] args) {
Frame frame = new Frame("Start-Stop");
Button btn1 = new Button("Start");
Button btn2 = new Button("Stop");
frame.setSize(300,500);
MyActionListener myActionListener = new MyActionListener();
btn1.addActionListener(myActionListener);
btn2.addActionListener(myActionListener);
frame.add(btn1, BorderLayout.NORTH);
frame.add(btn2, BorderLayout.SOUTH);
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 MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command == "Start"){
System.out.println("The game is starting...");
}else if(command == "Stop"){
System.out.println("Game over.");
}
}
}
3. TextField事件监听
package com.langtao.base.demo5;
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 Application {
public static void main(String[] args) {
CalFrame calFrame = new CalFrame();
}
}
class CalFrame extends Frame{
public CalFrame(){
TextField textField = new TextField();
CalTextActionListener calTextActionListener = new CalTextActionListener();
textField.addActionListener(calTextActionListener);
textField.setEchoChar('*');//设置输入显示的字符。
add(textField);
pack();
setVisible(true);
windowClose(this);
}
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class CalTextActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//TextField 敲击enter时的事件监听
TextField textField = (TextField)e.getSource();
System.out.println(textField.getText());
textField.setText("");//设置TextField文本为空。
}
}
4. 简易加法计算器
package com.langtao.base.demo6;
public class Application {
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
package com.langtao.base.demo6;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Calculator extends Frame{
public Calculator() {
//1. 一个计算器的窗口
//2. 3个TextField
TextField num1 = new TextField();
TextField num2 = new TextField();
TextField sum = new TextField();
num1.setColumns(10);
num2.setColumns(10);
sum.setColumns(11);
//3. 1个Button,等于号
Button btn = new Button("=");
btn.addActionListener(new CalActionListener(num1, num2, sum));
//4. 1个Label放加号
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(btn);
add(sum);
pack();
setVisible(true);
windowClose(this);
}
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
package com.langtao.base.demo6;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalActionListener implements ActionListener {
private TextField num1;
private TextField num2;
private TextField num3;
public CalActionListener(TextField a, TextField b, TextField c) {
num1 = a;
num2 = b;
num3 = c;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
5.简易计算器的代码重构1--OOP
package com.langtao.base.demo7;
import com.langtao.base.demo6.Calculator;
public class Application {
public static void main(String[] args) {
Calculator2 calculator = new Calculator2();
calculator.loadCalculator();
Calculator2.windowClose(calculator.getFrame());
}
}
package com.langtao.base.demo7;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Calculator2 {
//属性
private Frame frame = new Frame();
private TextField num1 = new TextField();
private TextField num2 = new TextField();
private TextField result = new TextField();
private Label label = new Label("+");
private Button button = new Button("=");
public void setNum1(TextField num1) {
this.num1 = num1;
}
public void setNum2(TextField num2) {
this.num2 = num2;
}
public void setResult(TextField result) {
this.result = result;
}
public TextField getNum1() {
return num1;
}
public TextField getNum2() {
return num2;
}
public Frame getFrame() {
return frame;
}
public TextField getResult() {
return result;
}
//方法
public void loadCalculator(){
button.addActionListener(new CalculatorActionListener(this));
this.num1.setColumns(10);
this.num2.setColumns(10);
this.result.setColumns(11);
frame.setLayout(new FlowLayout());
frame.add(num1);
frame.add(label);
frame.add(num2);
frame.add(button);
frame.add(result);
frame.pack();
frame.setVisible(true);
}
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
package com.langtao.base.demo7;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalculatorActionListener implements ActionListener {
private Calculator2 calculator = new Calculator2();
public CalculatorActionListener(Calculator2 calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(calculator.getNum1().getText());
int n2 = Integer.parseInt(calculator.getNum2().getText());
calculator.getResult().setText("" + (n1 + n2));
calculator.getNum1().setText("");
calculator.getNum2().setText("");
}
}
6. 计算器代码重构--OOP内部类
package com.langtao.base.demo8;
public class Application {
public static void main(String[] args) {
Calculator3 calculator3 = new Calculator3();
calculator3.calculatorLoad();
calculator3.windowClose();
}
}
package com.langtao.base.demo8;
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 Calculator3 {
//属性
Frame frame = new Frame();
TextField num1 = new TextField();
TextField num2 = new TextField();
TextField result = new TextField();
Label label = new Label("+");
Button button = new Button("=");
//方法
public void calculatorLoad(){
frame.add(num1);
frame.add(label);
frame.add(num2);
frame.add(button);
frame.add(result);
num1.setColumns(10);
num2.setColumns(10);
result.setColumns(11);
frame.setSize(300,500);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
button.addActionListener(new CalculatorActionListener1());
}
public void windowClose(){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//内部类
class CalculatorActionListener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
result.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}

浙公网安备 33010602011771号