0

When i run this code:

public class Menu extends JFrame implements ActionListener {


JLabel logo = new JLabel("MyChef");

JPanel north = new JPanel();

public void main(String args[]){
new Menu();
}

Menu(){
JFrame frame = new JFrame();

frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setTitle("MyChef");
frame.setSize(500, 300);
frame.setVisible(true);
frame.setResizable(false);

frame.add(north, BorderLayout.NORTH);
north.add(logo);

}

public void actionPerformed(ActionEvent e){

}
}

The window opens but it does not show anything... Where is my label? I am very lost because I have done various GUI before and either I am being stupid or i don't know! Sorry if its a stupid question, I'm just so stuck I had to post this.

  • [A common problem](http://stackoverflow.com/questions/24448031/jframe-not-presenting-any-components) -- please search the site before asking, and you'd quickly find the solution. – Hovercraft Full Of Eels Aug 20 '15 at 23:09

1 Answers1

1

Your code works for me, but I think it doesn't for you because you add a component after you set the frame visible. Call frame.setVisible(true) (and setSize) after

frame.add(north, BorderLayout.NORTH);
north.add(logo);

So your code should look like this (also formatted it properly):

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Menu extends JFrame implements ActionListener {

    JLabel logo = new JLabel("MyChef");

    JPanel north = new JPanel();

    public static void main(String args[]) {
        new Menu();
    }

    public Menu() {

        JFrame frame = new JFrame();

        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setTitle("MyChef");
        frame.setResizable(false);

        frame.add(north, BorderLayout.NORTH);
        north.add(logo);

        frame.setSize(500, 300);
        frame.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {

    }
}
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35