在侦听器创建时省略空函数签名

时间:2013-04-05 18:30:26

标签: java inner-classes

抱歉新手问题 - 在实现MouseListener类(或其他每个内部类)时是否可以省略空函数签名?
所以代码会缩小几行?

    JButton btnExit = new JButton("Exit");
    btnExit.addMouseListener(new MouseListener() {
        @Override public void mouseReleased(MouseEvent arg0) {}
        @Override public void mousePressed(MouseEvent arg0) {System.exit(0);}
        @Override public void mouseExited(MouseEvent arg0) {}
        @Override public void mouseEntered(MouseEvent arg0) {}
        @Override public void mouseClicked(MouseEvent arg0) {}
    });

1 个答案:

答案 0 :(得分:5)

使用MouseAdapter并仅覆盖所需的功能

btnExit.addMouseListener(new MouseAdapter() {

    @Override public void mousePressed(MouseEvent arg0) {System.exit(0);}

});