JavaGUI监听事件
事件监听
package com.JavaGUI.Demo02;
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()需要一个ActionListener,所以我们需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button, BorderLayout.CENTER);
frame.pack();
frame.setBounds(50, 50, 400, 400);
frame.setVisible(true);
windowsClose(frame);
}
//关闭窗口事件
private static void windowsClose(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("aaaaa");
}
}
多个按钮监听一个事件
package com.JavaGUI.Demo02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionTwo {
public static void main(String[] args) {
Frame frame = new Frame("开始-结束");
Button button1 = new Button("button1");
Button button2 = new Button("button2");
//可以显示的多个定义触发会返回的命令,如果不显示定义,则会走默认的值!
//可以多个按钮只写一个监听类
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1, BorderLayout.NORTH);
frame.add(button2, BorderLayout.SOUTH);
frame.setBounds(50, 50, 400,400);
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//getActionCommand 获得按钮的信息
System.out.println("按钮触发getActionCommand的作用为====>"+e.getActionCommand());
}
}
文本框
package com.JavaGUI.Demo02;
import org.w3c.dom.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
public static void main(String[] args) {
//一个程序的main方法里面应该只有简单的启动代码!!!
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
//TextField表示文本框,不能换行,TextArea表示文本域,可换行
TextField textField = new TextField();
add(textField);
//监听文本框输入的文字 enter接收
MyActionListerTextField myActionListerTextField = new MyActionListerTextField();
textField.addActionListener(myActionListerTextField);
//设置替换编码---可以类似输入密码变成* 的这种、
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListerTextField implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField textField = (TextField) e.getSource();//获得一些资源,返回一个对象
System.out.println(textField.getText());//获得输入文本框的内容
textField.setText("");//清空文本框,不能使用 null【对象的用法】
}
}
写一个简易计算
- 初始代码框架
package com.JavaGUI.Demo02;
import org.w3c.dom.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCala {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
public Calculator(){
//三个文本框
TextField textField1 = new TextField(10);//表示的是字符数
TextField textField2 = new TextField(10);//表示的是字符数
TextField textField3 = new TextField(20);//表示的是字符数
//一个按钮
Button button = new Button("=");
//添加监听事件
MyCalculatorLister myCalculatorLister = new MyCalculatorLister(textField1, textField2, textField3);
button.addActionListener(myCalculatorLister);
//一个标签
Label label = new Label("+");
//流式布局
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
}
class MyCalculatorLister implements ActionListener{
//获取三个变量
private TextField num1, num2, num3;
public MyCalculatorLister(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.JavaGUI.Demo02;
import org.w3c.dom.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCala {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
class Calculator extends Frame{
//属性
TextField num1, num2, num3;
//方法
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
Button button = new Button("=");
Label label = new Label("+");
//添加监听事件
button.addActionListener(new MyCalculatorLister(this));
//流式布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorLister implements ActionListener{
//获取计算器这个对象,在一个类中组合另外一个类
Calculator calculator = null;
//组合的概念
public MyCalculatorLister(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
//2.将这个值进行+运算后,赋值给第三个数
//3.清除前两个数
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
calculator.num3.setText(""+(n1+n2));
calculator.num1.setText("");
calculator.num2.setText("");
}
}
- 内部类
/*内部类*/
package com.JavaGUI.Demo02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCala {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
class Calculator extends Frame{
TextField num1, num2, num3;
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
Button button = new Button("=");
Label label = new Label("+");
//添加监听事件
button.addActionListener(new MyCalculatorLister());
//流式布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
private class MyCalculatorLister implements ActionListener{
@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("");
}
}
}