怎么问“你确定吗?”在关闭JInternalFrame之前?

时间:2013-09-05 09:54:23

标签: java swing

我用JInternal框架创建了一个java swing应用程序。当我关闭内部框架时,它应显示一条弹出消息“你确定吗?”在“选项”窗格中。如何将事件放在JInternal框架的关闭操作中?请帮帮我。

1 个答案:

答案 0 :(得分:8)

有关如何撰写InternalFrameListener的信息,请参阅http://docs.oracle.com/javase/tutorial/uiswing/events/internalframelistener.html。您可以将所有代码放在internalFrameClosing(InternalFrameEvent e)方法中。

然后,只需使用InternalFrameListener方法JInternalFrame向您的JInternalFrame添加新创建的addInternalFrameListener的新实例,如上一个链接所示。< / p>

以下是有关addInternalFrameListener方法的信息:JavaDoc

代码:

public class InternalFrameEventDemo implements InternalFrameListener{
    JInternalFrame frame;
    public InternalFrameEventDemo() {
        frame = new JInternalFrame("Event Generator",
                                                      true,  //resizable
                                                      true,  //closable
                                                      true,  //maximizable
                                                      true); //iconifiable
        //so that you can decide whether to close it or not
        frame.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
        frame.addInternalFrameListener(this);
    }
    public void internalFrameClosing(InternalFrameEvent e) {
        //YOUR CODE HERE
        int option = JOptionPane.showConfirmDialog(null, "Really Close?", "Exit", JOptionPane.YES_NO_OPTION);  
        if (option == JOptionPane.YES_OPTION){
            //call dispose to really close it
            dispose();  
        }  
    }
    public void internalFrameClosed(InternalFrameEvent e) {

    }
    public void internalFrameOpened(InternalFrameEvent e) {

    }
    public void internalFrameIconified(InternalFrameEvent e) {

    }
    public void internalFrameDeiconified(InternalFrameEvent e) {

    }
    public void internalFrameActivated(InternalFrameEvent e) {

    }
    public void internalFrameDeactivated(InternalFrameEvent e) {

    }

    public static void main(String[] args) {
        new InternalFrameEventDemo();
    }
}