简体   繁体   中英

Java Swing - Text and buttons not showing up

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.

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.

You forgot to add the label to the frame :)

add(label, BorderLayout.CENTER);

Whatever layout you wish to use...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM