0% found this document useful (0 votes)
17 views3 pages

Simple Blog Creation

Uploaded by

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

Simple Blog Creation

Uploaded by

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

EX.

NO:11 MINI PROJECT-SIMPLE BLOG CREATION


DATE:

AIM:
The aim of the program is to provide a basic GUI application where users can:
1. Enter a Message in a text field.
2. Click a button to display the Entered message on the GUI.
3. Handle cases where no input is entered by displaying a prompt.

ALGORITHM:
1. Initialize the Application:
o Create a JFrame (window) with the title "Message App".
o Set the size of the JFrame to 400x200 pixels.
o Set the default close operation to EXIT_ON_CLOSE to close the application
when the window is closed.
o Set the layout of the JFrame to FlowLayout for arranging the components
sequentially.
2. Create UI Components:
o Text Field: Create a JTextField with a column size of 20 (for user input).
o Buttons: Create a JButton named displayButton with the text "Display
Message". Create another JButton named clearButton with the text
"Clear".
o Label: Create a JLabel named label with HTML content (to support
multiline text) initialized as an empty HTML (<html></html>). Set the
preferred size of the label to 350x100 pixels, ensuring it has enough space
to display multiple lines of text. Set the vertical alignment of the label to
top (for proper alignment of text).
3. Define Action Listeners:
o For the Display Message Button (displayButton):
▪ Action: When clicked, the program:
1. Retrieve the text from the JTextField.
2. Check if the text is empty (after trimming whitespace):
▪ If empty, add "Please enter a message." to the label.
▪ If not empty, display the message prefixed by
"Message: ".
3. Optionally, clear the text in the JTextField after the message
is displayed.
o For the Clear Button (clearButton):
▪ Action: When clicked, the program:
1. Clears the text in the JTextField.
2. Clears all content from the JLabel by setting it to an empty
HTML (<html></html>).
4. Add Components to the Frame: Add the JTextField, JButton for "Display
Message", JButton for "Clear", and JLabel to the frame.
5. Display the Frame: Set the frame to visible so that the application UI is rendered
and user interaction is enabled.
6. Program Termination: The application will continue running until the user closes
the window, triggering the EXIT_ON_CLOSE action which ends the program.
PROGRAM:
import javax.swing.*
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MessageApp {
public static void main(String[] args) {
JFrame frame = new JFrame("Message App");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JTextField textField = new JTextField(20);
JButton displayButton = new JButton("Display Message");
JButton clearButton = new JButton("Clear");
JLabel label = new JLabel("<html></html>");
label.setPreferredSize(new Dimension(350, 100));
label.setVerticalAlignment(SwingConstants.TOP);
displayButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String message = textField.getText().trim();
if (message.isEmpty()) {
label.setText(label.getText().replace("</html>", "") + "Please enter a
message.<br></html>");
} else {
label.setText(label.getText().replace("</html>", "") + "Message: " + message +
"<br></html>");
}textField.setText(""); // Optionally clear the text field after submitting
}
});
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.setText("");
label.setText("<html></html>"); // Clear all messages
} });
frame.add(textField);
frame.add(displayButton);
frame.add(clearButton);
frame.add(label);
frame.setVisible(true);
}
}
OUTPUT:

APPLICATIONS:
1. Basic User Input Handling.
2. Simple Message Display Tool.
3. Educational Tool for Learning Swing.
4. Prototype for Feedback Systems.
5. Simple Text Editor Interface.

RESULT:
Thus, the java program for Simple Blog Creation is successfully developed and
executed successfully.

You might also like