Java课程设计3

对对象所做的一组动作称为事件
事件处理机制中,涉及到三个对象:事件本身、事件的来源和事件处理器。
事件是事件类的一个对象,事件类的根类是Java.util.EventObject通常所用到的事件类在Java.awt.event包中。
事件源也是组件,事件源传给事件监听器(以接口的形式),事件+后缀名Listener就是事件监听器。
组件可以有多个事件,但一个事件共有一个监听器。
事件源提供了一组方法,用于为事件注册一个或多个监听器对象,并向其发送事件对象,
package com.imau.gui;
import java.awt.;
import javax.swing.
;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Login extends JFrame implements ActionListener{
private JLabel lname;
private JLabel lpass;
private JTextField tname;//用户名是明文
private JPasswordField tpass;//密码不是明文
private JButton login;

private void initEvnet(){
	login.addActionListener(this);
	
}



private void init(){


	lname=new JLabel("用户名");
	lpass=new JLabel("密码");
	tname=new JTextField(14);
	tpass=new JPasswordField(14);
	login=new JButton ("登录");
	Dimension dim=getToolkit().getScreenSize();
	int w=dim.height/2;
	int h=dim.width/2;
	
	
	initEvnet();//做调用
	
	this.setLayout(new FlowLayout(FlowLayout.CENTER));
	this.add(lname);
	this.add(tname);
	this.add(lpass);
	this.add(tpass);
	this.add(login);
	this.setTitle("QQ登录");
	this.setSize(255,125);
	this.setLocation(400,300);
	this.setVisible(true);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Login(){
	init();
}

//处理器的方法

@Override
public void actionPerformed(ActionEvent e) {
	
	System.out.println("单击了按钮······"+tname.getText()+","+tpass.getText()+","+e.getSource());//获取到输入的密码和用户名。e.getSource()方法是打印的结果,判断多个事件中哪个的事件
	
	
}

}

package com.imau.gui;
import java.awt.;
import javax.swing.
;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import java.awt.event.*;
public class EvtentDemo extends JFrame{
private JTextField tf;
private JTextField tf2;
private JPanel pane1;
private JComboBox box;
private String data[]={“A”,“B”,“C”,“D”};

private void initEvtent(){
	tf.addFocusListener(new MyListener());
	tf.addKeyListener(new MyListener());
	pane1.addMouseListener(new MyListener());
	pane1.addMouseMotionListener(new MyListener());
	box.addItemListener(new MyListener());
}

//调用EvtentDemo
public EvtentDemo(){
	init();
}

private void init(){
	
    tf=new JTextField(10);
   tf2=new JTextField(10);
   pane1=new JPanel();
   pane1.setSize(200, 200);
   pane1.setBackground(Color.BLUE);
   box=new JComboBox<>(data);
   this.add(pane1);
    this.add(tf2);
    this.add(tf);
    this.add(box);
	this.setLayout(new FlowLayout(FlowLayout.CENTER));

	 initEvtent();
	//获取屏幕大小,设置位置 大小
	Dimension dim=getToolkit().getScreenSize();
	int H=dim.height/2;
    int W=dim.width/2;
    this.setResizable(false);
	this.setTitle("事件案例");
	this.setSize(500,300);
	this.setLocation(W-500/2,H-300/2);
	this.setVisible(true);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
   class MyListener implements FocusListener,KeyListener,MouseMotionListener, MouseListener,ItemListener{

	@Override
	public void focusGained(FocusEvent arg0) {
		System.out.println("aa");
		
	}

	@Override
	public void focusLost(FocusEvent arg0) {
	       
		//getComponent()   获取当前组件对象
		//requestFocus()  从新获得焦点
		
		//System.out.println("bb");
		if(tf.getText().length()<=8) {
			System.out.println("密码必须大于8位");
			tf.requestFocus();
			
		}
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	//按下时
	public void keyReleased(KeyEvent e) {
		//System.out.println("按下键盘"+e.getKeyCode());
		if(e.getKeyCode()==KeyEvent.VK_KP_UP){
			System.out.println("坦克向上移动");
		}
	}

	@Override
	//释放时
	public void keyTyped(KeyEvent e) {
		
		System.out.println("释放键盘"+e.getKeyChar());
	}

	
	

	@Override
	public void mouseClicked(MouseEvent e) {
		System.out.println("mouseClicked=="+e.getButton());
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		//System.out.println("mouseEntered");
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		//System.out.println("mouseExited");
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		System.out.println("mousePressed");
		
	}

	@Override
	//按下鼠标
	public void mouseReleased(MouseEvent e) {
		System.out.println("mouseReleased");
		
	}

	@Override
	//按下鼠标拖拽
	public void mouseDragged(MouseEvent e) {
		System.out.println("mouseDragged");
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		//获得X,Y坐标
		e.getX();
		e.getY();
		//获取单击时哪个键
		e.getButton();
		System.out.println("mouseMoved");
		
	}
  //选项发生改变时候会触发
	@Override
	public void itemStateChanged(ItemEvent arg0) {
		System.out.println("选项菜单");
	}


   }
   //class MyListener2 extends FocusAdapter{
		//@Override
		//public void focusLost(FocusEvent e){
			//super.focusLost(e);
		//}

}

你可能感兴趣的:(Java课程设计3)