Java 中的適配器類別並不是什麼新鮮事。它們用於提供 Listener 介面的實作。適配器類別的優點是節省代碼。如果我們使用Adapter介面繼承適配器類,我們就不會被迫實作所有監聽器介面方法。 Java 適配器類別位於三個套件中。這三個套餐分別是:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
此外,適配器類別的另一個優點是,當我們想要處理由特定事件偵聽器介面處理的一些事件時,它們非常有用。
正如我們在簡介中註意到的,我們在三個套件中找到適配器類別。我們現在將在所有三個套件中看到相應適配器類別的相應監聽器介面。
以下是一些 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.
The below code gives the syntax of the DragSourceAdapter class.
Syntax:
public abstract class DragSourceAdapter extends Object implements DragSourceAdapterListener
The below code gives the syntax of the DragTargetAdapter class.
Syntax:
public abstract class DragTargetAdapter extends Object implements DragTargetAdapterListener
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.
Below is the code which provides the syntax of the MouseInputAdapter class.
Syntax:
public abstract class MouseInputAdapter extends MouseAdapter implements MouseInputListener
Below is the code which provides the syntax of the InternalFrameAdapter class.
Syntax:
public abstract class InternalFrameAdapter extends Object implements InternalFrameListener
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.
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中文網其他相關文章!