个性化阅读
专注于IT技术分析

Java适配器类

本文概述

Java适配器类提供了侦听器接口的默认实现。如果继承适配器类, 则不会被迫提供所有侦听器接口方法的实现。因此, 它节省了代码。

适配器类可在java.awt.event, java.awt.dnd和javax.swing.event包中找到。适配器类及其相应的侦听器接口如下所示。

java.awt.event适配器类

适配器类 侦听器界面
WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
HierarchyBoundsAdapter HierarchyBoundsListener

java.awt.dnd适配器类

适配器类 侦听器界面
DragSourceAdapter DragSourceListener
DragTargetAdapter DragTargetListener

javax.swing.event适配器类

适配器类 侦听器界面
MouseInputAdapter MouseInputListener
InternalFrameAdapter InternalFrameListener

Java WindowAdapter示例

import java.awt.*;
import java.awt.event.*;
public class AdapterExample{
	Frame f;
	AdapterExample(){
		f=new Frame("Window Adapter");
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				f.dispose();
			}
		});
		
		f.setSize(400, 400);
		f.setLayout(null);
		f.setVisible(true);
	}
public static void main(String[] args) {
	new AdapterExample();
}
}

输出:

java awt windowadapter示例1

Java MouseAdapter示例

import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseAdapter{
	Frame f;
	MouseAdapterExample(){
		f=new Frame("Mouse Adapter");
		f.addMouseListener(this);
		
		f.setSize(300, 300);
		f.setLayout(null);
		f.setVisible(true);
	}
	public void mouseClicked(MouseEvent e) {
		Graphics g=f.getGraphics();
		g.setColor(Color.BLUE);
		g.fillOval(e.getX(), e.getY(), 30, 30);
	}
	
public static void main(String[] args) {
	new MouseAdapterExample();
}
}

输出:

java awt mouseadapter示例1

Java MouseMotionAdapter示例

import java.awt.*;
import java.awt.event.*;
public class MouseMotionAdapterExample extends MouseMotionAdapter{
	Frame f;
	MouseMotionAdapterExample(){
		f=new Frame("Mouse Motion Adapter");
		f.addMouseMotionListener(this);
		
		f.setSize(300, 300);
		f.setLayout(null);
		f.setVisible(true);
	}
public void mouseDragged(MouseEvent e) {
	Graphics g=f.getGraphics();
	g.setColor(Color.ORANGE);
	g.fillOval(e.getX(), e.getY(), 20, 20);
}
public static void main(String[] args) {
	new MouseMotionAdapterExample();
}
}

输出:

java awt mousemotionadapter示例1

Java KeyAdapter示例

import java.awt.*;
import java.awt.event.*;
public class KeyAdapterExample extends KeyAdapter{
	Label l;
	TextArea area;
	Frame f;
	KeyAdapterExample(){
		f=new Frame("Key Adapter");
		l=new Label();
		l.setBounds(20, 50, 200, 20);
		area=new TextArea();
		area.setBounds(20, 80, 300, 300);
		area.addKeyListener(this);
		
		f.add(l);f.add(area);
		f.setSize(400, 400);
		f.setLayout(null);
		f.setVisible(true);
	}
	public void keyReleased(KeyEvent e) {
		String text=area.getText();
		String words[]=text.split("\\s");
		l.setText("Words: "+words.length+" Characters:"+text.length());
	}

	public static void main(String[] args) {
		new KeyAdapterExample();
	}
}

输出:

java awt keyadapter示例1
赞(0)
未经允许不得转载:srcmini » Java适配器类

评论 抢沙发

评论前必须登录!