Java Homework:setVisible import

时间:2015-04-27 21:07:00

标签: java swing inheritance jframe

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

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);
    }

}

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

public void setVisible(boolean bln) {
        // compiled code
}

是我发现的,之后我尝试将window.java中的setVisible类导入到我的代码中,我尝试的所有东西都不起作用。

1 个答案:

答案 0 :(得分:8)

  

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

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

e.g。

// be sure to import java.awt.Window;
Window window = new ProductFrame();
window.setVisible(true);

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