I wrote my program but I’m not getting why its possible
to possible to write
frame.getContentPane().add(label);
I’m confused on the order of methods for the above code that corresponds with the full code below.
Is the add method calling the label object then calling getContentPane method to the frame object. Anyone can enlighten me regarding this concept. It would help a great deal if I can fully understand java much better. 🙂
JFrame frame = new JFrame ("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set the label
JLabel label = new JLabel("Hello world");
frame.getContentPane().add(label);
frame.setVisible(true);
frame.getContentPane()returns a Container that the JFrame holds (actually a JPanel) that acts as its contentPane. Then theadd(...)method adds the JLabel to the contentPane. The order is left to right.This is equivalent: