在Swing事件机制所采用的设计模式:适配器模式
- 定义一个接口:在接口中定义所需要的规范(方法)
- 定义一个抽象类 抽象类实现了接口,抽象类中对接口中的方法都是空实现
- 在使用的时候,不需要去实现接口(不需要去实现接口中的所有的方法),去继承抽象类适配器,需要那个方法,就重写那个方法
在实际开发中,事件的添加
- 需要知道事件源
- 需要知道该组件需要绑定的是那种事件
- 一般使用匿名内部类去实现事件(适配器)
package org.lanqiao.loginui;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginUI {
JFrame loginFrame;
JPanel loginPanel;
JTextField username;
JPasswordField password;
JLabel codeImg = new JLabel();
private String uname;
private String pw;
JButton bt = new JButton("按钮");
public LoginUI() {
loginFrame = new JFrame();
loginPanel = new JPanel();//使用空布局
username = new JTextField();
password = new JPasswordField();
}
public void init() {
loginFrame.setSize(650, 400);
loginFrame.setIconImage(new ImageIcon("image/title.gif").getImage());
loginFrame.setLocationRelativeTo(null);
loginFrame.setResizable(false);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginPanel = new LoginPanel("image/login1.jpg");
loginPanel.setLayout(null);
username.setBounds(390, 157, 192, 25);
password.setBounds(390, 191, 192, 25);
bt.setBounds(300, 120, 120, 25);
Map<String,Object> codeAndPic = CodeUtil.generateCodeAndPic();
codeImg.setIcon(new ImageIcon((Image)codeAndPic.get("codePic")));
codeImg.setBounds(502, 235, 90, 20);
loginPanel.add(codeImg);
loginPanel.add(bt);
loginPanel.add(username);
loginPanel.add(password);
loginFrame.add(loginPanel);
loginFrame.setVisible(true);
//当鼠标移动进入登陆按钮区域时,改变鼠标的形状
loginFrame.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();//获取鼠标当前所处位置的x坐标
int y = e.getY();
if(x > 397 && x < 504 && y > 308 && y < 336) {
Cursor c = new Cursor(Cursor.HAND_CURSOR);
loginFrame.setCursor(c);
}else {
loginFrame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
});
//当鼠标进入到登陆按钮区域时,监听鼠标单击事件
loginFrame.addMouseListener(new MouseAdapter() {
/*
* 鼠标单击事件
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
*/
@Override
public void mouseClicked(MouseEvent e) {
uname = username.getText();
pw = new String(password.getPassword());
int x = e.getX();//获取鼠标当前所处位置的x坐标
int y = e.getY();
if(x > 397 && x < 504 && y > 308 && y < 336) {
String loginRes = new LoginController().login(uname, pw);
if("用户名密码为空".equals(loginRes)) {
JOptionPane.showMessageDialog(loginFrame, "用户名或密码不能为空","Inane warning",
JOptionPane.WARNING_MESSAGE);
}else if("用户名密码有误".equals(loginRes)) {
JOptionPane.showMessageDialog(loginFrame, "用户名密码有误","Inane warning",
JOptionPane.WARNING_MESSAGE);
}
else {
JOptionPane.showMessageDialog(loginFrame, "登陆成功","Inane warning",
JOptionPane.WARNING_MESSAGE);
loginFrame.setVisible(false);
}
}
}
});
bt.addMouseListener(new MouseAction());
username.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
System.out.println(username.getText());
}
});
password.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
/*int keyCode = e.getKeyCode();
String keyText = e.getKeyText(keyCode);
System.out.println(keyCode+"----"+keyText);*/
//当按下的a键的时候,输出"你好"
if(KeyEvent.VK_A == e.getKeyCode()) {
System.out.println("你好");
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
});
}
public static void main(String[] args) {
LoginUI ui = new LoginUI();
ui.init();
}
}
class MouseAction extends MouseAdapter{
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("按钮被点击了");
}
}