我的onclick想要得到的JPanel的名称是在JFrame present [英] onclick i want to get the name of the JPanel that is present on the JFrame

查看:250
本文介绍了我的onclick想要得到的JPanel的名称是在JFrame present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我尝试实现,但中提到的其行给出的错误;这是因为函数不列入得到其中component.getName返回()是...
    字符串名称=新的String();

  mntmOneToOne.addMouseListener(新MouseAdapter()
               {
                    @覆盖
                    公共无效的mouseClicked(的MouseEvent arg0中)
                    {
                        字符串名称=新的String();
                        诠释计数= arg0.getClickCount();
                        如果(计数== 1)
                        {
                          组分面板=(成分)arg0.getSource();
                          的System.out.println(panel.getName());
                        }                    }
                 });


解决方案

通过的MouseEvents的的getSource()方法获取点击的对象。然后调用它的的getName()方法。

  mntmOneToOne.addMouseListener(新MouseAdapter(){
     @覆盖
     公共无效的mouseClicked(的MouseEvent的MouseEvent){
         诠释计数= mouseEvent.getClickCount();
         如果(计数== 1){
           组分面板=(成分)mouseEvent.getSource();
           名称= panel.getName();
         }
     }
});

Below is what im trying to implement but its giving error on the line mentioned ; this is because the function doesnot get which Component.getName() is of ... String name=new String();

                mntmOneToOne.addMouseListener(new MouseAdapter()
               {
                    @Override
                    public void mouseClicked(MouseEvent arg0)
                    {
                        String name=new String();
                        int count = arg0.getClickCount();
                        if (count == 1)
                        {
                          Component panel = (Component) arg0.getSource();   


                          System.out.println(panel.getName());
                        }

                    }
                 });

解决方案

Get the clicked object via your MouseEvent's getSource() method. Then call its getName() method.

mntmOneToOne.addMouseListener(new MouseAdapter() {
     @Override
     public void mouseClicked(MouseEvent mouseEvent) {
         int count = mouseEvent.getClickCount();
         if (count == 1) {
           Component panel = (Component)mouseEvent.getSource();
           name = panel.getName(); 
         }
     }
});

这篇关于我的onclick想要得到的JPanel的名称是在JFrame present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆