简体   繁体   中英

How to add MouseListener to actionPerformed?

I don't know how I can add mouseListener (mouseClicked, mouseEntered, etc...) to my actionPerformed. I learned only how to add action from JButton but mouseListener is in JLabel.

Here it's this code:

test = new JLabel (ikona);
    test.setBounds(200, 200, 100, 100);
    add(test);
    test.addMouseListener(new MouseListener()
    {

        public void mouseClicked(MouseEvent e) {
            System.out.println(ikona2);

        }

        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

and:

public void actionPerformed(ActionEvent arg0) 
{
    Object Zrodlo = arg0.getSource();
    if (Źródło==przycisk)
    {
    wyswietlacz.setText(new Date().toString());
    //System.out.println(new Date());
    }
    else if (Zrodlo==przycisk2)
    {
        dispose();
    }
    else if (Zrodlo==przycisk3)
    {
    wyswietlacz.setText(new Date().toString());
    }
    else if (Zrodlo==test)
    {
        wyswietlacz.setText("");
    }

"przycsik, przycisk2, przycisk3" are JButton, I try doing something with JLAbel ("test") but I don't have idea how solve this.

PS sorry for my english...

EDIT: For JButton I use this to see action in mine JFrame:

public void actionPerformed(ActionEvent arg0) 
{
    Object Zrodlo = arg0.getSource();
    if (Źródło==przycisk)
    {
    wyswietlacz.setText(new Date().toString());
    //System.out.println(new Date());
    }
    else if (Źródło==przycisk2)
    {
        dispose();
    }

I want to do same with my JLabel and mouseListener. I want see interaction which mouse/cursor which MouseListener. I want to add icon(gif) to JLabel and use MouseListener to change icon1 to icon2 example mouseClicked or mousePressed. If I use:

test.addMouseListener(new MouseListener()
    {

        public void mouseClicked(MouseEvent e) {
            System.out.println(ikona2);

        }

I only see source to my "ikona2" in my Eclipse Console. I want to see action in my JFrame.

Not sure I understand the question but you can paint a JButton like a JLabel but still have the ActionListener work like a button:

JButton button3 = new JButton("Label Button");
button3.setBorderPainted(false);
button3..setFocusPainted(false);
button3.setContentAreaFilled(false);
button3.addActionListener( ... );

A listener is a type of callback that follows the observer pattern , something happens, you get notified.

There are many types of listeners for many different types of events. Buttons have an ActionListener which is triggered by, at least, the user clicking or pressing enter or space while the button has focus.

A label does not have an ActionListener , a label is a static component for all intended purposes, however, a label does have a MouseListener ...

MouseListener listener = ...;
JLabel label = new JLabel("This is a clickable lable");
label.addMouseListener(listener);

This will allow you to monitor when a mouse click occurs on the label.

Take a look at:

For more details

You can't combine those two action listeners (MouseListener & ActionListener). You can add the MouseListener on a JLabel as well on a JButton. Adding an ActionListener to the JLabel isn't allowed anyway. What you can do is to create one MouseListener which handles the events for the JLabel as well for the JButton. Here is an example:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JLabel;

public class MouseListenerTest extends javax.swing.JFrame {
    private static final long serialVersionUID = 3109442737770802801L;

    public static void main(String[] args) {
        MouseListenerTest t = new MouseListenerTest();
        t.setLayout(new BorderLayout());

        MyMouseListener mouseListener = new MyMouseListener();
        JLabel l = new JLabel("JLabel");
        l.setPreferredSize(new Dimension(200, 100));
        JButton b = new JButton("JButton");
        b.setPreferredSize(new Dimension(200, 100));

        l.addMouseListener(mouseListener);
        b.addMouseListener(mouseListener);

        t.add(l, BorderLayout.CENTER);
        t.add(b, BorderLayout.SOUTH);

        t.pack();
        t.setVisible(true);
    }
}

class MyMouseListener implements MouseListener {

    @Override
    public void mouseClicked(MouseEvent e) {
        Object o = e.getSource();
        if (o instanceof JButton) {
            System.out.println("JButton");
        } else if (o instanceof JLabel) {
            System.out.println("JLabel");
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {/* TODO */
    }

    @Override
    public void mouseReleased(MouseEvent e) {/* TODO */
    }

    @Override
    public void mouseEntered(MouseEvent e) {/* TODO */
    }

    @Override
    public void mouseExited(MouseEvent e) {/* TODO */
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM