如何简化MouseListener,以便没有所有这些未使用的方法? [英] How do I simplify MouseListener so that I don't have all these unused methods?

查看:100
本文介绍了如何简化MouseListener,以便没有所有这些未使用的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我有以下代码,以便当有人单击关闭"时,窗口将关闭.在其下方是同一菜单栏上的另一个退出按钮,仅用于冗余(稍后将其更改为其他内容,但要点如下).我的问题是,有什么办法可以使它更简单吗?我的意思是每个菜单有四个未使用的方法,我将需要做更多的事情.有关如何解决此问题的任何想法?

Below I have the following code, so that when someone clicks on the "Close", the window will close. Below that is another exit button on the same menu bar, simply for redundancy (it'll be changed later to be something else, but the point stands as follows). My question is, is there any way to make this more simplistic? I mean there are four unused methods for every menu, and I'm going to need to do a few more. Any ideas on how to fix this?

closeFile.addMouseListener(new MouseListener() {

                public void mouseClicked(MouseEvent arg0) {
                    System.exit(0);

                }

                public void mouseEntered(MouseEvent e) {


                }

                public void mouseExited(MouseEvent e) {


                }

                public void mousePressed(MouseEvent e) {


                }

                public void mouseReleased(MouseEvent e) {


                }

            });

            exit.addMouseListener(new MouseListener() {

                public void mouseClicked(MouseEvent arg0) {
                    System.exit(0);

                }

                public void mouseEntered(MouseEvent e) {


                }

                public void mouseExited(MouseEvent e) {


                }

                public void mousePressed(MouseEvent e) {


                }

                public void mouseReleased(MouseEvent e) {


                }

            });

此外,ActionListener不能为我工作,所以我不能使用它(不相信我也应该这样做).

Also, ActionListener wouldn't work for me, so I can't use that (don't believe I'm supposed to either).

推荐答案

使用MouseAdapter并覆盖所需的方法.

Use a MouseAdapter and override the methods that you want.

closeFile.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent arg0) {
            System.exit(0);
        }
    });

这篇关于如何简化MouseListener,以便没有所有这些未使用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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