-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 回答