关于JAVA单击事件

    在这里我只是简单的说一下,关于在JAVA的GUI中,如何使用事件监听的方法,至于其实现原理,还是请有兴趣的人,自己去查阅相关资料吧!
    下面给出一个登录窗体的代码示例,来看看这个监听的流程!
import java.awt.event.*;

import javax.swing.*;

/*
 * 实现简单的GUI控件,以及事件监听
 * Author  ajayumi
 * Date    2008-06-05
 
*/

public class Demo1 extends JFrame {

    
private JLabel lb1 = null;

    
private JLabel lb2 = null;

    
private JTextField txtNameUser = null;

    
private JPasswordField txtPwd = null;

    
private JButton btnSubmit = null;

    
private JButton btnCancel = null;

    
public Demo1() {
        
super("User Login");

        
this.lb1 = new JLabel("UserName:");
        
this.add(this.lb1);

        
this.txtNameUser = new JTextField(20);
        
this.add(this.txtNameUser);

        
this.lb2 = new JLabel("Password:");
        
this.add(this.lb2);

        
this.txtPwd = new JPasswordField(20);
        
this.add(this.txtPwd);

        
this.btnSubmit = new JButton("确定");
        
this.add(this.btnSubmit);

        
this.btnCancel = new JButton("取消");
        
this.add(this.btnCancel);

        
// 添加 确定 按钮的事件监听
        this.btnSubmit.addActionListener(new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                btnSumbit_Clicked(e);
            }

        }
);
        
// 添加 取消 按钮的事件监听
        this.btnCancel.addActionListener(new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                btnCancel_Clicked(e);
            }

        }
);

        
this.setSize(350120); // 设置窗体的初始大小
        this.setVisible(true); // 设置窗体显示
        
// 设置窗体的布局管理器
        this.setLayout(new java.awt.FlowLayout());
        
// 设置窗体关闭的操作
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }


    
protected void btnCancel_Clicked(ActionEvent e) {
        
// TODO 为 取消 按钮添加单击事件
        System.exit(0);
    }


    
protected void btnSumbit_Clicked(ActionEvent e) {
        
// TODO 为 确定 按钮添加单击事件
        String UserName = this.txtNameUser.getText();
        String Pwd 
= this.txtPwd.getText();
        JOptionPane.showMessageDialog(
this"UserName:" + UserName
                
+ "\nPassword:" + Pwd);
    }


    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        new Demo1();
    }


}


其中的
// 添加 确定 按钮的事件监听
        this.btnSubmit.addActionListener(new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                btnSumbit_Clicked(e);
            }

        }
);
这里是进行 确定 按钮的事件监听,其实要实现事件监听可以有多种方法,但是在这里,我比较推崇使用这种写法,因为这样的写法有点类似于.NET。从下面的方法调用,我们就能看到它与.NET的相似之处了。
protected void btnSumbit_Clicked(ActionEvent e) {
        
// TODO 为 确定 按钮添加单击事件
        String UserName = this.txtNameUser.getText();
        String Pwd 
= this.txtPwd.getText();
        JOptionPane.showMessageDialog(
this"UserName:" + UserName
                
+ "\nPassword:" + Pwd);
    }
使用C#的人,看到这里,就会发现,这里事件的实现,就差在参数列表少了一个object sender的参数,而其余结构非常相似。
    一个简单的登录窗口就完成了!如果有什么不明白,大家可以交流一下!弄清楚事件的触发机制,是学习JAVA所必须的!
posted @ 2008-06-05 13:36  ajayumi  阅读(701)  评论(0)    收藏  举报