-2

我正在做作业,要求我研究声明 setVisible 方法的 JFrame 类继承哪个类的 java api。然后,导入该类并修改 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);
    }
}
4

2 回答 2

2

您发现了一些具有“setVisible()”方法的类。但是,您的任务是找到一个 JFrame 继承自的类。

JFrame 不继承自 JWindow。

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

于 2014-12-11T23:47:01.180 回答
1

由于您的课程直接ProductFrame扩展JFrame,您不能说

JWindow frame = new ProductFrame();

将引用设置为JWindow等于 a是无效的,ProductFrame因为ProductFrame不扩展JWindow。您需要更改类声明才能做到这一点。

于 2014-12-11T23:33:58.523 回答