java 将动作侦听器添加到 jpanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10772401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add actionlistener to jpanel
提问by YemSalat
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.
我想知道是否有任何方法可以将 ActionListener 添加到 JPanel?我将它们添加到 JButtons 没有问题,但 JPanel 似乎没有这样的方法。
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.
基本上,我在带有网格布局的 JFrame 中有一堆 JPanel,我想知道是否有任何方法可以知道用户何时单击了其中一个。
Any help much appreciated!
非常感谢任何帮助!
回答by JB Nizet
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
.
动作监听器用于监听动作事件。按钮在使用鼠标按下和释放时(而不是在单击时)或使用键盘按下时(空格键、助记符、键盘快捷键等)会触发操作事件。这是一场高水平的活动。鼠标单击是一个更底层的事件,由MouseListener
.
Add a MouseListener to your JPanel if you want to handle mouse clicks.
如果您想处理鼠标点击,请将 MouseListener 添加到您的 JPanel。
回答by YemSalat
Aparently you can use AddMouseLitener.. Silly me..
显然你可以使用 AddMouseLitener .. 傻我..