Java Program to Display Some Text in the Frame using Labels

This is a Java program to display some text in the frame with the help of a label.

Problem Description

We have to write a program in Java such that it displays some text in a frame using JLabel.

Expected Input and Output

To display text using a label, we have the following set of input and output.

To display a text:
At the execution of the program, it is expected that a frame appears with some text written with the help of a label.
Text is “This text is written using a label”

Problem Solution

1. Create a frame and a label.
2. Add the label to the frame.
3. Write some text to the label.
4. Display the frame.

Program/Source Code

Here is source code of the Java Program to display text in a frame using a label. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

advertisement
advertisement
  1. /*Java Program to display text using label*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. class Text_Label
  5. {
  6. 	//Driver function
  7. 	public static void main(String args[])
  8. 	{
  9. 		//Create a frame
  10. 		JFrame frame=new JFrame("Text using Label");
  11. 		frame.setSize(500,500);
  12. 		frame.setBackground(Color.white);
  13. 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. 		frame.setLayout(null);
  15. 		//Create a Label
  16. 		JLabel label=new JLabel();
  17. 		label.setBounds(0,100,500,50);
  18. 		frame.add(label);
  19. 		//Write text to the label
  20. 		String str="This text is written using a label";
  21. 		label.setText(str);
  22. 		//Display the frame
  23. 		frame.setVisible(true);
  24. 	}
  25. }
Program Explanation

1. Create a frame with background color & specific size. frame.setBackground(Color.white); is used to create the frame background color as white. frame.setSize(500,500); is used to set the width and height of the frame.
2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); is a default close operation for the frame.
3. Create a label with a setBounds() values and add it to the frame by using frame.add();.
JLabel label=new JLabel(); –> Used to create the label.
label.setBounds(0,100,500,50); –> Sets the position of label.
frame.add(label); –> It will add the label to the frame.

4. String str=”This text is written using a label”; –> It is used to write text to the label. Text is “This text is written using a label”
5. label.setText(str); –> It will add the text to the label.
6. frame.setVisible(true); is used to display the frame. The frame will display with the text “This text is written using a label” which is added to the label.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases

Here’s the run time test case for displaying text in a frame using a label.

Test case – Here’s the runtime output to display a text. Once the program is executed, it displays the frame with text “This text is written using a label”.
java-program-text-using-label

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.