“Mouselistener in Java” Code-Antworten

Mouselistener in Java

// Java program to handle MouseListener events
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Mouse extends Frame implements MouseListener {
 
    // Jlabels to display the actions of events of mouseListener
    // static JLabel label1, label2, label3;
 
    // default constructor
    Mouse()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a frame
        JFrame f = new JFrame("MouseListener");
 
        // set the size of the frame
        f.setSize(600, 100);
 
        // close the frame when close button is pressed
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // create anew panel
        JPanel p = new JPanel();
 
        // set the layout of the panel
        p.setLayout(new FlowLayout());
 
        // initialize the labels
        label1 = new JLabel("no event  ");
 
        label2 = new JLabel("no event  ");
 
        label3 = new JLabel("no event  ");
 
        // create an object of mouse class
        Mouse m = new Mouse();
 
        // add mouseListener to the frame
        f.addMouseListener(m);
 
        // add labels to the panel
        p.add(label1);
        p.add(label2);
        p.add(label3);
 
        // add panel to the frame
        f.add(p);
 
        f.show();
    }
 
    // getX() and getY() functions return the
    // x and y coordinates of the current
    // mouse position
    // getClickCount() returns the number of
    // quick consecutive clicks made by the user
 
    // this function is invoked when the mouse is pressed
    public void mousePressed(MouseEvent e)
    {
 
        // show the point where the user pressed the mouse
        label1.setText("mouse pressed at point:"
                       + e.getX() + " " + e.getY());
    }
 
    // this function is invoked when the mouse is released
    public void mouseReleased(MouseEvent e)
    {
 
        // show the point where the user released the mouse click
        label1.setText("mouse released at point:"
                       + e.getX() + " " + e.getY());
    }
 
    // this function is invoked when the mouse exits the component
    public void mouseExited(MouseEvent e)
    {
 
        // show the point through which the mouse exited the frame
        label2.setText("mouse exited through point:"
                       + e.getX() + " " + e.getY());
    }
 
    // this function is invoked when the mouse enters the component
    public void mouseEntered(MouseEvent e)
    {
 
        // show the point through which the mouse entered the frame
        label2.setText("mouse entered at point:"
                       + e.getX() + " " + e.getY());
    }
 
    // this function is invoked when the mouse is pressed or released
    public void mouseClicked(MouseEvent e)
    {
 
        // getClickCount gives the number of quick,
        // consecutive clicks made by the user
        // show the point where the mouse is i.e
        // the x and y coordinates
        label3.setText("mouse clicked at point:"
                       + e.getX() + " "
                       + e.getY() + "mouse clicked :" + e.getClickCount());
    }
}
Mpumelelo Namelo

Mouselistener in Java

this.setLayout(new BorderLayout());
  checkBox = new JCheckBox();
  add(checkBox, BorderLayout.CENTER);
  setOpaque(false);
this.addMouseListener(new MouseListener() {
  public void mouseClicked(MouseEvent arg0) {
    TreePath tp = selfPointer.getPathForLocation(arg0.getX(), arg0.getY());
Mpumelelo Namelo

Mouselistener in Java

package com.javapointers.javase;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
 
public class MouseListenerTest implements MouseListener {
 
    JPanel leftPanel, rightPanel;
    JTextArea textArea;
    JFrame frame;
 
    public MouseListenerTest() {
        frame = new JFrame("MouseListener Test");
        textArea = new JTextArea();
        textArea.setLineWrap(true);
 
        leftPanel = new JPanel();
        rightPanel = new JPanel();
        leftPanel.setPreferredSize(new Dimension(200, 100));
        rightPanel.setPreferredSize(new Dimension(200, 100));
        leftPanel.setBackground(Color.GREEN);
        rightPanel.setBackground(Color.BLUE);
 
        leftPanel.addMouseListener(this);
        rightPanel.addMouseListener(this);
 
        frame.setLayout(new BorderLayout());
        frame.add(leftPanel, BorderLayout.WEST);
        frame.add(rightPanel, BorderLayout.EAST);
        frame.add(textArea, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have clicked the left panel");
        } else {
            textArea.setText("You have clicked the right panel");
        }
    }
 
    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have pressed the left panel");
        } else {
            textArea.setText("You have pressed the right panel");
        }
    }
 
    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have released mouse on left panel");
        } else {
            textArea.setText("You have released mouse on right panel");
        }
    }
 
    @Override
    public void mouseEntered(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have entered mouse on left panel");
        } else {
            textArea.setText("You have entered mouse on right panel");
        }
    }
 
    @Override
    public void mouseExited(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have exited mouse on left panel");
        } else {
            textArea.setText("You have exited mouse on right panel");
        }
    }
 
    public static void main(String args[]) {
        MouseListenerTest test = new MouseListenerTest();
    }
}
Mpumelelo Namelo

Mouselistener in Java

final JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(600, 400));
final JToolBar toolBar = new JToolBar();
button.addMouseListener(new MouseAdapter() {
  public void mousePressed(MouseEvent e) {
    popup.show(e.getComponent(), e.getX(), e.getY());
toolBar.add(button);
frame.getContentPane().add(toolBar, BorderLayout.NORTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Mpumelelo Namelo

Ähnliche Antworten wie “Mouselistener in Java”

Fragen ähnlich wie “Mouselistener in Java”

Weitere verwandte Antworten zu “Mouselistener in Java” auf Java

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen