The problem is that you extend the JFrame class from your class, but you initiate your own JFrame in main() , also you cannot call add() , because you are in the main, which is static while add() requires, so that the instance was created before the call, see my comment on your question, however you should not extend the JFrame and, rather, do:
public class CalebBreckonHW3 { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame(); frame.setTitle("Greeting App");
also always creates an EDT (Thread Dispatch Thread), on which all user interface components lie. I did this using SwingUtilities.invokeLater(); , also always adding a JFrame contentPane via getContentPane().add(); . Another thing is why are you adding your Jframe to your Jframe? As you can see, I showed an example of adding JPanel
EDIT:
you make your JFrame global variable, and then you can use frame.setLayout() , etc.
however, if you want your current code to work, follow these steps:
public class CalebBreckonHW3 extends JFrame{ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new CalebBreckonHW3().createandShowUI(); } }); } private void createAndShowUI() { setTitle("Greeting App");
but a JFrame not recommended
source share