Java 中的适配器类并不是什么新鲜事。它们用于提供 Listener 接口的实现。适配器类的优点是节省代码。如果我们使用Adapter接口继承适配器类,我们就不会被迫实现所有监听器接口方法。 Java 适配器类位于三个包中。这三个套餐分别是:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
- java.awt。活动
- java.awt。哒哒
- java.swing。活动
此外,适配器类的另一个优点是,当我们想要处理由特定事件侦听器接口处理的一些事件时,它们非常有用。
使用语法在 Java 中使用适配器类
正如我们在简介中注意到的,我们在三个包中找到适配器类。我们现在将在所有三个包中看到相应适配器类的相应监听器接口。
- java.awt。事件适配器类
- java.awt.dnd 适配器类
- java.摇摆。事件适配器类
1. java.awt。事件适配器类
下面是一些 Adapter 类及其相应的监听器接口。这些都存在于 Java 包的 Abstract Window Toolkit 中。
Adapter Class |
Listener Interface |
WindowAdapter | WindowListener |
KeyAdapter | KeyListener |
MouseAdapter | MouseListener |
MouseMotionAdapter | MouseMotionListener |
FocusAdapter | FocusListener |
ComponentAdapter | ComponentListener |
ContainerAdapter | ContainerListener |
HierarchyBoundsAdapter | HierarchyBoundsListener |
监听器接口
AdapterExample() { f=new Frame("Hello World"); f.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) { f.close(); } }语法:
KeyAdapter 类
下面的代码给出了 KeyAdapter 类的语法。public abstract keyAdapter Class extends Object Implements KeyListener语法:
MouseAdapter 类
下面的代码给出了 MouseAdapter 类的语法。MouseAdapterExample() { f=new Frame("Mouse Adapter"); f.addMouseListener(this); f.setSize(100,100); f.setLayout(null); f.setVisible(false); }语法:
MouseMotionAdapter 类
下面的代码给出了 MouseMotionAdapter 类的语法。MouseMotionAdapterExample() { f=new Frame("Adapter for Mouse Motion"); f.addMouseMotionListener(this); f.setSize(100,100); f.setLayout(null); f.setVisible(false); } public void mouseDragged(MouseEvent e) { Graphics g=f.getGraphics(); g.setColor(Color.ORANGE); g.fillOval(e.getX(),e.getY(),20,20); }语法:
FocusAdapter 类
下面的代码给出了 FocusAdapter 类的语法。public abstract class FocusAdapter extends Object Implements FocusListener语法:
ComponentAdapter 类
下面的代码给出了 ComponentAdapter 类的语法。class MyAdapter extends ComponentAdapter { public void componentMoved(ComponentEvent e) { int a = e.getComponent().getX(); int b = e.getComponent().getY(); System.out.println("Value of X " + a); System.out.println("Value of Y: " + b); } }语法:
ContainerAdapter 类
下面的代码给出了 ContainerAdapter 类的语法。public abstract class ContainerAdapter extends Object implements ContainerListener语法:
HierarchyBoundsAdapter 类
下面的代码给出了 HierarchyBoundsAdapter 类的语法。public abstract class HierarchyBoundsAdapter extends Object implements HierarchyBoundsListener语法:
Adapter Class | Listener Interface |
DragSourceAdapter | DragSourceListener |
DragTargetAdapter | DragTargetListener |
In this case, there are two Adapter classes under the respective package, which is called. We will see the syntax of the two Adapter classes and study the codes under the respective package.
DragSourceAdapter Class
The below code gives the syntax of the DragSourceAdapter class.
Syntax:
public abstract class DragSourceAdapter extends Object implements DragSourceAdapterListener
DragTargetAdapter Class
The below code gives the syntax of the DragTargetAdapter class.
Syntax:
public abstract class DragTargetAdapter extends Object implements DragTargetAdapterListener
3. java.swing. event Adapter Classes
Below are some of the classes which are used in the swing. event package. They are listed down in a table.
Adapter Class | Listener Interface |
MouseInputAdapter | MouseInputListener |
InternalFrameAdapter | InternalFrameListener |
In this case, we notice two adapter classes along with their Listener Interface. The package javax. swing. event has two adapter classes which are respectively listed down in the table.
MouseInputAdapter Class
Below is the code which provides the syntax of the MouseInputAdapter class.
Syntax:
public abstract class MouseInputAdapter extends MouseAdapter implements MouseInputListener
InternalFrameAdapter Class
Below is the code which provides the syntax of the InternalFrameAdapter class.
Syntax:
public abstract class InternalFrameAdapter extends Object implements InternalFrameListener
Explanation
Adapter Classes are basically used in the case when the class is declared as abstract. Only the working is based on different functionalities, including the working of the Java classes, which are part of different Java packages. There are three packages in Java that have different Adapter classes and Listener Interfaces along with them. Adapter Classes are used in numerous cases where servers are used, and there is connectivity with the SQL database.
There are different functionalities, and mostly all of them are being used in Android programming as well. Adapter Classes are well-known functionality in Java programming, which helps coders get the idea of many applications that can be used via Swing methodology. There are many ideas that can be put forward to develop the Adapter Class methodology in Java. Loops can also be incorporated to develop some other types of functionality.
Conclusion
In this article, we see the working, and we see that there are three main packages that contain the adapter classes. We see the syntax of all the adapter classes which are present, as well as some of the codes of the working, are also provided. Adapter classes are basically unique in Java, and they are part of Swing functionality in Java.
以上是Java 适配器类的详细内容。更多信息请关注PHP中文网其他相关文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版
中文版,非常好用

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能