Java Homework:setVisible import

gjt*_*123 1 java inheritance swing jframe

我正在做一些java的家庭作业,我很难理解我做错了什么.在这一步中,我需要确定JFrame类继承的哪个类声明了setVisible方法,然后导入该类并修改main方法中的代码,以便将frame变量声明为该类型而不是JFrame类型.

import javax.swing.JFrame;

public class ProductFrame extends JFrame {
    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
        JFrame frame = new ProductFrame();

        // this displays the frame
        frame.setVisible(true);
    }

}
Run Code Online (Sandbox Code Playgroud)

我会发布我一直在尝试的内容,但它没有意义,也不值得复制.我的发现是setVisible来自Window.java,和

public void setVisible(boolean bln) {
        // compiled code
}
Run Code Online (Sandbox Code Playgroud)

是我发现的,之后我尝试从Window.java导入setVisible类到我的代码,我尝试的一切都不起作用.

Hov*_*els 8

我需要确定JFrame类继承的哪个类声明了setVisible方法,然后导入该类并修改main方法中的代码,以便将frame变量声明为该类型而不是JFrame类型.

Java API会告诉你这个.中查找JFrame的API项,然后在该网页上,搜索的setVisible方法,它表明,该方法属于窗口类:java.awt.Window(窗口API入口).因此,如果您需要做的只是调用setVisible(true)它,您可以使用Window变量代替JFrame变量.请注意,如果您需要此变量中的任何其他JFrame特定功能,例如获取contentPane,则Window类型将不起作用.

例如

// be sure to import java.awt.Window;
Window window = new ProductFrame();
window.setVisible(true);
Run Code Online (Sandbox Code Playgroud)

最重要的是让您学习使用API​​,因为它将回答您的大部分 Java问题.