1

私のSwingチャットアプリケーションには、ユーザーをログアウトするために使用されるログアウトボタンがあり、正常に機能します。ここで、Swing アプリケーション ウィンドウを閉じるときにユーザーをログアウトする必要があります。

JavaScript を使用してブラウザーを閉じるときに Web アプリケーションでこれを行いましたが、今度は Swing アプリケーションでこれを行う必要があります。

どうすればこれを達成できますか?

4

2 に答える 2

6
  • 電話JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
  • フレームに を追加しWindowListenerます。
  • リスナーの適切なメソッドをオーバーライドして終了メソッドを呼び出し、フレームを非表示に設定して破棄します。

例えば

ダイアログ、フレーム終了のチェッ​​ク

import java.awt.*;
import java.awt.event.*;
import java.net.URI;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

class CheckExit {

    public static void doSomething() {
        try {
            // do something irritating..
            URI uri = new URI(
                    "http://stackoverflow.com/users/418556/andrew-thompson");
            Desktop.getDesktop().browse(uri);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setPreferredSize(new Dimension(400, 100));
                gui.setBackground(Color.WHITE);

                final JFrame f = new JFrame("Demo");
                f.setLocationByPlatform(true);
                f.add(gui);

                // Tell the frame to 'do nothing'.
                f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

                WindowListener listener = new WindowAdapter() {

                    @Override
                    public void windowClosing(WindowEvent we) {
                        int result = JOptionPane.showConfirmDialog(
                                f, "Close the application");
                        if (result==JOptionPane.OK_OPTION) {
                            doSomething();
                            f.setVisible(false);
                            f.dispose();
                        }
                    }
                };
                f.addWindowListener(listener);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
于 2012-12-10T12:11:04.290 に答える
1

JFrame でウィンドウ イベントを使用します。たとえば、必要なメソッド (windowclosed();) があります。それは WindowListener です

編集 :

あなたが言うことができます

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

ただし、X (閉じるボタン) を押しても Windowlistener は機能します。

そこで、このコードでメソッドwindowClosingをオーバーライドします

public void windowClosing(WindowEvent e) {
    int i = JOptionPane.showConfirmDialog(TestFrame.this, "do you really want to close?","test",JOptionPane.YES_NO_OPTION);
    if(i == 0) {
        System.exit(0);
    }
}

これは仕事をするでしょう

于 2012-12-10T12:10:16.167 に答える