简体   繁体   English

匿名 class 实现接口,不能有 arguments

[英]Anonymous class implements interface, cannot have arguments

i have some code that i am trying to recompile and understand, but i have a method that does not compile and i wish to find the good work around.我有一些我正在尝试重新编译和理解的代码,但我有一个无法编译的方法,我希望找到好的解决方法。 the method is as follows.方法如下。

private void launchEventPanel(String title) { 
    EventQueue.invokeLater(new Runnable(title) { 
        public void run() { 
        JFrame myFrame = new JFrame(); 
        myFrame.setTitle("Conference Call"); 
        myFrame.setIconImage(CallConference.this.myCore.myPanel.myIconManager.getPromptIcon(CallEMart.class.toString())); 
        myFrame.getContentPane().add(CallConference.this.myEventPanel, "Center"); 
        myFrame.pack(); 
        myFrame.setVisible(true); } }); }

the second line of the EventQueue.invokeLater does not compile, i get the error "Anonymous class implements interface, cannot have arguments". EventQueue.invokeLater 的第二行没有编译,我收到错误“Anonymous class implements interface, cannot have arguments”。

any help and work around is highly appreciated.非常感谢任何帮助和解决方法。 thanks!谢谢!

That's because Runnable is just an interface and it doesn't take any argument as an anonymous class. To get around it, you can assign final to the upper parameter:那是因为 Runnable 只是一个接口,它不接受任何参数作为匿名 class。要绕过它,您可以将 final 分配给上层参数:

private void launchEventPanel(final String title) { 
    EventQueue.invokeLater(new Runnable() { 
        public void run() { 
        JFrame myFrame = new JFrame(); 
        myFrame.setTitle("Conference Call");  
        myFrame.setIconImage(CallConference.this.myCore.myPanel.myIconManager.getPromptIcon(CallEMart.class.toString())); 
        myFrame.getContentPane().add(CallConference.this.myEventPanel, "Center"); 
        myFrame.pack(); 
        myFrame.setVisible(true); } }); }

Well, it is what it says, you can't give arguments to a Runnable "constructor" because there is no constructor -- it's an interface.好吧,这就是它所说的,您不能将 arguments 提供给Runnable “构造函数”,因为没有构造函数——它是一个接口。

Instead, declare title final , and use it directly inside the inner class.相反,声明title final ,并直接在内部 class 中使用它。

private void launchEventPanel(final String title) { 
    EventQueue.invokeLater(new Runnable() { 
        public void run() { 
        JFrame myFrame = new JFrame(); 
        myFrame.setTitle("Conference Call"); 
        myFrame.setIconImage(CallConference.this.myCore.myPanel.myIconManager.getPromptIcon(CallEMart.class.toString())); 
        myFrame.getContentPane().add(CallConference.this.myEventPanel, "Center"); 
        myFrame.pack(); 
        myFrame.setVisible(true); } }); }

And you're done.你完成了。 Although as far as I can see, you're not actually using the value of title anywhere in your code.尽管据我所知,您实际上并没有在代码中的任何地方使用title的值。

You can convert your anonymous class into nested class. It will allow you to pass parameters into constructor.您可以将匿名 class 转换为嵌套的 class。它将允许您将参数传递给构造函数。

private static final MyRunnable implements Runnable {

    private final String title;

    public MyRunnable(String title) {
        this.title = title;
    }

    @Override
    public void run() {
        // use title here
    }

}    

Runnable is an interface and so it does not contain a constructor which accepts a string, which you are doing in this line: EventQueue.invokeLater(new Runnable(title) { Runnable是一个接口,因此它不包含接受字符串的构造函数,您在这一行中正在这样做: EventQueue.invokeLater(new Runnable(title) {

If you want to use title in public void run() , just make title a final argument and you are free to use it within that method.如果你想在public void run()中使用title ,只需将 title 作为最后一个参数,你就可以在该方法中自由使用它。

Well, as the message says: java.lang.Runnable is an interface so you cannot pass title to its constructor.好吧,正如消息所说:java.lang.Runnable 是一个接口,因此您不能将标题传递给它的构造函数。 Use:采用:

EventQueue.invokeLater(new Runnable() { 

instead.反而。

Note that title isn't used anywhere.请注意,title 没有在任何地方使用。 If you need it inside the Runnable, you need to declare it final:如果在 Runnable 中需要它,则需要将其声明为 final:

private void launchEventPanel(final String title) { 
EventQueue.invokeLater(new Runnable() { 
    public void run() { 
    JFrame myFrame = new JFrame(); 
    myFrame.setTitle(title); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM