Java课程设计第三节

1.当事件发生后,系统会通知我们去处理这些事件,对这些事件做出相应的处理的程序,称为事件处理器
2.通常用到的事件类在java.awt.event包中
3.事件监听器是在一个事件发生时被通知的对象,也称为事件处理器,也分为很多种,以接口形式
4.事件源提供了一组方法,用于为事件注册一个或多个监听器对象,并向其发送事件对象
5.每种不同的事件都有自己的注册方法,一般形式为:
public void add Listener(TypeLietener e)添加
public void remove Listener(TypeLietener e)移除
6.写一个内部类
public class Login exdents JFrame{
private Jlabel lname;
private Jlabel lpass;
private JTextField tname;
private JPasswordField tpass;
private Jlabel login;
private void initEvent(){
login.addActionListener(new MyListener());
}
private void init(){
lname=new Jlabel(“用户名”);
lpass=new Jlabel(“密码”);
tname=new JTextField(14);
tpass=new JPasswordFieldField(14);
login=new JButton(“登录”);
this.setLayout(new FlowLayout(FlowLayout.center));
this.add(lname);
this.add(tname);
this.add(lpass);
this.add(tpass);
this,add(login);
Dimension dim=getToolkit().getScreenSize();
this.setRestzablr(false);
this.setTitle(“QQ登录”);
this.setSize(260,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
public Login(){
init();}
}

public class Test{
publuc static void main(String[] args){
new login()}

Class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e) System.out.println(“单击了按钮”+tnme.getText()+","+tpass.getText()+","+e.getSourse());}
}
}
7.package com.imau.gui;
import java.awt.;
import javax.swing.
;
import java.awt.event.*;
public class EventDemo extend JFrame{
private JTextField tf;
private JTextField tf2;
private void initEvent(){
tf.addFocusListener(new MyListener());}
public EventDemo(){
init();
}
private void init(){
tf=new JTextField(10);
tf2=new JTextField(10);
this.add(tf);
this.add(tf2);
this.setLayout(new FlowLayout(FlowLayout.center));
initEvent();
Dimension dim=getToolkit().getScreenSize();
this.setRestzablr(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{
public void focusGained(FocusEvent e){
System.out.println(“aa”);
}
public void focusLost(FocusEvent e){
//getComponent()可以获取当前组件对象
//requestFocus 重新获得焦点

  1. System.out.println(“bb”+e.getComponent()==tf);
    tf.requestFocus();
  2. if(tf.getText().length()<=8){
    System.out.println(“密码必须大于8位”);
    tf.requestFocus();}

}
}
}
package com.imau.gui;
public class Test{
public static void main(String[] args){
new EventDemo();}
}
8.键盘事件
package com.imau.gui;
import java.awt.;
import javax.swing.
;
import java.awt.event.*;
public class EventDemo extend JFrame{
private JTextField tf;
private JTextField tf2;
private void initEvent(){
tf.addFocusListener(new MyListener());
tf.addKeyListener(new MyListener())}
public EventDemo(){
init();
}
private void init(){
tf=new JTextField(10);
tf2=new JTextField(10);
this.add(tf);
this.add(tf2);
this.setLayout(new FlowLayout(FlowLayout.center));
initEvent();
Dimension dim=getToolkit().getScreenSize();
this.setRestzablr(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{
public void focusGained(FocusEvent e){
System.out.println(“aa”);
}
public void focusLost(FocusEvent e){
//getComponent()可以获取当前组件对象
//requestFocus 重新获得焦点

  1. System.out.println(“bb”+e.getComponent()==tf);
    tf.requestFocus();
  2. if(tf.getText().length()<=8){
    System.out.println(“密码必须大于8位”);
    tf.requestFocus();}

}
public void keyPressed(KeyEvent e){
1. System.out.println(“按下键盘”+e.getKeyCode());
2. if(e.getKeyCode()==keyEvent.VK_KP_UP){
System.out.println(“坦克向上移动”); }
}
public void keyReleas(KeyEvent e){
2. System.out.println(“释放键盘”+e,getKeyChar());
}
}
}
package com.imau.gui;
public class Test{
public static void main(String[] args){
new EventDemo();}
}

9.鼠标事件
package com.imau.gui;
import java.awt.;
import javax.swing.
;
import java.awt.event.*;
public class EventDemo extend JFrame{
private JTextField tf;
private JTextField tf2;
private JPanel panel;
private void initEvent(){
tf.addFocusListener(new MyListener());
tf.addKeyListener(new MyListener());
panel.addMouseListener(new MyListener());
panel.addMouseMotionListener(new MyListener());}
public EventDemo(){
init();
}
private void init(){
tf=new JTextField(10);
tf2=new JTextField(10);
panel=new JPanel();
panel.setSize(200,200);
panel.setBackground(Color.BLUE);
this.add(tf);
this.add(tf2);
this.add(panel);
// this.setLayout(new FlowLayout(FlowLayout.center));
initEvent();
Dimension dim=getToolkit().getScreenSize();
this.setRestzablr(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,MouseListener,MouseMotionListener{
public void focusGained(FocusEvent e){
System.out.println(“aa”);
}
public void focusLost(FocusEvent e){
//getComponent()可以获取当前组件对象
//requestFocus 重新获得焦点

  1. System.out.println(“bb”+e.getComponent()==tf);
    tf.requestFocus();
  2. if(tf.getText().length()<=8){
    System.out.println(“密码必须大于8位”);
    tf.requestFocus();}

}
public void keyPressed(KeyEvent e){
1. System.out.println(“按下键盘”+e.getKeyCode());
2. if(e.getKeyCode()==keyEvent.VK_KP_UP){
System.out.println(“坦克向上移动”); }
}
public void keyReleas(KeyEvent e){
2. System.out.println(“释放键盘”+e,getKeyChar());
}
}
}
package com.imau.gui;
public class Test{
public static void main(String[] args){
new EventDemo();}
}

package com.imau.gui;
import java.awt.;
import javax.swing.
;
import java.awt.event.*;
public class EventDemo extend JFrame{
private JTextField tf;
private JTextField tf2;
private JPanel panel;
private JComboBox box;
private String data[]={“A”,“B”,“C”};
private void initEvent(){
tf.addFocusListener(new MyListener());
tf.addKeyListener(new MyListener());
panel.addMouseListener(new MyListener());
panel.addMouseMotionListener(new MyListener());
box.addItemListener(new MyListener());}
public EventDemo(){
init();
}
private void init(){
tf=new JTextField(10);
tf2=new JTextField(10);
panel=new JPanel();
panel.setSize(200,200);
panel.setBackground(Color.BLUE);
box=newJComboBox<>(data
; this.add(tf);
this.add(tf2);
this.add(panel);
this.setLayout(new FlowLayout(FlowLayout.center));
initEvent();
Dimension dim=getToolkit().getScreenSize();
this.setRestzablr(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,MouseListener,MouseMotionListener.ItemListener{
public void focusGained(FocusEvent e){
System.out.println(“aa”);
}
public void focusLost(FocusEvent e){
//getComponent()可以获取当前组件对象
//requestFocus 重新获得焦点

}
public void keyPressed(KeyEvent e){
1. System.out.println(“按下键盘”+e.getKeyCode());
2. if(e.getKeyCode()==keyEvent.VK_KP_UP){
System.out.println(“坦克向上移动”); }
}
public void keyReleas(KeyEvent e){
2. System.out.println(“释放键盘”+e,getKeyChar());
}
//选项发生改变时候触发
public void itemStateChanged(ItemEveny e){
System.out.println(’'选项菜单"+e.getItem()+","+e.getStateCha);
}
}
}
package com.imau.gui;
public class Test{
public static void main(String[] args){
new EventDemo();}
}

你可能感兴趣的:(Java课程设计第三节)