Java Homework using inheritence

150 views Asked by At

I'm working on homework that asks me to research the java api for which class is inherited by the JFrame class that declares the setVisible method. Then, import that class and modify the code in the main method so the frame variable is declared as that type rather than the JFrame type.

I've found what class declares the setVisible method, JWindow, but anytime I try to change the code it won't run so any help would be appreciated.

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

There are 2 answers

0
null On BEST ANSWER

You found some class that has a method "setVisible()". However, your task was to find a class that JFrame inherits from.

JFrame does not inherit from JWindow.

In other words: If you had the task to find an ancestor of yours and interview them about their job, interviewing your sister or cousin will not get the job done.

0
Bill the Lizard On

Since your class ProductFrame extends JFrame directly, you cannot say

JWindow frame = new ProductFrame();

It's not valid to set a JWindow reference equal to a ProductFrame because ProductFrame does not extend JWindow. You need to change the class declaration so that it does.