Kelas Penyesuai di Java bukanlah perkara baharu. Ia digunakan untuk menyediakan pelaksanaan antara muka Pendengar. Kelebihan kelas penyesuai ialah ia menyimpan kod. Jika kami mewarisi kelas penyesuai menggunakan antara muka Penyesuai, kami tidak akan dipaksa untuk melaksanakan semua kaedah antara muka pendengar. Terdapat tiga pakej di mana kelas penyesuai Java ditemui. Tiga pakej tersebut ialah:
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Selain itu, kelebihan lain Kelas Penyesuai ialah ia berguna apabila kita ingin memproses beberapa acara yang dikendalikan oleh antara muka pendengar acara tertentu.
Seperti yang kita perhatikan dalam pengenalan, terdapat tiga pakej di mana kita mencari kelas penyesuai. Kami kini akan melihat antara muka Pendengar yang sepadan bagi kelas penyesuai masing-masing dalam ketiga-tiga pakej.
Di bawah ialah beberapa kelas Penyesuai dan antara muka pendengar yang sepadan. Ini terdapat dalam Kit Alat Tetingkap Abstrak bagi pakej Java.
|
Antara Muka Pendengar
|
||||||||||||||||||
Penyesuai Tetingkap | WindowListener | ||||||||||||||||||
KeyAdapter | KeyListener | ||||||||||||||||||
Penyesuai Tetikus | Pendengar Tetikus | ||||||||||||||||||
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(); } }Sintaks:
Kelas Penyesuai Kunci
Kod di bawah memberikan sintaks Kelas KeyAdapter.public abstract keyAdapter Class extends Object Implements KeyListenerSintaks:
Kelas Penyesuai Tetikus
Kod di bawah memberikan sintaks Kelas MouseAdapter.MouseAdapterExample() { f=new Frame("Mouse Adapter"); f.addMouseListener(this); f.setSize(100,100); f.setLayout(null); f.setVisible(false); }Sintaks:
Kelas Penyesuai MouseMotion
Kod di bawah memberikan sintaks Kelas 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); }Sintaks:
Kelas Penyesuai Fokus
Kod di bawah memberikan sintaks kelas FocusAdapter.public abstract class FocusAdapter extends Object Implements FocusListenerSintaks:
Kelas Penyesuai Komponen
Kod di bawah memberikan sintaks kelas 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); } }Sintaks:
Kelas Penyesuai Bekas
Kod di bawah memberikan sintaks Kelas ContainerAdapter.public abstract class ContainerAdapter extends Object implements ContainerListenerSintaks:
Kelas Penyesuai HierarkiBounds
Kod di bawah memberikan sintaks kelas HierarchyBoundsAdapter.public abstract class HierarchyBoundsAdapter extends Object implements HierarchyBoundsListenerSintaks:
Adapter Class | Listener Interface |
DragSourceAdapter | DragSourceListener |
DragTargetAdapter | DragTargetListener |
Kelas Penyesuai | Antara Muka Pendengar |
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.
Atas ialah kandungan terperinci Kelas Penyesuai Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!