Java Homework使用继承

时间:2014-12-11 23:27:07

标签: java swing constructor jwindow

我正在做功课,要求我研究java api,该类由JFrame类继承,该类声明了setVisible方法。然后,导入该类并修改main方法中的代码,以便将frame变量声明为该类型而不是JFrame类型。

我发现什么类声明了setVisible方法,JWindow,但是每当我尝试更改代码时它都不会运行,所以任何帮助都会受到赞赏。

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

2 个答案:

答案 0 :(得分:2)

你发现某个类有一个方法" setVisible()"。 但是,您的任务是找到JFrame继承的类。

JFrame不会从JWindow继承。

换句话说: 如果你的任务是找到你的祖先,并采访他们的工作,那么采访你的姐姐或表弟将无法完成工作。

答案 1 :(得分:1)

由于您的班级ProductFrame直接延伸JFrame,您无法说出

JWindow frame = new ProductFrame();

JWindow引用设置为ProductFrame无效是因为ProductFrame未延伸JWindow。您需要更改类声明,以便它可以。