首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >确定在MouseListener中单击的JPanel组件。事件处理

确定在MouseListener中单击的JPanel组件。事件处理
EN

Stack Overflow用户
提问于 2011-09-08 04:15:39
回答 1查看 22K关注 0票数 6

我有一个扩展JPanel的类:

代码语言:javascript
运行
复制
public class ButtonPanel extends JPanel {

    private label;

    public ButtonPanel() {
        label=new JLabel("waiting for click");
        add(label);
    }

    public void setButtonText() {
        label.setText("just clicked");
    }

}

我有几个被添加到JFrame中的类的实例。我想创建一个MouseAdapter类的实例,然后将它们作为鼠标侦听器添加到我的JFrame上的所有ButtonPanel组件中。我的意思是:

代码语言:javascript
运行
复制
ButtonPanel butt1 = new ButtonPanel();
ButtonPanel butt2 = new ButtonPanel();
ButtonPanel butt3 = new ButtonPanel();
//... here goes code which add ButtonPanels to JFrame

MouseAdapterMod mam = new MouseAdapterMod();
butt1.addMouseListener(mam);
butt2.addMouseListener(mam);
butt3.addMouseListener(mam);

我希望将MouseAdapterMod类与其他类分开,并定位在它自己的包中。它应该看起来像这样:

代码语言:javascript
运行
复制
public class MouseAdapterMod extends MouseAdapter {

    public void mouseClicked(MouseEvent e) {
        //here goes the code of calling setButtonText method of ButtonPanel component on which the event had occurred
    }
}

所以问题是,我不知道如何实现mouseClicked方法来确定哪个ButtonPanel生成事件,并调用与该组件setButtonText()方法相对应的。有人知道怎么做吗?

我知道我可以通过在ButtonPanel类中包含事件处理功能来实现这一点,但这种方式对我来说并不合适,因为我希望保持如上所述的类结构,并且只有一个MouseAdapterMod类实例来处理所有的ButtonPanels。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-08 04:20:47

MouseEvent#getSource方法将返回被单击的对象:

代码语言:javascript
运行
复制
public class MouseAdapterMod extends MouseAdapter {

   // usually better off with mousePressed rather than clicked
   public void mousePressed(MouseEvent e) {
       ButtonPanel btnPanel = (ButtonPanel)e.getSource();
       btnPanel.setButtonText();
   }
}

正如评论指出的那样,你最好听mousePressed或mouseReleased,而不是mouseClicked,因为要让mouseClicked正常工作,按下和释放必须是从同一个点上进行的,如果鼠标移动了一点点,点击就不会被记录下来。

我的测试程序:

代码语言:javascript
运行
复制
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.*;

public class MainForButtonPanel extends JPanel {
   public MainForButtonPanel() {
      setLayout(new GridLayout(4, 4));

      MouseAdapter myMA = new MouseAdapterMod();

      for (int i = 0; i < 4; i++) {
         for (int j = 0; j < 4; j++) {
            ButtonPanel btnPanel = new ButtonPanel();
            btnPanel.addMouseListener(myMA);
            add(btnPanel);
         }
      }

   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("MainForButtonPanel");
      frame.getContentPane().add(new MainForButtonPanel());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

class ButtonPanel extends JPanel {

   private static final int TIMER_DELAY = 2000;
   private static final String JUST_CLICKED = "just clicked";
   private static final String WAITING_FOR_CLICK = "waiting for click";
   private static final Color CLICKED_COLOR = Color.pink;
   private JLabel label;

   public ButtonPanel() {
      label = new JLabel(WAITING_FOR_CLICK);
      add(label);
   }

   public void setButtonText() {
      label.setText(JUST_CLICKED);
      setBackground(CLICKED_COLOR);

      new Timer(TIMER_DELAY, new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            label.setText(WAITING_FOR_CLICK);
            setBackground(null);
            ((Timer)ae.getSource()).stop();
         }
      }).start();
   }

}

class MouseAdapterMod extends MouseAdapter {

   // usually better off with mousePressed rather than clicked
   public void mousePressed(MouseEvent e) {
       ButtonPanel btnPanel = (ButtonPanel)e.getSource();
       btnPanel.setButtonText();
   }
}
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7340001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档