java监听器ActionListener 的入门小应用

在我们学习java图形化用户页面的时候我们就会使用各种监听器

ActionListener就是一个,当我们想实现例如点击 登录 就去判断账号和密码是否正确来判断下一步时我们就会用可以使用这个监听器来监听你是否点击了登录来判断下一步

是否来判断点击登录后密码和账号是否正确


    @Override       
    public void actionPerformed(ActionEvent e) {
       // String s=new String(this.jPPass.getPassword());
        if("admin".equals(this.jUser.getText())&&"12345".equals(new String(this.jPPass.getPassword()))){
            this.jframe.dispose();
        }else{
            this.jUser.setText((String)null);
            this.jPPass.setText((String)null);
            this.jUser.requestFocus();
        }
    }
}

监听器的外部定义

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class manager  implements ActionListener{
    private JFrame jframe;
    private JTextField jUser;
    private JPasswordField jPPass;
  //  private JButton jb1;
    private manager a;
    public manager(){}
    public manager(JTextField jTfUser, JPasswordField jPfPass, JFrame jFrame){
        this.jframe=jFrame;
        this.jUser=jTfUser;
        this.jPPass=jPfPass;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
       // String s=new String(this.jPPass.getPassword());
        if("admin".equals(this.jUser.getText())&&"12345".equals(new String(this.jPPass.getPassword()))){
            this.jframe.dispose();
        }else{
            this.jUser.setText((String)null);
            this.jPPass.setText((String)null);
            this.jUser.requestFocus();
        }
    }
}

当我们想让他监听某个按钮时

JButton jbtLogin;
manager b;
this.b=new manager(this.jTfUser,this.jPfPass,this.jFrame); this.jbtLogin.addActionListener(b);
import java.awt.*;
import javax.swing.*;
public class app{
     Icon[] faces;
    JFrame jFrame;
    JLabel jlbUser;
    JLabel jlbPass;
    JTextField jTfUser;
    JPasswordField jPfPass;
    JButton jbtLogin;
    JButton jbtCancel;
    JPanel jp1;
    JPanel jp2;
    JPanel jp3;
    JPanel jp4;
    Container c;
    manager b;
    public app(){
        faces=new Icon[]{
                new ImageIcon("src/img/登录1.png"),
                new ImageIcon("src/img/取消1.png"),
        };
        this.jFrame=new JFrame();
        this.jlbUser=new JLabel();
        this.jFrame.setBounds(200,200,300,200);
        this.jFrame.setBackground(Color.blue);
        this.jlbUser=new JLabel("用户");
        this.jlbPass=new JLabel("密码");
        this.jTfUser=new JTextField(14);
        this.jPfPass=new JPasswordField(14);
        this.jbtLogin=new JButton("登录");
        this.jbtLogin.setIcon(faces[0]);
        this.jbtCancel=new JButton("取消");
        this.jbtCancel.setIcon(faces[1]);
        this.jp1=new JPanel();
        this.jp1.setOpaque(false);
        this.jp2=new JPanel();
        this.jp2.setOpaque(false);
        this.jp3=new JPanel();
        this.jp3.setOpaque(false);
        this.jp4=new JPanel();
        this.jp4.setOpaque(false);
        this.jp1.add(this.jlbUser);
        this.jp1.add(this.jTfUser);
        this.jp2.add(this.jlbPass);
        this.jp2.add(this.jPfPass);
        this.jp3.add(jbtLogin);
        this.jp3.add(this.jbtCancel);
        this.jp4.add(this.jp1);
        this.jp4.add(this.jp2);
        this.jp4.add(this.jp3);
        this.jFrame.add(this.jp4);
        this.jFrame.setBackground(Color.blue);
        this.jFrame.setVisible(true);
        this.c=jFrame.getContentPane();
        this.c.setBackground(Color.cyan);
        this.b=new manager(this.jTfUser,this.jPfPass,this.jFrame);
        this.jbtLogin.addActionListener(b);
    }
}

当你将一个类用作事件监听器时,你已经设置好一个特定的事件类型,它会用该类进行监听。接下来的操作是:一个匹配的监听器必须被加入到该组件中 组件被创建之后,可以在组件上调用如下方法来将监听器与它联系起来 addActionListener( ) 可用于 Button,Check, TexyField 等组件 addAdjustmentListener( ) 可用于 ScrollBar 组件 addFocusListener( ) 可用于所有可视化组件 addItemListener( ) 可用于 Button,CheckBox 等组件 addKeyListener( ) 可用于所有可视化组件 addMouseListener( ) 可用于所有可视化组件 addMouseMotionListener( ) 可用于所有可视化组件 addWindowsListener( ) 可用于 Window,Frame等组件 例:下面语句创建一个 Button 对象,并将它与动作事件监听器联系起来 Button button = new Button( “OK” ) ; button . addActionListener ( this ) ; //this 指明当前类就是事件监听器 2 使用方法 (1)动作事件(ActionEvent) 发生在用户完成了使用下列组件之一的动作: Button , CheckBox , ComboBox , TextField , RadioButton 类为了能够处理这些事件必须实现接口 ActionListener 。 每个产生一个动作事件的组件上要调用方法 addActionListener( ) 方法 actionPerformed(ActionEvent evt) 是接口 ActionListener 惟一的 方法采用如下格式: public void actionPerformed(ActionEvent evt) { //…… } 在 ActionEvent 对象上可以使用的方法: getSource( ) 获取产生事件组件的组件名 getActionCommand( ) 获取与组件有关的文本,如命令按钮的标签 键盘事件(KeyEvent) 发生在键盘上的某个键被按下时。 类为了能够处理这些事件必须实现接口 KeyListener 。 每个产生一个键盘事件的组件上要调用方法 addKeyListener( ) 在接口 KeyListener 中有三个方法: public void keyPressed(KeyEvent evt) { //…… } public void keyReleased(KeyEvent evt) { //…… } public void keyTyped(KeyEvent evt) { //…… } 在 KeyEvent 对象上可以使用的方法: getKeyChar( ) 返回与事件相关的键盘字符的 Unicode 码 . . . . . . . . . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值