如何将MouseListener添加到actionPerformed? [英] How to add MouseListener to actionPerformed?

查看:83
本文介绍了如何将MouseListener添加到actionPerformed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何将mouseListener(mouseClicked,mouseEntered等)添加到我的actionPerformed中.我只学习了如何从JButton添加动作,但是mouseListener在JLabel中.

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.

这是此代码:

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

        }

和:

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"是JButton,我尝试使用JLAbel(测试")进行操作,但是我不知道如何解决这个问题.

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

P.S.对不起,我的英语...

P.S. sorry for my english...

对于JButton,我用它来查看我的JFrame中的动作:

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();
    }

我想对我的JLabel和mouseListener做同样的事情.我想查看哪个鼠标/光标哪个MouseListener的交互.我想将icon(gif)添加到JLabel并使用MouseListener将icon1更改为icon2示例mouseClicked或mousePressed.如果我使用:

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);

        }

我在Eclipse控制台中仅看到"ikona2"的源代码.我想在我的JFrame中查看操作.

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

推荐答案

侦听器是一种遵循

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

对于许多不同类型的事件,有许多类型的侦听器.按钮具有ActionListener,至少由用户在按钮具有焦点时单击或按下Enter或空格来触发.

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.

标签没有ActionListener,标签是出于所有预期目的的静态组件,但是标签确实具有MouseListener ...

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.

看看:

  • How to write a mouse listener
  • Writing Event Listeners
  • Creating a GUI with Swing

了解更多详情

这篇关于如何将MouseListener添加到actionPerformed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆