Adding a Label to a JFrame

Cayden Humphreys :
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Class {

  public static void main(String[] args) {
    //Establishing the JFrame
    JFrame frame = new JFrame("Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    frame.setSize(800, 500);
    //adding labels
    JLabel label1 = new JLabel("Label Text", JLabel.RIGHT);
    label1.setText("Today's Task");
    label1.setVisible(true);
    label1.setVerticalAlignment(JLabel.TOP);
  }
}

I have looked into creating a label for a JFrame but for some reason I am just bamboozled. If someone could please enlighten me as to why my label is not appearing in my JFrame please do because I am confused... Thank you! I am new to coding so I do apologize for asking such a simple easy question.

Roshana Pitigala :

You must add your JLabel to your JFrame.

frame.add(label1);

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=79906&siteId=1