内部类的WindowAdapter [英] WindowAdapter In Inner Class

查看:110
本文介绍了内部类的WindowAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事Java学校任务,认为我已经完成了所有工作,但后来又重新审视了这些要求并意识到我忽略了一个特定的要求,现在我需要重写一段代码,但我不知道我不明白要求是什么要求。

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 now I need to rewrite a section of code, but I don't understand what the requirements are asking me for.

我需要在GUI窗口关闭时发生一些事情,目前我把它写成一个简单的方法,它工作正常但是赋值是说处理程序应该是扩展WindowAdapter类的内部类的对象。我对内部类和扩展有了基本的了解,但这说它需要是一个对象?我很困惑。

I need something to happen when the GUI window closes, currently I have it written as a simple method and it works fine, but the assignment is saying "the handler should be an object of an inner class that extends the WindowAdapter class." I've got a basic understanding of inner classes and extending, but this saying it needs to be an object? I'm confused.

目前我只是将它作为GUI类的一部分,它工作正常,我知道要求的东西不同但我不喜欢我不知道怎么做:

Currently I just have this as a part of the GUI class, and it works fine, I know the requirements are asking for something different but I don't know how to do it:

addWindowListener (new WindowAdapter()
{
    public void windowCloses(WindowEvent e)
    {
           try
           {
                //Code logic
           }
           catch (Exception ex)
           {
             //Error print
             e.getWindow().dispose();
           }
    }

正如我所说,现在每个人都工作正常,但我认为这不是必需的。我会问我的教授,但我不认为他会及时回复我截止日期。

As I said, everyone works fine now, but I don't think this is what is required. I would ask my professor but I don't think he'll get back to me in time for the deadline.

如何重写此代码以满足要求?'

How do I go about rewriting this code to meet the requirements?'

谢谢!

编辑:根据我目前收到的信息添加到此(谢谢!)

edit: Adding to this based on the information I've received so far (thanks!)

class innerClass extends WindowAdapter {

      public void windowClosing(WindowEvent e){

           //Same logic as above
      }
}

我想我弄清楚了。那么现在我创建一个这个类的实例并将windowListener添加到那个?

I think I figured it out. So now I create an instance of this class and add the windowListener to that?

推荐答案

看起来你说得对不对!

现在我是什么假设您的老师意味着:处理程序应该是扩展 WindowAdapter 类的内部类的实例(而不是对象)

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.

通过执行以下操作:

new WindowAdapter() {

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

...你实际上正在创建一个新的匿名扩展WindowAdapter 。在这个新定义中,您覆盖 WindowAdapter#windowCloses 方法,然后在新增实例化你作为参数传递给 addWindowListener 的对象

... 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!

这篇关于内部类的WindowAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆