-1

タイトルがあまり説明的ではないことはわかっています。質問の言い方がわかりませんでした。私が持っているのは、ウィンドウが閉じられたときにイベントをトリガーしたい GUI です (ウィンドウ/アプリケーションを強制終了したときを含む)。助けてくれてありがとう!!! これが私のコードです:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import java.io.Serializable;
public class NotAVirus extends JFrame {
private JTextField statusField = new JTextField(20);
private JButton yesButton = new JButton("Open");
private JButton noButton = new JButton("Close");
private static NotAVirus app = new ImLost();

public static void main() {
    app.setVisible(true);
    app.setLocationRelativeTo(null);
}    

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
public ImLost() {
    super("ImLost");
    statusField.setText("There's No Escape");
    statusField.setHorizontalAlignment(JLabel.CENTER);
    statusField.setEditable(false);
    add(statusField, BorderLayout.CENTER);
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(1, 2));
    p.add(yesButton);
    p.add(noButton);
    add(p, BorderLayout.SOUTH);
    yesButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) {
                app.setVisible(false);
                for(int i = 0; i <= 10000; i ++)
                {
                    JFrame frame = new JFrame("ImLost");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                    JLabel emptyLabel = new JLabel("");
                    emptyLabel.setPreferredSize(new Dimension(160, 1));
                    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

                    //Display the window.
                    frame.setLocation((int)(Math.random() * ((1280) + 1)),(int)(Math.random() * ((800) + 1)));
                    frame.pack();
                    frame.setVisible(true);
                }
            }
        });
    noButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) {
                app.setVisible(false);
                for(int i = 0; i <= 10000; i ++)
                {
                    JFrame frame = new JFrame("ImLost");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                    JLabel emptyLabel = new JLabel("");
                    emptyLabel.setPreferredSize(new Dimension(160, 1));
                    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

                    //Display the window.
                    frame.setLocation((int)(Math.random() * ((1280) + 1)),(int)(Math.random() * ((800) + 1)));
                    frame.pack();
                    frame.setVisible(true);
                }
            }
        });
    p.setPreferredSize(new Dimension(200, 35));
    pack();
}
4

1 に答える 1

2

に を追加できWindowListenerますJFrame

frame.addWindowListener(new WindowListener() {

    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
        //window is being closed
    }
    @Override
    public void windowClosed(WindowEvent e) {
        //window is closed
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }

});

ウィンドウが閉じられたときにイベントをトリガーしたい (ウィンドウ/アプリケーションを強制終了したときを含む)

「誰かがプロセスを強制終了したとき」という意味であれば、プロセスが強制終了されてすぐに実行が停止するため、それは不可能だ思います。「アプリケーションがフリーズしてユーザーが強制終了したとき」という意味であれば、それも不可能だと思います。通常、アプリケーションを強制終了すると、アプリケーションがフリーズして応答しなくなったことを意味するため、他のそのコードは不可能です。

于 2013-09-20T21:38:33.543 に答える