我想在窗口关闭时停止声音,如何在处置时添加 Action 监听器?
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);
}
最佳答案
最简单的方法是添加一个WindowListener
:
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
Sound.gameSound.stop();
}
});
关于java - 如何在处理时添加 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13851284/