You forgot to add the p panel to the sample. Either use add(p) at the end, or just remove the p panel because your class class extends JPanel.
Option 1:
JPanel p = new JPanel(); JLabel lab1 = new JLabel("User Name", JLabel.LEFT); p.setLayout(new FlowLayout()); p.add(lab1 = new JLabel("add JLabel")); add(p);
option 2:
JLabel lab1 = new JLabel("User Name", JLabel.LEFT); setLayout(new FlowLayout()); add(lab1 = new JLabel("add JLabel"));
Also why are you overriding JLabel initialization? In your code, JLable will always contain the value "add JLabel". If you want to see the "Username", use add(lab1); instead of add(lab1 = new JLabel("add JLabel")); .
Perhaps you just need to:
JLabel lab1 = new JLabel("User Name", JLabel.LEFT); setLayout(new FlowLayout()); add(lab1);
Also, the constructor cannot have a return type, so remove void from your constructor.
source share