import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class FrameCreator {
public static void main(String args[]) {
//All Swing actions should be run on the Event Dispatch Thread (EDT)
//Calling SwingUtilities.invokeLater makes sure that happens.
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
//JFrames will not display without size being set
frame.setSize(500, 500);
JLabel label = new JLabel("Hello World");
frame.add(label);
frame.setVisible(true);
});
}
}
As you may notice if you run this code, the label is position in a very bad place. This is difficult to change in a good manner using the add
method. To allow more dynamic and flexible placing check out Swing Layout Managers.