Nathan Tolley
Nathan Tolley

Reputation: 33

Adding a JLabel under a JPanel not working (JAVA)

I'm designing a long and signup page as a test for some java projects and whilst trying to append the JLabel under the existing panel, the text does not show up.

Here is the code:

//Setting Panel Color
int r1 = 172;
int g1 = 50;
int b1 = 50;
Color myFgColor = new Color(r1,g1,b1);
JPanel panel = new JPanel();
panel.setBounds(750,60,375,420);
panel.setBackground(myFgColor);
        
//Login and Sign Up Text
JLabel label = new JLabel("LOGIN");
label.setFont(new Font("Serif", Font.PLAIN, 14));
label.setForeground(Color.white);
panel.add(label);
gui.getContentPane().add(panel);

Upvotes: 0

Views: 68

Answers (1)

MTM
MTM

Reputation: 17

Is this what you are trying to see?

window

I didn't change anything, I just created a new JFrame and put your components... The difference I see is that JFrame creates the contentPane, and you didn't:

private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Window frame = new Window();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Window() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1200, 575);
    setResizable(false);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);    

    //Setting Panel Color
    Color myFgColor = new Color(172,50,50);
    JPanel panel = new JPanel();
    panel.setBounds(750,60,375,420);
    panel.setBackground(myFgColor);
            
    //Login and Sign Up Text
    JLabel label = new JLabel("LOGIN");
    label.setFont(new Font("Serif", Font.PLAIN, 14));
    label.setForeground(Color.white);
    panel.add(label);
    getContentPane().add(panel);

    //Setting Background Color
    Color myBgColor = new Color(30,30,30);
    getContentPane().setBackground(myBgColor);
    
    //Centering Window
    setLocationRelativeTo(null);
}

I recommend you using WindowBuilder to create frames, it's a visual designer and it makes it very easy

Upvotes: 1

Related Questions