CJ_Slip_7A

Slip No.7

A) Write a java program to display Label with text “Dr. D Y Patil College”,background color Red and font size 20 on the frame.

1. Open Notepad.

2. Type the following code:

    import javax.swing.*;
    import java.awt.*;

    public class LabelExample {
    public static void main(String[] args) {
    // Create a new JFrame (the window)
    JFrame frame = new JFrame("Label Example");

    // Set the default close operation so the application will exit when the window is closed
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create a new JLabel with the specified text
    JLabel label = new JLabel("Dr. D Y Patil College");

    // Set the background color of the label to red
    label.setOpaque(true); // Make sure the background color is visible
    label.setBackground(Color.RED);

    // Set the font size of the label
    label.setFont(new Font("Arial", Font.PLAIN, 20));

    // Add the label to the frame
    frame.add(label);

    // Set the size of the frame
    frame.setSize(400, 100);

    // Set the frame to be visible
    frame.setVisible(true);
    }
    }


3. Save the file with the name LabelExample.java. Make sure to select "All Files" in the "Save as type" dropdown and add the .java extension manually.

4. Open the Command Prompt.

5. Compile the Java program by typing:
javac LabelExample.java

6. Run the compiled Java program by typing:
java LabelExample

No comments:

Post a Comment