如何在处置时添加actionlistener

时间:2012-12-13 00:54:27

标签: java dispose actionlistener

我希望在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);

}

2 个答案:

答案 0 :(得分:1)

最简单的方法是添加WindowListener

addWindowListener(new WindowAdapter() {

   @Override
   public void windowClosing(WindowEvent e) {
     Sound.gameSound.stop();
   }
});

答案 1 :(得分:0)