WindowAdapter In Inner Class

前端 未结 2 1916
梦毁少年i
梦毁少年i 2021-01-28 13:56

I\'m working on a Java school assignment, thought I had it all finished, but then went over the requirements one more time and realized I overlooked a specific requirement and n

相关标签:
2条回答
  • 2021-01-28 14:29

    looks like you got everything right!

    Now what I assume your teacher means is: the handler should be an instance (not "object") of an inner class that extends the WindowAdapter class.

    By doing the following:

    new WindowAdapter() {
    
        // Use annotations, it's useful :)
        @Override
        public void windowCloses(WindowEvent e) {
            // ...
        }
    }
    

    ... you're actually creating a new anonymous class that extends WindowAdapter. In this new definition, you override the WindowAdapter#windowCloses method, then you instantiate it in a new object that you pass as an argument to addWindowListener.

    Apparently, your teacher expects you to actually define an inner class and create a new instance of it. I don't wanna just give you the code for this but you're god damn close, you'll figure it out in an instant!

    0 讨论(0)
  • 2021-01-28 14:53

    To what I can understand, this should help :

     addWindowListener(new WindowAdapter() {
          public void windowCloses(WindowEvent e) {
            try{
                int confirmed = JOptionPane.showConfirmDialog(null, 
                "This is my handler. Shall I Exit?", "Exit Message Box, JOptionPane.YES_NO_OPTION);
                if (confirmed == JOptionPane.YES_OPTION) {                                        
                  dispose();
                }
             }
          catch(Exception ex){
              //Error print
            e.getWindow.dispose()
          }
        });
    
    0 讨论(0)
提交回复
热议问题