Ted West
Ted West

Reputation: 81

Trouble adding label to frame

I'm attempting to add a JLabel ontop of a JPanel, but i can't seem to get it to appear, and i'm not sure why.

JFrame frame = new JFrame();
    frame.setTitle("testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = (JPanel) frame.getContentPane();
    panel.setPreferredSize(new Dimension(SCREENX, SCREENY));
    panel.setLayout(null);
    setBounds(0, 0, SCREENX, SCREENY);
    panel.add(this);
    setIgnoreRepaint(true);

    frame.setResizable(false);

    frame.setLocationRelativeTo(null);


    JLabel scoreLabel = new JLabel();
    scoreLabel.setText("Points: "+points);
    panel.add(scoreLabel);
    frame.setContentPane(panel);
    frame.pack();
    scoreLabel.setVisible(true);

Upvotes: 0

Views: 28

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You've made the JPanel's layout null, essentially shooting yourself in the foot by doing this, and then add a JLabel without specifying its location or size, something that needs to be done when you shoot yourself in the foot like this.

Solution: don't shoot yourself in the foot in the first place. Use layouts and learn to use them well.

If you need more specific layout recommendations, please give us more information about your gui structure and its desired structure as well as pictures if you have them.

Upvotes: 2

Related Questions