抱歉新手问题 - 在实现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) {}
});
答案 0 :(得分:5)
使用MouseAdapter
并仅覆盖所需的功能
btnExit.addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent arg0) {System.exit(0);}
});