A cikin wannan labarin, za mu tattauna yadda ake samun haษin gwiwar linzamin kwamfuta a cikin shirin Java. Gudanar da abubuwan da suka faru na linzamin kwamfuta da samun haษin gwiwar su aiki ne mai mahimmanci wajen gina aikace-aikacen mu'amala da mu'amalar mai amfani. Da farko za mu duba tushen sarrafa taron linzamin kwamfuta da kuma ษakunan karatu daban-daban da abin ya shafa, sannan mu bi mataki-mataki-mataki na lambar don cimma wannan aikin.
Gabatarwa zuwa Gudanar da Ayyukan Mouse a Java
Gudanar da taron linzamin kwamfuta muhimmin bangare ne na ฦirฦirar aikace-aikacen mu'amala a Java. The java.awt da kuma java.awt. taron fakiti suna ba da azuzuwan da ake buฦata da musaya don gudanar da al'amuran linzamin kwamfuta yadda ya kamata.
Lokacin aiki tare da abubuwan da suka faru na linzamin kwamfuta a Java, manyan azuzuwan da musaya da muke buฦatar fahimta su ne Taron Mouse, Mai sauraren linzamin kwamfuta, Da kuma MouseMotionListener. Ajin MouseEvent ฦaramin aji ne na ajin ComponentEvent kuma yana wakiltar aikin linzamin kwamfuta, kamar danna maษalli ko matsar da alamar linzamin kwamfuta. MouseListener interface ya ฦunshi hanyoyi don gudanar da al'amuran linzamin kwamfuta daban-daban, yayin da MouseMotionListener ke hulษa da abubuwan motsin linzamin kwamfuta kamar ja da motsi.
Aiwatar Dawowar Haษin Kan Mouse
Bari mu aiwatar da mafita mai sauฦi don dawo da haษin gwiwar linzamin kwamfuta a Java.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseCoordinates extends JFrame implements MouseMotionListener { JLabel coordinatesLabel; public MouseCoordinates() { coordinatesLabel = new JLabel("Mouse coordinates: "); add(coordinatesLabel, BorderLayout.NORTH); addMouseMotionListener(this); } @Override public void mouseMoved(MouseEvent e) { int x = e.getX(); int y = e.getY(); coordinatesLabel.setText("Mouse coordinates: (" + x + ", " + y + ")"); } @Override public void mouseDragged(MouseEvent e) {} public static void main(String[] args) { MouseCoordinates frame = new MouseCoordinates(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setVisible(true); } }
Bayanin mataki-mataki na Code
- Na farko, muna shigo da abin da ake bukata java.awt, java.awt. taron da kuma javax.swing fakiti.
- Sai mu kirkiro class mai suna MouseCoordinates wannan ya faษaษa JFrame da aiwatar da MouseMotionListener dubawa. Wannan yana ba mu damar gudanar da al'amuran linzamin kwamfuta a cikin aji.
- Bayan haka, muna ayyana madaidaicin JLabel da ake kira daidaitawaLabel wanda zai nuna haษin gwiwar linzamin kwamfuta na yanzu.
- A cikin maginin MouseCoordinates ajin, muna aiwatar da wannan daidaitawaLabel kuma saita farkon rubutunsa. Muna ฦara shi zuwa JFrame ta amfani da BorderLayout. AREWA matsayi kuma ฦara mai sauraron motsin linzamin kwamfuta zuwa JFrame ta amfani da addMouseMotionListener() Hanya.
- Sai mu yi watsi da MouseMoved() Hanyar daga MouseMotionListener interface. Ana kiran wannan hanyar a duk lokacin da aka motsa linzamin kwamfuta a cikin sashin. Muna samun daidaitawar x da y na linzamin kwamfuta ta amfani da samuX() da kuma samu() hanyoyin ajin MouseEvent, da sabunta rubutun na daidaitawaLabel tare da sabon linzamin kwamfuta daidaitawa.
- Ba mu da sha'awar sarrafa abubuwan da suka faru na jan linzamin kwamfuta a cikin wannan misalin, don haka kawai muna samar da aiwatarwa mara kyau don aikin MouseDragged() Hanya.
- A ฦarshe, a cikin babban hanyar, muna ฦirฦirar misali na MouseCoordinates aji, saita kaddarorin JFrame kuma sanya shi ganuwa ga mai amfani
Tare da wannan aiwatarwa, masu amfani za su iya ganin haษin gwiwar linzamin kwamfuta na yanzu a cikin taga aikace-aikacen yayin da suke motsa linzamin kwamfuta a kusa. Wannan labarin yana nuna ikon iya gudanar da taron Java da kuma yadda yake da sauฦi don ฦirฦirar aikace-aikacen mu'amala tare da ฦดan layukan lamba.