私のSwingチャットアプリケーションには、ユーザーをログアウトするために使用されるログアウトボタンがあり、正常に機能します。ここで、Swing アプリケーション ウィンドウを閉じるときにユーザーをログアウトする必要があります。
JavaScript を使用してブラウザーを閉じるときに Web アプリケーションでこれを行いましたが、今度は Swing アプリケーションでこれを行う必要があります。
どうすればこれを達成できますか?
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);
}
}
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);
}
}
これは仕事をするでしょう