This seems like a very beginner question, but I don’t know the answer.
Suppose you create a new JFrame in Java. With the new frame comes the default content pane. If I declare a variable and initialize it as that frame’s content pane, is the variable I initialized a reference to the frame’s content pane or does it become its own thing? Example:
JFrame frame = new JFrame();
Container panel = frame.getContentPane();
If I wanted to, for example, change the layout of the content pane, would adjusting panel give the same effect as changing frame.getContentPane()?
To show exactly what I mean:
frame.getContentPane().setLayout(new GridLayout());
Would this create the same result as:
panel.setLayout(new GridLayout());
Will anything that I then do to panel then be reflected in frame without me having to then say frame.setContentPane(panel); at the end?
Absolutely. They point to the same object in memory.
Imagine tying two strings to a balloon. You hold one (let’s call you
panel) and you give the other string to a friend (let’s call himframe.getContentPane()).Now if you your friend follows his string, finds the balloon, and puts a hat on it (i.e.
frame.getContentPane().setLayout(new GridLayout());), then when you follow your string, you will find a balloon with a hat!