关于#java#JLabel不显示的问题,请各位专家解答!

Java初学者JLabel不显示


package Homework;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;

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

public class GUI1_8_6 extends JFrame {
    JLabel tag1, tag2;

    GUI1_8_6() {
        this.setSize(600, 400);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        tag1 = new JLabel("我将参加Java程序设计考试。");
        tag1.setBackground(Color.red);
        tag1.setVisible(true);

        Container con = new Container();
        con.setLayout(new FlowLayout());
        con.add(tag1);
    }

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        GUI1_8_6 obj = new GUI1_8_6();
        obj.setVisible(true);
    }

}

img

在当前自定义jfame里 设置 布局,再添加label
参考如下:

//Container con = new Container();
this.setLayout(new FlowLayout());
this.add(tag1);

img


如有帮助,欢迎采纳哈!

在这里插入图片描述

本人的开源项目,欢迎star支持下!!!