简体   繁体   English

将actionListener()添加到JPanel时遇到问题

[英]Trouble when adding actionListener() to a JPanel

I am trying to add actionListener to a JPanel in swing frame. 我试图将actionListener添加到摆动框架中的JPanel中。 I want to create an event when clicking on that particular JPanel. 我想在单击该特定的JPanel时创建一个事件。 Here is some slice of code where in stuck down. 这是一些代码片段。

JLabel jlab=new JLabel("");
JPanel pnl=new JPanel();
pnl.add(jlab);
JPanel mainpnl=new JPanel();
pnl.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae)      {jlab.setText("Clciking !!!");}});
mainpnl.add(pnl);

JPanel doesn't have the capabilities to raise ActionEvent s, thus it does not have the method addActionListener . JPanel没有引发ActionEvent的功能,因此它没有方法addActionListener

Instead, you probably want to use a MouseListener instead 相反,您可能想改用MouseListener

Check out How to write mouse listeners for more details... 查看如何编写鼠标侦听器以获取更多详细信息...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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