用java的事件监听机制实现按钮和窗体的互动

一、事件源对象

所有的容器组件(比如窗体)和元素组件(比如按钮)都可以成为事件源,事件发生在哪,哪就是事件源。

二、事件监听方法

事件监听方法可以捕获事件源对象的动作,这些动作信息,会成为事件监听方法的参数对象。

比如动作事件监听方法ActionListener,可以捕获鼠标点击,输入框中回车等动作。

三、事件接口

事件接口是处理动作的接口。参数对象拿监听方法给的信息后,就会根据动作信息调用事件处理方法,然后执行方法中的代码进行动作的处理。

比如ActionListener中的actionPerformed(ActionEvent e)抽象方法

实例:点击登录界面上的登录按钮,根据用户输入的账号和密码进行验证,如果正确则显示一个新窗体接着关闭登录窗体;如果错误则显示一个错误的窗体。


写一个登录界面

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.FlowLayout;

public class Login extends LoginListener{
public static void main(String args[]){
Login lg = new Login();
lg.showframe();
}
public void showframe(){
//创建窗体(顶级容器)
JFrame loginframe = new JFrame();
loginframe.setTitle("Login");
loginframe.setSize(300,400);
loginframe.setLocation(300,300);
loginframe.setDefaultCloseOperation(3);
//设置布局方式
FlowLayout f1 = new FlowLayout();//流式布局
loginframe.setLayout(f1);
//添加图标                                                                     (\为转义符,\\为路径分隔)
ImageIcon image = new ImageIcon("C:\\Users\\lenovo\\Desktop\\background1.jpg");
JLabel label = new JLabel(image);
loginframe.add(label);
//添加用户名,密码框
JLabel labelName = new JLabel("账号:");
loginframe.add(labelName);
JTextField textName = new JTextField(20);
loginframe.add(textName);

JLabel labelPassword = new JLabel("密码:");
loginframe.add(labelPassword);
JPasswordField textPassword = new JPasswordField(20);
loginframe.add(textPassword);
//添加登录按钮
JButton button = new JButton("登录");
loginframe.add(button);

//设置窗体可见
loginframe.setVisible(true);
                //实例化监视器
LoginListener l1 = new LoginListener();
button.addActionListener(l1);

l1.setLogin(loginframe);//将登录窗体传递到l1对象的login属性
l1.setName(textName);

l1.setPassword(textPassword);

        }

}

写登录界面的监听类

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class LoginListener implements ActionListener{

private JFrame login;//声明登录窗体的属性
private JTextField name;//同理
private JTextField password;

//设置登录窗体(用户名,密码)属性值的方法
public void setLogin(JFrame login){
this.login = login;
}
public void setName(JTextField name){
this.name = name;
}
public void setPassword(JTextField password){
this.password = password;
}


public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame();
frame.setTitle("画图");
frame.setSize(700, 500);
frame.setDefaultCloseOperation(3);
//使窗体显示在正中央
frame.setLocationRelativeTo(null);
//获取输入框的内容,getText()必须在监视器中才能生效
String s1=name.getText();
String s2=password.getText();
//验证用户名密码是否正确
if(s1.equals("abc") && s2.equals("abc")){
frame.setVisible(true);
}
else{
JFrame wrongframe = new JFrame();
wrongframe.setSize(200,100);
wrongframe.setDefaultCloseOperation(3);
wrongframe.setLocationRelativeTo(null);
JButton button = new JButton("用户名或密码错误");
wrongframe.add(button);
wrongframe.setVisible(true);
}

//关闭登录窗体
login.dispose();
}
}

猜你喜欢

转载自blog.csdn.net/a784296404/article/details/80032521
今日推荐