Muneeb Ejaz Muneeb Ejaz - 2 years ago 72
Java Question

GUI cannot add() JLabel to the JFrame

I am just new to Java. When I run the program below I get nothing - no

is added to the window

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;

public class MainProgram{

public static void main(String[] args) {
JFrame frame = new JFrame("This is the title of the window");//adding the JFrame or window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//so it really and literally close when we
//hit the close button on the window
frame.setVisible(true);//setting the visibility
//adding the label
JLabel label_1 = new JLabel("this is a JLabel");
//adding the label to the window
label_1.setToolTipText("This is the tool tip");

Any suggestions?

Answer Source

just add this line


try to set a dimension to the jlabel and the frame itself; when using add(Component) don't to forget frame before it, use it as following : frame.add(yourLabel);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download