0% found this document useful (0 votes)
6 views2 pages

Mad 1

The document is a Java program that creates a graphical user interface (GUI) application for changing font styles and text colors. It includes components such as labels, text fields, and buttons, allowing users to input text and modify its appearance. The application uses Swing for the GUI and features event listeners for interactive functionality.

Uploaded by

keekukeeks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Mad 1

The document is a Java program that creates a graphical user interface (GUI) application for changing font styles and text colors. It includes components such as labels, text fields, and buttons, allowing users to input text and modify its appearance. The application uses Swing for the GUI and features event listeners for interactive functionality.

Uploaded by

keekukeeks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import javax.swing.

*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUIFontColorApp {

public static void main(String[] args) {


// Create the main frame
JFrame frame = new JFrame("GUI Font and Color Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);

// Panel to hold components


JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 1));
panel.setBackground(Color.LIGHT_GRAY);

// Label
JLabel label = new JLabel("Welcome to the Font & Color App!");
label.setFont(new Font("Arial", Font.BOLD, 16));
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);

// TextField
JTextField textField = new JTextField("Type your text here");
textField.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(textField);

// Button to change font style


JButton fontButton = new JButton("Change Font");
fontButton.setBackground(Color.CYAN);
fontButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
panel.add(fontButton);

// Button to change text color


JButton colorButton = new JButton("Change Text Color");
colorButton.setBackground(Color.PINK);
colorButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
panel.add(colorButton);

// Label to show user-entered text


JLabel outputLabel = new JLabel("Your text will appear here",
JLabel.CENTER);
outputLabel.setFont(new Font("Verdana", Font.PLAIN, 14));
panel.add(outputLabel);

// Button action for changing font style


fontButton.addActionListener(e -> {
Font currentFont = outputLabel.getFont();
outputLabel.setFont(new Font("Courier New", Font.ITALIC, 18));
outputLabel.setText("Font Changed!");
});

// Button action for changing text color


colorButton.addActionListener(e -> {
Color selectedColor = JColorChooser.showDialog(null, "Choose a Text
Color", Color.BLACK);
if (selectedColor != null) {
outputLabel.setForeground(selectedColor);
}
});

// Handle text input from the text field


textField.addActionListener(e -> outputLabel.setText(textField.getText()));

frame.add(panel);
frame.setVisible(true);
}
}

You might also like