搜尋
首頁Javajava教程Java 適配器類

Java 適配器類

Aug 30, 2024 pm 04:07 PM
java

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
適配器類別

監聽器介面

視窗適配器 視窗監聽器 密鑰適配器 按鍵監聽器 滑鼠適配器 滑鼠監聽器 MouseMotionAdapter MouseMotionListener 焦點適配器 焦點監聽喇叭 組件適配器 組件監聽器 容器適配器 容器監聽器 HierarchyBoundsAdapter HierarchyBoundsListener 表> WindowAdapter 類別 下面的程式碼給了 Adapter 類別的語法,它讓我們了解 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 類別

下面的程式碼給了 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
2. java.awt.dnd 適配器類別 下面是 awt.dnd 套件中使用的一些適配器類別。它們列在表格中。 適配器類別 監聽器介面 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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

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應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中