简体   繁体   English

了解Netbeans GUI生成的代码中的按钮/鼠标侦听器

[英]Understanding button/mouse listener in Netbeans GUI generated code

can any tell me what this part of code is doing? 有谁能告诉我这部分代码在做什么?

 jButton1.addMouseListener(new java.awt.event.MouseAdapter() 
                           {
                 public void mouseClicked(java.awt.event.MouseEvent evt) 
                                 {
                     jButton1MouseClicked(evt);
                 }
                   });

why are there methods in the parameter for the method addMouseListener? 为什么在方法addMouseListener的参数中有方法? can some one explain in details? 有人可以详细解释吗? im using netbeans and this is code generated. 我使用netbeans,这是生成的代码。

It's an anonymous MouseAdapter, meaning it calls a new class instance without a variable/assignment. 这是一个匿名的MouseAdapter,意味着它将调用一个没有变量/赋值的新类实例。 The code itself is calling a new custom event handling method jButton1MouseClicked() , which is what netbeans generates for you so that you can add in your own handling for the code. 代码本身正在调用新的自定义事件处理方法jButton1MouseClicked() ,这是netbeans为您生成的,因此您可以添加自己的代码处理。

Yes, don't add a mouse listener to a button. 是的,不要在按钮上添加鼠标侦听器。 Well, I use this method to get the position of X and Y mouse click in my GUI using NetBeans. 好吧,我使用NetBeans使用此方法在GUI中获得X和Y鼠标单击的位置。

Right-click the swing container: Choose Events: choose Mouse: choose MouseClicked. 右键单击“ swing”容器:选择“事件”:选择“鼠标”:选择“ MouseClicked”。 Add the following piece of code; 添加以下代码; (of course my text fields have the variable names tfMouseX and tfMouseY). (当然,我的文本字段具有变量名称tfMouseX和tfMouseY)。

tfMouseX.setText(evt.getX() + "");
tfMouseY.setText(evt.getY() + "");

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

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