Why isn't my label showing up? (Java)

Elliot

I'm trying to make something but my label "money" isn't showing up and I don't know why. I've been trying to fix it for a while. This is my code:

     public class main {
static int amountOM = 200;
static JFrame jf = new JFrame("Game");
static JPanel jp = new JPanel();
static JPanel jp2 = new JPanel();
static JButton exit = new JButton("Exit");
static JLabel money = new JLabel("$ "+amountOM);


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

public static void start() {


    jf.setLayout(null);
    jf.pack();

    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setSize(500, 500);


    jf.setLocationRelativeTo(null);
    jf.setResizable(false);

    jp.setLocation(0, 0);
    jp.setSize(400, 500);

    jp2.setLocation(400, 0);
    jp2.setSize(100, 500);

    exit.setLocation(405, 10);
    exit.setSize(85,50);

    money.setLocation(405, 200);
    money.setSize(100,50);

    jp2.setBackground(Color.gray);
    money.setBackground(Color.black);

    jf.add(jp);
    jf.add(jp2);
    jf.add(exit);
    jf.add(money);

    jf.setVisible(true);

}
 }

If you know the problem, please tell me. Thank you.

(It keeps telling me that my post is mostly code and to please add more details so im adding this text right here)

Hovercraft Full Of Eels

You're covering your JLabel with a JPanel, jp2 to be exact. The solution though is to not use absolute positioning and null layouts because it's too easy to shoot yourself in the foot just like you're doing. As all similar answers to this type of question will tell you: learn and use the layout managers.

While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

You can find the layout manager tutorial here: Layout Manager Tutorial, and you can find links to the Swing tutorials and to other Swing resources here: Swing Info.

For example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.*;

public class Game2 extends JPanel {

    private static final int BOX_W = 400;
    private static final int BOX_H = BOX_W;
    private static final Color SIDE_BG = Color.GRAY;
    private JLabel moneyLabel = new JLabel("$20.00");

    public Game2() {
        JButton exitButton = new JButton("Exit");
        JPanel exitBtnPanel = new JPanel();
        exitBtnPanel.add(exitButton);
        exitBtnPanel.setOpaque(false);
        int eb = 2;
        exitBtnPanel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));

        JPanel moneyPanel = new JPanel();
        moneyPanel.add(moneyLabel);
        moneyPanel.setOpaque(false);

        JPanel sidePanel = new JPanel();
        sidePanel.setBackground(SIDE_BG);
        sidePanel.setLayout(new BoxLayout(sidePanel, BoxLayout.PAGE_AXIS));
        sidePanel.add(exitBtnPanel);
        sidePanel.add(moneyPanel);

        setLayout(new BorderLayout());
        add(Box.createRigidArea(new Dimension(BOX_W, BOX_H)));
        add(sidePanel, BorderLayout.LINE_END);
    }

    private static void createAndShowGui() {
        Game2 mainPanel = new Game2();

        JFrame frame = new JFrame("Game2");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related