使用继承的Java作业
我正在做家庭作业,要求我研究JavaAPI,声明setVisible方法的JFrame类继承了该类。然后,导入该类并修改main方法中的代码,以便将frame变量声明为该类型而不是JFrame类型 我已经找到了什么类声明了setVisible方法JWindow,但每当我试图更改代码时,它都不会运行,因此希望您提供帮助使用继承的Java作业,java,swing,constructor,jwindow,Java,Swing,Constructor,Jwindow,我正在做家庭作业,要求我研究JavaAPI,声明setVisible方法的JFrame类继承了该类。然后,导入该类并修改main方法中的代码,以便将frame变量声明为该类型而不是JFrame类型 我已经找到了什么类声明了setVisible方法JWindow,但每当我试图更改代码时,它都不会运行,因此希望您提供帮助 import javax.swing.JFrame; import javax.swing.JWindow; //JFrame is inherited from java.a
import javax.swing.JFrame;
import javax.swing.JWindow;
//JFrame is inherited from java.awt.Frame class
//setVisible is declared by the java.awt.Window class
public class ProductFrame extends JWindow
{
public ProductFrame()
{
// all of these methods are available because
// they are inherited from the JFrame class
// and its superclasses
this.setTitle("Product");
this.setSize(200, 200);
this.setLocation(10, 10);
this.setResizable(false);
// this method uses a field that's available
// because it's inherited
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
// this creates an instance of the ProductFrame
JWindow frame = new ProductFrame();
// this displays the frame
frame.setVisible(true);
}
}
由于类
ProductFrame
直接扩展了JFrame
,所以不能说
JWindow frame = new ProductFrame();
将
JWindow
引用设置为等于ProductFrame
是无效的,因为ProductFrame
不扩展JWindow
。您需要更改类声明,使其生效。您发现某个类有一个方法“setVisible()”。
但是,您的任务是查找JFrame继承自的类
JFrame不从JWindow继承
换言之:
如果你的任务是找到你的祖先,并就他们的工作采访他们,采访你的姐姐或表妹将无法完成工作。你的问题对我来说非常模糊和不清楚。也许只有我?!!!对不起,我不是想含糊其辞。也许我从教科书上得到的是模糊的lol,我正在尝试将JFrame扩展到JWindow,并且代码仍能正常运行。如果你看一下JavaDocs,你会发现其中没有标记“setDefaultCloseOperation”。它是由
JFrame
直接声明的是的,我看到了。我不知道我应该如何修改代码,并且仍然能够正常运行。JWindow类中是否有类似的方法?