This example should show you the correct order in which to perform the operations you want. Also, the SwingUtilities.invokeLater()
method is the way you should start a swing application. This uses the EDT which you can read about here. Another note is that you set the size of the frame, but when it displays it will be much smaller. That is because you have used frame.pack()
which will make sure that only the space you really need is used up.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Graph {
JFrame frame;
public Graph() {
frame = new JFrame("My Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1366,770);
frame.setLocationRelativeTo(null);
JLabel label = new JLabel("SomeLabel");
JPanel panel = new JPanel(new BorderLayout());
panel.add(label, BorderLayout.CENTER);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Graph g = new Graph();
}
});
}
}