Reputation: 25
I'm working on some java homework and am having difficulty understanding what I am doing wrong. In this step, I am required to determine which class inherited by the JFrame class 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 as a JFrame type.
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);
}
}
I would post what I have been trying, but it doesn't make sense and isn't worth copying. My findings are that setVisible is from Window.java, and
public void setVisible(boolean bln) {
// compiled code
}
is what I find, after that I try to import the setVisible class from Window.java to my code, and everything I try doesn't work.
Upvotes: 1
Views: 1289
Reputation: 285401
I am required to determine which class inherited by the JFrame class 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 as a JFrame type.
The Java API will tell you this. Look up the JFrame API entry, and then on that page, search for the the setVisible
method and it shows that this method belongs to the Window class: java.awt.Window
(Window API entry). So you can use a Window variable in place of your JFrame variable if all you need to do is call setVisible(true)
on it. Note that if you need any more JFrame specific functionality from this variable, such as getting the contentPane, then Window type won't work.
e.g.
// be sure to import java.awt.Window;
Window window = new ProductFrame();
window.setVisible(true);
The bottom line is for you to learn to use the API as it will answer most of your Java questions.
Upvotes: 8