简体   繁体   中英

Add actionlistener to jpanel

I would like to know if there is any way to add an ActionListener to a JPanel? I have no problem adding those to JButtons, but JPanel seems not to have such a method.

Basically I have a bunch of JPanels inside a JFrame with a grid layout and I would like to know if there is any way to know when the user has clicked on one of them.

Any help much appreciated!

An action listener serves to listen to action events. A button triggers an action event when it's pressed and released using the mouse (and not when it's clicked), or when it's pressed using the keyboard (space bar, mnemonic, keyboard shortcut, etc.). It's a high-level event. A mouse click is a more low-level event, which is handled by a MouseListener .

Add a MouseListener to your JPanel if you want to handle mouse clicks.

显然你可以使用AddMouseLitener ..傻我..

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