Java接口事件

点击QQ登录窗口上的登录按钮后,如果输入的账号密码正确则打开一个新窗口,关闭登录窗口,如错误,则输出错误警报

使用工具:eclipse

需要QQ登录窗口代码,以下给出,详细请见上一篇java实现图形界面

QQ登录窗口代码

package jiemian;



import java.awt.Dimension;

import java.awt.FlowLayout;



import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;



public class Login {



public static void main(String[] args) {

Login log=new Login();

log.InitUI();

}

public void InitUI()

{

JFrame frame=new JFrame();

frame.setTitle("Login");

frame.setSize(540,427);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(3);

FlowLayout f1=new FlowLayout(FlowLayout.LEFT);

frame.setLayout(f1);

ImageIcon imag1=new ImageIcon("H:/1.png");

JLabel pic1=new JLabel(imag1);

frame.add(pic1);

JLabel name1=new JLabel();

name1.setPreferredSize(new Dimension(110,30));

frame.add(name1);

JLabel name=new JLabel("账号:");

frame.add(name);

JTextField nametext=new JTextField();

nametext.setPreferredSize(new Dimension(220, 30));

frame.add(nametext);

JLabel name2=new JLabel();

name2.setPreferredSize(new Dimension(110,30));

frame.add(name2);

JLabel name3=new JLabel();

name3.setPreferredSize(new Dimension(110,30));

frame.add(name3);

JLabel password=new JLabel("密码:");

frame.add(password);

JPasswordField passwordtext=new JPasswordField();

passwordtext.setPreferredSize(new Dimension(220, 30));

frame.add(passwordtext);

JLabel name4=new JLabel();

name4.setPreferredSize(new Dimension(110,30));

frame.add(name4);

JLabel name5=new JLabel();

name5.setPreferredSize(new Dimension(220,30));

frame.add(name5);

JButton bu=new JButton("登录");

bu.setPreferredSize(new Dimension(80,30));

frame.add(bu);

frame.setVisible(true);
}
}

接口事件

一:事件监听机制

1.事件源对象

根据你的动作来决定,你的动作发生哪一个组件上那么该组件就是事件源对象。

此事件要求在点击登录按钮之后发生页面跳转,则登录按钮就是事件源对象

2.事件监听方法

能够对动作的发生产感应

(1)addActionListener(ActionListener l);

动作监听方法,用来捕获类似输入框事件源对象上的键盘回车动作和类似按钮事件源对象上的鼠标点击动作,然后收集事件源对象上的相关的信息,然后将动作和信息交给监听方法的参数ActionListener的对象进行处理。

(2)addMouseListener(MouseListener l);

鼠标监听方法,用来捕获事件源对象上鼠标的按下,释放,点击,进入和离开动作,然后收集事件源对象上的相关信息以及动作信息,最后将信息交给监听方法的参数MouseListener的对象进行处理。

(3)addMouseMotionListener(MouseMotionListener l);

鼠标移动监听方法,用来捕获事件源对象上鼠标的移动和拖动动作,然后收集事件 源对象上的相关信息以及动作信息,最后将信息交给监听方法的参数 MouseMotionListener的对象进行处理。

3.事件接口(事件处理类)

事件由监听方法捕捉后,由事件处理类进行处理,其中的ActionListener,MouseListener,MouseMotionListener为接口,例如监听方法捕捉到点击登录按钮的动作后,新窗口的打开由事件处理类处理

二.接口

类实现(继承)接口的格式

类实现(继承)接口的关键字:implements

格式:

public class 类名 extends 类名 implements 接口,… {

}

注意:类实现(继承)接口后,类必须要实现(重写)接口中所有的抽象方法。

三.具体步骤

1.第一个功能:点击登录按钮后,打开一个新窗口

(1)接口类

接口不能创建对象,则必须创建一个类(LoginLisen)继承接口,由此类可创建对象,实现接口中的抽象方法,必须在此类中把继承的接口中的所有抽象方法进行重写

以下代码不对窗口的新建进行解释,详细参考上一篇《java实现图形界面》

package jiemian;

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

import javax.swing.JFrame;

//创建名为 LoginLisen的类继承接口ActionListener,此处事件源对象为登录按钮,则使用接口ActionListener,继承的关键字为implements,需要添加的包名为import java.awt.event.ActionListener;
public class LoginLisen implements ActionListener{
//actionPerformed为接口ActionListener中的唯一抽象方法,必须对接口中所有抽象方法进行重写,此处将actionPerformed重写,需要添加的包名为import java.awt.event.ActionEvent;
public void actionPerformed(ActionEvent e)
{
//动作发生之后要执行的为新建一个窗口,以下新建名为Draw的窗口,设置窗口属性
JFrame frame=new JFrame();
frame.setTitle("Draw");
frame.setSize(540,427);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
}
}

(2)至此点击动作发生之后的执行方法已经完成,然后我们需要在QQ登录窗口类Login中用LoginLisen在其中创建对象,并添加监听方法,再将此对象作为监听方法的参数,之后当点击登录按钮的动作发生之后,监听方法会捕捉到,并自动执行新窗口的新建

注:具体代码在上面QQ登录窗口代码中用紫色字体标注,并附带注释

2.第二个功能:点击登录按钮后,打开新窗口,关闭旧窗口,此功能依然在LoginLisen中实现,其中有注释的为新加的语句

关闭旧窗口的操作应该在监听器捕捉到动作之后执行,所以应该在LoginLisen的actionPerformed方法中执行关闭操作

package jiemian;

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

import javax.swing.JFrame;

public class LoginLisen implements ActionListener{
//用于接受传入的窗口对象
public JFrame f;

//需要关闭的窗口为Login类中创建的,则需要把Login中的窗口对象传入到LoginListen中,在Login中调用getFrame函数可将Login中的窗口对象传入LoginListen中,由f保存,此时的窗口f就为Login中的QQ登录窗口
public void getFrame(JFrame fr)
{
f=fr;
}
public void actionPerformed(ActionEvent e)
{
JFrame frame=new JFrame();
frame.setTitle("Draw");
frame.setSize(540,427);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
//用f调用窗口关闭方法dispose,登录窗口关闭
f.dispose();
}
}

注:具体代码在上面QQ登录窗口代码中用蓝色字体标注,并附带注释

3.第三个功能:验证输入账号密码,如果正确,则打开新窗口关闭旧窗口,如果不正确,输出“错误”,窗口不变

同以上关闭窗口的操作,需要把JTextField和JPasswordField账号和密码文本框对象传入LoginListen,并获取其中的输入内容与正确的账号密码进行比较

新加的语句带注释

package jiemian;

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

import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginLisen implements ActionListener{
public JFrame f;

//接受密码框对象
public JPasswordField pp;

//接受文本框对象
public JTextField tt;

//需要把Login中的文本框对象传入到LoginListen中,在Login中调用getT函数可将Login中的窗口对象传入LoginListen中,由tt保存,此时的文本框tt就为Login中的文本框nametext
public void getT(JTextField t)
{
tt=t;
}
//同上
public void getP(JPasswordField p)
{
pp=p;
}
public void getFrame(JFrame fr)
{
f=fr;
}
public void actionPerformed(ActionEvent e)
{
//getPassword获取密码框输入内容,其返回值为char[]类型,新建string对象passwrod存储
String passwrod = new String(pp.getPassword());
//getText获取文本框输入内容,其返回值为String对象,不可与”qwe”直接比较,需要用到equals函数,password同
if(tt.getText().equals("qwe")&&passwrod.equals("123"))
{
JFrame frame=new JFrame();
frame.setTitle("Draw");
frame.setSize(540,427);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
f.dispose();
}
//如不相同输出错误
else
{
System.out.println("错误!");
}
}
}

注:具体代码在上面QQ登录窗口代码中用绿色字体标注,并附带注释

完整代码

Login类

package jiemian;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login {

public static void main(String[] args) {
Login log=new Login();
log.InitUI();
}
public void InitUI()
{
JFrame frame=new JFrame();
frame.setTitle("Login");
frame.setSize(540,427);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
FlowLayout f1=new FlowLayout(FlowLayout.LEFT);
frame.setLayout(f1);
ImageIcon imag1=new ImageIcon("H:/1.png");
JLabel pic1=new JLabel(imag1);
frame.add(pic1);

JLabel name1=new JLabel();
name1.setPreferredSize(new Dimension(110,30));
frame.add(name1);

JLabel name=new JLabel("账号:");
frame.add(name);

JTextField nametext=new JTextField();
nametext.setPreferredSize(new Dimension(220, 30));
frame.add(nametext);

JLabel name2=new JLabel();
name2.setPreferredSize(new Dimension(110,30));
frame.add(name2);

JLabel name3=new JLabel();
name3.setPreferredSize(new Dimension(110,30));
frame.add(name3);

JLabel password=new JLabel("密码:");
frame.add(password);

JPasswordField passwordtext=new JPasswordField();
passwordtext.setPreferredSize(new Dimension(220, 30));
frame.add(passwordtext);

JLabel name4=new JLabel();
name4.setPreferredSize(new Dimension(110,30));
frame.add(name4);

JLabel name5=new JLabel();
name5.setPreferredSize(new Dimension(220,30));
frame.add(name5);

JButton bu=new JButton("登录");
bu.setPreferredSize(new Dimension(80,30));
frame.add(bu);

LoginLisen lo=new LoginLisen();
lo.getP(passwordtext);
lo.getT(nametext);
lo.getFrame(frame);
bu.addActionListener(lo);

frame.setVisible(true);
}

}


LoginListen类

package jiemian;

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

import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginLisen implements ActionListener{
public JFrame f;
public JPasswordField pp;
public JTextField tt;
public void getT(JTextField t)
{
tt=t;
}
public void getP(JPasswordField p)
{
pp=p;
}
public void getFrame(JFrame fr)
{
f=fr;
}
public void actionPerformed(ActionEvent e)
{
String passwrod = new String(pp.getPassword());
if(tt.getText().equals("qwe")&&passwrod.equals("123"))
{
JFrame frame=new JFrame();
frame.setTitle("Draw");
frame.setSize(540,427);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
f.dispose();
}
else
{
System.out.println("错误!");
}
}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值