検索

Javaアダプタクラス

Aug 30, 2024 pm 04:07 PM
java

Java のアダプター クラスは新しいものではありません。これらは、リスナー インターフェイスの実装を提供するために使用されます。アダプター クラスの利点は、コードを節約できることです。アダプタ インターフェイスを使用してアダプタ クラスを継承する場合、すべてのリスナー インターフェイス メソッドを実装する必要はありません。つまり、Java アダプター クラスが含まれるパッケージは 3 つあります。 3 つのパッケージは次のとおりです:

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

  • java.awt.イベント
  • java.awt. dnd
  • java.swing。イベント

また、アダプター クラスのもう 1 つの利点は、特定のイベント リスナー インターフェイスによって処理されるいくつかのイベントを処理する場合に便利であることです。

構文を使用した Java のアダプター クラスの動作

冒頭で気づいたように、アダプター クラスが含まれるパッケージが 3 つあります。ここで、3 つのパッケージすべてのそれぞれのアダプター クラスに対応するリスナー インターフェイスを見ていきます。

  • java.awt.イベントアダプタークラス
  • java.awt.dnd アダプター クラス
  • ジャワ.スイング。イベントアダプタークラス

1. java.awt.イベントアダプタークラス

以下は、アダプター クラスとそれに対応するリスナー インターフェイスの一部です。これらは、Java パッケージの Abstract Window Toolkit に含まれています。

Adapter Class

Listener Interface

WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
HierarchyBoundsAdapter HierarchyBoundsListener
アダプター クラス

リスナー インターフェイス

ウィンドウアダプター ウィンドウリスナー キーアダプター KeyListener マウスアダプター マウスリスナー マウスモーションアダプター MouseMotionListener フォーカスアダプター フォーカスリスナー コンポーネントアダプター コンポーネントリスナー コンテナアダプター コンテナリスナー HierarchyBoundsAdapter HierarchyBoundsListener テーブル> WindowAdapter クラス 以下のコードは、アダプター クラスの構文を示しており、WindowAdapter クラスの外観と構文についてのアイデアを与えてくれます。
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 クラスの構文を示します。
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 クラスの構文を示します。
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
2. java.awt.dnd アダプター クラス 以下は、awt.dnd パッケージで使用されるアダプター クラスの一部です。それらは表にリストされています。 アダプター クラス リスナー インターフェイス ドラッグソースアダプター 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 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、