0

对不起新手问题 - 在实现 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) {}
    });
4

1 回答 1

5

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

btnExit.addMouseListener(new MouseAdapter() {

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

});
于 2013-04-05T18:36:41.410 回答