事件用接口实现
包 java.awt.event
事件监听器在一个事件发生时被通知的对象 也成为了事件处理器
事件源提供了一组方法,用于为事件注册一个或多个监听器对象,并向其发送事件对象
public void addListener(TypeListener e)
private void initEvent(){
}
当前类 加一个 implements ActionListeener
内部类 new ActionListener()
外部类 class MyListener implements ActionListener{
}
焦点事件 addFocusListener
focusGained 获得焦点 focusLost 失去焦点
getcomponent () 获得当前组件
tf.requestFocus(); 重新获取焦点
键盘事件 addKeyListener
keypressed 按下键盘
keyReleased 释放键盘
e.getkeyCode 返回ASCII值
e.getkeyChar 返回按值
鼠标事件 俩个 addMouseListener() addMouseMotionListener()
mouseEntered 鼠标进入
mouseMoved 鼠标移动
mouseExited 鼠标退出
mouseDragged 鼠标拖动
mouseClicked 鼠标单机
mousePressed 鼠标按下
mouseReleased 鼠标松开
e.getx() 获取x坐标
e.gety() 获取y坐标
e.getButton() 获取单击的是左键还是右键还是滚轮
下拉框事件 addItemListener()
itemStateChanged();选项发生改变时候触发
图标
ImageIcon icon=new ImageIcon(“文件”);
窗体图标
this.setIconImage(image);
BufferedImage image =new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB)
每一个容器都有 public void paint(Graphics g){
} 画笔
g.setColor(color.red) 颜色
g.setFont(new Font(“宋体”,Font.BOLD,30));
g.drawString(“hello”,30,100) 字
g.drawLine(30,110,100,100) 直线
g.drawRect(30,120,30,60) 矩形
g.fillRect(30,190,30,60) 填充
public void repaint(){
} 重绘
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class EventDemo extends JFrame {
private JTextField tf;
private JTextField tf2;
private JPanel panel;
private JComboBox<String> 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.setBackground(Color.BLUE);
box=new JComboBox<String>(data);
this.add(tf);
this.add(box);
this.add(tf2);
this.add(panel);
setLayout(new FlowLayout(FlowLayout.CENTER));
initEvent();
Dimension dim =getToolkit().getScreenSize();
int x=dim.width/2-100;
int y=dim.height/2-100;
this.setResizable(false);
this.setTitle("事假案例");
this.setSize(500,300);
this.setLocation(x,y);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class MyListener implements FocusListener,KeyListener,MouseListener,MouseMotionListener,ItemListener {
@Override
public void focusGained(FocusEvent e) {
System.out.println("hh");
}
@Override//getComponent 重新获取焦点
//requestFocus 自动获取焦点
public void focusLost(FocusEvent e) {
/* System.out.println("ss"+e.getComponent()); */
/* tf.requestFocus(); */
/*
* if (tf.getText().length()<=8) { System.out.println("密码必须大于8位");
* tf.requestFocus(); }
*/
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_KP_UP) {
System.out.println("坦克向上移动");
}
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("松开键盘"+e.getKeyChar());
}
@Override
public void mouseDragged(MouseEvent e) {
//System.out.println("mouseDragged");
}
@Override
public void mouseMoved(MouseEvent e) {
//System.out.println("mouseMoved");
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("mouseClicked==="+e.getButton()+e.getX());
}
@Override
public void mousePressed(MouseEvent e) {
//System.out.println("mousePressed");
}
@Override
public void mouseReleased(MouseEvent e) {
//System.out.println("mouseReleased");
}
@Override
public void mouseEntered(MouseEvent e) {
//System.out.println("mouseEntered");
}
@Override
public void mouseExited(MouseEvent e) {
//System.out.println("mouseExited");
}
@Override
public void itemStateChanged(ItemEvent e) {
System.out.println("选项菜单"+e.getItem());
}
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class EventDemo1 extends JFrame {
/* private JButton button; */
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 30));
g.drawString("嬲", 30, 100);
g.drawLine(20, 200, 30, 500);
}
public EventDemo1(){
init();
}
private void init() {
setLayout(new FlowLayout(FlowLayout.CENTER));
/*
* ImageIcon icon=new ImageIcon("image/quest.png"); button=new
* JButton("按钮",icon); this.add(button);
*/
Dimension dim =getToolkit().getScreenSize();
int x=dim.width/2-100;
int y=dim.height/2-100;
this.setResizable(false);
this.setTitle("事假案例");
this.setSize(500,300);
this.setLocation(x,y);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}