快捷搜索:

Java Bean 事件处理(摸板)

/*

* LoginEvent.java

*/

package beanTest;

import java.util.EventObject;

public class LoginEvent extends EventObject {

private static final long serialVersionUID = 1L;

private String userName;

private String password;

public LoginEvent(Object o, String userName, String password) {

super(o);

this.userName = userName;

this.password = password;

}

public String getPassword() {

return password;

}

public String getUserName() {

return userName;

}

public void setPassword(String password) {

this.password = password;

}

public void setUserName(String userName) {

this.userName = userName;

}

}

/**

* LoginListener.java

*/

package beanTest;

import java.util.EventListener;

/**

* @author wuliqiang

*

*/

public interface LoginListener extends EventListener {

public void validateLogin(LoginEvent e);

}

/**

* LoginBean.java

*/

package beanTest;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

/**

* @author wuliqiang

*

*/

public class LoginBean extends JPanel implements ActionListener {

private static final long serialVersionUID = 1L;

private JPasswordField textPassword;

private JTextField textUsername;

private JButton loginButton;

private LoginListener loginListener;

private LoginEvent loginEvent;

public LoginBean() {

super();

setLayout(null);

final JLabel loginNameLabel = new JLabel();

loginNameLabel.setText("Login Name:");

loginNameLabel.setBounds(82, 44, 70, 18);

add(loginNameLabel);

textUsername = new JTextField();

textUsername.setBounds(176, 42, 108, 22);

add(textUsername);

final JLabel passwordLabel = new JLabel();

passwordLabel.setText("Password:");

passwordLabel.setBounds(82, 94, 70, 18);

add(passwordLabel);

textPassword = new JPasswordField();

textPassword.setBounds(176, 92, 108, 22);

add(textPassword);

loginButton = new JButton();

loginButton.setText("Login");

loginButton.setBounds(140, 140, 77, 28);

loginButton.addActionListener(this);

add(loginButton);

}

public void actionPerformed(ActionEvent e) {

//初始化 loginEvent

loginEvent = new LoginEvent(loginButton, textUsername.getText(),

new String(textPassword.getPassword()));

fireLoginEvent(loginEvent);

}

public void addLoginListener(LoginListener loginListener) {

// 认真初始化 loginListener;

this.loginListener = loginListener;

}

public void fireLoginEvent(LoginEvent loginEvent) {

// 调用接口的措施

loginListener.validateLogin(loginEvent);

}

}

/**

* MainFrame.java

*/

package beanTest;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

/**

* @author wuliqiang

*

*/

public class MainFrame extends JFrame implements LoginListener {

private static final long serialVersionUID = 1L;

private LoginBean loginBean;

public MainFrame() {

super("my test MainFrame");

loginBean = new LoginBean();

loginBean.addLoginListener(this);

getContentPane().add(loginBean);

setSize(400, 500);

setVisible(true);

}

public static void main(String[] args) {

MainFrame app = new MainFrame();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void validateLogin(LoginEvent e) {

JOptionPane.showMessageDialog(this, "userName:" + e.getUserName()

+ "\n" + "Password:" + new String(e.getPassword()));

}

}

您可能还会对下面的文章感兴趣: