An warware: java samun haษ—in gwiwar linzamin kwamfuta

samun haษ—in gwiwar linzamin kwamfuta 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.

Shafi posts:

Leave a Comment