首頁 > Java > java教程 > 主體

Java 適配器類

PHPz
發布: 2024-08-30 16:07:15
原創
528 人瀏覽過

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中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板