プログラムでSwingフレームを閉じる最良の方法は、「X」ボタンが押されたときのように動作させることです。そのためには、ニーズに合ったWindowAdapterを実装し、フレームのデフォルトのクローズ操作を何もしないように設定する必要があります(DO_NOTHING_ON_CLOSE)。
次のようにフレームを初期化します。
private WindowAdapter windowAdapter = null;
private void initFrame() {
    this.windowAdapter = new WindowAdapter() {
        // WINDOW_CLOSING event handler
        @Override
        public void windowClosing(WindowEvent e) {
            super.windowClosing(e);
            // You can still stop closing if you want to
            int res = JOptionPane.showConfirmDialog(ClosableFrame.this, "Are you sure you want to close?", "Close?", JOptionPane.YES_NO_OPTION);
            if ( res == 0 ) {
                // dispose method issues the WINDOW_CLOSED event
                ClosableFrame.this.dispose();
            }
        }
        // WINDOW_CLOSED event handler
        @Override
        public void windowClosed(WindowEvent e) {
            super.windowClosed(e);
            // Close application if you want to with System.exit(0)
            // but don't forget to dispose of all resources 
            // like child frames, threads, ...
            // System.exit(0);
        }
    };
    // when you press "X" the WINDOW_CLOSING event is called but that is it
    // nothing else happens
    this.setDefaultCloseOperation(ClosableFrame.DO_NOTHING_ON_CLOSE);
    // don't forget this
    this.addWindowListener(this.windowAdapter);
}
次のように、WINDOW_CLOSINGイベントを送信して、プログラムでフレームを閉じることができます。
WindowEvent closingEvent = new WindowEvent(targetFrame, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closingEvent);
これは、「X」ボタンが押されたようにフレームを閉じます。