Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing - Text and buttons not showing up

Tags:

java

swing

Trying everything, just not showing up:

package me.ultimate.ST;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class ST extends JFrame {

    private static final long serialVersionUID = 1L;

    public ST() {
        setSize(500, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setUndecorated(true);
        getContentPane().setBackground(Color.BLACK);
        JLabel label = new JLabel("Test");
        label.setText("Some Test!");
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ST ex = new ST();
                ex.setVisible(true);
            }
        });
    }
}

I then just get a black box.

like image 807
PaulBGD Avatar asked Feb 03 '26 17:02

PaulBGD


2 Answers

You need to add the label to the frame:

label.setText("Some Test!");
add(label);

I suggest you read the Swing tutorial for the basics. Maybe the section on How to Use Labels would be a good place to start. The tutorial will also show you a better way to design your class wo that you follow Swing guidelines.

like image 86
camickr Avatar answered Feb 06 '26 07:02

camickr


You forgot to add the label to the frame :)

add(label, BorderLayout.CENTER);

Whatever layout you wish to use...

like image 33
Sesame Avatar answered Feb 06 '26 06:02

Sesame



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!