我希望在Windows关闭时停止声音,如何在处置时添加动作列表器?
public Tetris(boolean sound) {
statusbarLabel = new JLabel(" 0");
add(statusbarLabel, BorderLayout.SOUTH);
Board board = new Board(this);
if(sound){
Sound.gameSound.play();
}
add(board);
board.start();
setSize(200, 400);
setTitle("Tetris");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
答案 0 :(得分:1)
最简单的方法是添加WindowListener
:
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
Sound.gameSound.stop();
}
});
答案 1 :(得分:0)