Java Program to Change Frame Background Color as Cyan

This is a Java Program to Display String with Background Color as Cyan in a Frame

Problem Description

We have to write a program in Java such that it creates a frame with background color as cyan and a text with color red is written in the frame.

Expected Input and Output

For displaying string with background color as cyan, we have the following set of input and output.

To Display the Frame :

On the execution of the program,
it is expected that the frame has background color as cyan, and also
displays the text "The Background of Frame is Cyan" in frame with red color.
Problem Solution

1. Create the frame.
2. Set the background color of frame as cyan using setBackground(Color.CYAN).
3. Create a text with required font type and size.
4. Set the color of text to red using setForeground(Color.red).
5. Display the frame.

advertisement
advertisement
Program/Source Code

Here is source code of the Java Program to perform arithmetic operations. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /*Java Program to Display Text with Background Color as Cyan*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. class Background_Cyan
  5. {
  6.     //Driver function
  7.     public static void main(String args[])
  8.     {
  9.         //Create a frame
  10. 	JFrame frame = new JFrame("Cyan Text");
  11. 	frame.setSize(400,400);
  12. 	frame.setLayout(new FlowLayout());
  13. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. 	frame.getContentPane().setBackground(Color.CYAN);
  15. 	//Write some text to the frame
  16. 	String str="The Background of Frame is Cyan";
  17. 	JLabel text = new JLabel(str);
  18. 	text.setFont(new Font("Serif",Font.ITALIC,20));
  19. 	frame.add(text);
  20. 	//Set Background Color of Text as red
  21. 	text.setForeground(Color.red);
  22. 	//Display the frame
  23. 	frame.setVisible(true);
  24.     }
  25. }
Program Explanation

1. To set the background color of frame, use the setBackground() function.
2. To set the foreground color of text, use the setForeground() function.

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

Here’s the run time test cases to display text with background color as cyan.

Test case 1 – To view the frame.
java-program-background-cyan

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.