事件委托模型

来源:互联网 发布:windows 模拟软件 编辑:程序博客网 时间:2024/05/16 20:28

    所有基于GUI的系统都是基于事件原理的。图形用户界面产生于20世纪80年代初,但直到90年代才成为程序设计人员选用的界面。


    在Java中,GUI就是事件生成器,每次鼠标的移动、每次击键、每次窗口大小调整意见每次窗口的重新定位,都会触发事件,应用程序需要捕获这些事件。


    从Java1.1开始,Sun在事件监听器概念基础上引入了一套增强事件处理策略。事件监听器是一个实现java.awt.EventListenter接口的类。 事件监听通常和Swing组件一起使用,如,一个简单的QQ登陆界面的代码:
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

import sun.awt.windows.WWindowPeer;


public class QQFrame extends JFrame implements ActionListener{

    private ImageIcon bannerImage,titleImage;
   
    private JLabel banner,account,regNewAccount,password,getPassword,loginState;
   
    private JComboBox accountValue,stateImage;
   
    private TextField passwordValue;
   
    private Checkbox rememberPassword,autoLogin;
   
    private JButton setButton,loginButton;
   
    public QQFrame(){
        //获取屏幕分辨率
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
       
        //设置顶级窗口
        this.setTitle("口口2009 绝世版");       
        this.setResizable(false);           
        this.setBounds(screenSize.width/2-170, screenSize.height/2-123, 340, 246);
        this.setDefaultCloseOperation(QQFrame.EXIT_ON_CLOSE);
        this.setIconImage(this.getToolkit().getImage("image//qq.gif"));
        this.setLayout(null);
        initcontainer();
        this.setVisible(true);
       
    }
   
    public void initcontainer(){
        Container container = new Container();
        container = this.getContentPane();
        container.setBackground(Color.WHITE);
//        FileOperation fo = new FileOperation();
//        for(int i = 0;i < fo.readAccount().length;i++){
//                System.out.println(fo.readAccount()[i]);
//        }
       
        //插入banner图片
        bannerImage = new ImageIcon("image//banner.jpg");
        banner = new JLabel(bannerImage);
        banner.setBounds(-2,-68,339,200);
        container.add(banner);
       
        account = new JLabel("账号:");  
        account.setFont(new Font("宋体",Font.PLAIN,12));
        account.setBounds(30,44,88,88);
        container.add(account);
       
        accountValue = new JComboBox();
        accountValue.setBounds(70,77,180,22);
        accountValue.setEditable(true);
        accountValue.addActionListener(this);
        container.add(accountValue);
       
        regNewAccount = new JLabel("注册新账号");
        regNewAccount.setFont(new Font("宋体",Font.PLAIN,12));
        regNewAccount.setBounds(258,77,185,22);
        container.add(regNewAccount);
       
        password = new JLabel("密码:");
        password.setFont(new Font("宋体",Font.PLAIN,12));
        password.setBounds(30,80,88,88);
        container.add(password);
       
        passwordValue = new TextField();
        passwordValue.setBounds(70,111,180,22);
        passwordValue.addActionListener(this);
        container.add(passwordValue);
       
        getPassword = new JLabel("取回密码");
        getPassword.setFont(new Font("宋体",Font.PLAIN,12));
        getPassword.setBounds(258,111,185,22);
        container.add(getPassword);
       
        loginState = new JLabel("状态:");
        loginState.setFont(new Font("宋体",Font.PLAIN,12));
        loginState.setBounds(30,113,88,88);
        container.add(loginState);
       
        stateImage = new JComboBox();
        titleImage = new ImageIcon("image//ys.jpg");
        stateImage.setBounds(66,151,40,16);
        stateImage.addItem(titleImage);
        stateImage.addItem("隐身");
        container.add(stateImage);
       
        rememberPassword = new Checkbox("记住密码");
        rememberPassword.setBounds(110,138,78,40);
        container.add(rememberPassword);
       
        autoLogin = new Checkbox("自动登录");
        autoLogin.setBounds(190,138,78,40);
        container.add(autoLogin);
       
        setButton = new JButton("设置");
        setButton.setFont(new Font("宋体",Font.PLAIN,12));
        setButton.setBounds(16,184,70,22);
        container.add(setButton);
       
        loginButton = new JButton("登陆");
        loginButton.addActionListener(this);
        loginButton.setFont(new Font("宋体",Font.PLAIN,12));
        loginButton.setBounds(250,184,70,22);
        container.add(loginButton);
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        FileOperation fo = new FileOperation();
        String string1 = (String)accountValue.getSelectedItem();
        String string2 = passwordValue.getText();
        if(e.getActionCommand().equals("登陆")){
            fo.writeToFile(string1, string2);
        }
    }
}

原创粉丝点击