This is a Java Program to Display String with Background Color as Cyan in a Frame
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.
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.
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.
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.
/*Java Program to Display Text with Background Color as Cyan*/
import javax.swing.*;
import java.awt.*;
class Background_Cyan
{
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame = new JFrame("Cyan Text");
frame.setSize(400,400);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.CYAN);
//Write some text to the frame
String str="The Background of Frame is Cyan";
JLabel text = new JLabel(str);
text.setFont(new Font("Serif",Font.ITALIC,20));
frame.add(text);
//Set Background Color of Text as red
text.setForeground(Color.red);
//Display the frame
frame.setVisible(true);
}
}
1. To set the background color of frame, use the setBackground() function.
2. To set the foreground color of text, use the setForeground() function.
Here’s the run time test cases to display text with background color as cyan.
Test case 1 – To view the frame.
Sanfoundry Global Education & Learning Series – Java Programs.
- Practice BCA MCQs
- Practice Programming MCQs
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Check Programming Books