In this piece of code:
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
I can see it makes a new label and adds it to the JFrame object frame.
But I want to understand what does getContentPane() do, and why do I need it?
I read this API but I still didn't understand.
解决方案
Every JPanel is a container, so either add it to a panel then add it to the container or directly use add(component) or use the getContentPane().add method. Both add the component to the container in Java 7 (I don't know if version 6 has a problem with this or not).