简体   繁体   English

实现相同接口的java setVisible(true)类

[英]java setVisible(true) class that implements same interface

I have some classes that extends Jframe and implements same interface. 我有一些扩展Jframe并实现相同接口的类。

public class classe extends JFrame implements Elenco{
@Override
   public void prova(){
   .......
   }       

}

In another class i have: 在另一堂课中,我有:

 public class Selezione{
 Elenco e;
 e.Prova();
 }

In this class how can i show class that implements interface elenco? 在该课程中,我如何显示实现接口elenco的课程?
i can't do e.setVisible(true); 我不能做e.setVisible(true);

I try this solution but it doesn't work 我尝试了这种解决方案,但是没有用

 public class Selezione{
Elenco e;
 public Modifica(){
 e.Prova();

 }

public void Transfert(JFrame frame) {
   frame = (JFrame) e;  

} }

You have to add the setVisible method to the interface Elenco . 您必须将setVisible方法添加到接口Elenco Or you change the type from Elenco to JFrame this would also be possible with casting. 或者,您可以将类型从Elenco更改为JFrame这也可以通过强制转换实现。

Also your code does not work this way, you have to change it to: 同样,您的代码无法以这种方式工作,您必须将其更改为:

public interface Elenco {
    ...
    public void setVisible(boolean visible);
}

public class Selezione {
    Elenco e;

    public void do() {
        e.prova();
        e.setVisible(true);
    }
}

If you want to check whether an Elenco object is actually a JFrame , you can use the instanceof operator with an if statement: 如果要检查Elenco对象是否实际上是JFrame ,可以将instanceof运算符与if语句一起使用:

if (e instanceof JFrame) {
    // Here I cast e to type JFrame
    JFrame frame = (JFrame)e;
    // Now you can call setVisible!
    frame.setVisible(true);
}

It's basically just a cast. 基本上只是演员阵容。

Alternatively, you can add a method to Elenco called setVisible : 另外,您可以向Elenco添加一个名为setVisible的方法:

interface Elenco {
    void prova();
    void setVisible(boolean visible);
}

Since your classe extends JFrame and JFrame already contains the setVisble method, you don't need to add any extra methods. 由于您的classe延伸JFrameJFrame已经包含了setVisble方法,你不需要添加任何额外的方法。

If you use the second approach, you can call setVisible directly on e , without the cast: 如果使用第二种方法,则可以直接在e上调用setVisible ,而无需强制转换:

e.setVisible(true);

If you wish to use other methods in JFrame , you can add them to your interface, too. 如果您希望在JFrame使用其他方法,也可以将它们添加到您的界面中。

The third approach is to directly store a JFrame in Selezione : 第三种方法是将JFrame直接存储在Selezione

public class Selezione {
    JFrame frame;
}

But this is not very recommended because we should keep the abstraction in a class consistent. 但这不是很推荐,因为我们应该在一个类中保持抽象的一致性。

Your problem is the the field e has the reference type Elenco and at compile time you can't call other methods but those found in that interface. 您的问题是字段e的引用类型为Elenco,并且在编译时无法调用其他方法,但只能在该接口中找到那些方法。 If you wish to call setVisible, you should cast e to class classe (which is an awful class name). 如果要调用setVisible,则应将e强制转换为类classe(这是一个糟糕的类名)。 After casting you could call dirrectly setVisible(). 投射后,您可以直接调用setVisible()。 Other method would be to add the setVisible code in the classes's implementation of the prova method 其他方法是在prova方法的类的实现中添加setVisible代码

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

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