0% found this document useful (0 votes)
9 views21 pages

Setting Icon for JFrame in Java Swing

The document contains a series of Java programs using Swing and JSP for various practical applications. These include setting icons for frames, displaying dialog boxes, changing button labels, and creating servlets and JSP pages for user interaction. Each practical example demonstrates specific functionalities in Java GUI programming and web development.

Uploaded by

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

Setting Icon for JFrame in Java Swing

The document contains a series of Java programs using Swing and JSP for various practical applications. These include setting icons for frames, displaying dialog boxes, changing button labels, and creating servlets and JSP pages for user interaction. Each practical example demonstrates specific functionalities in Java GUI programming and web development.

Uploaded by

ffghhcvnn3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PRACTICAL 1

AIM :- WAP Setting an Icon for a Frame in Java using Swing?

import [Link].*;

import [Link].*;

public class FrameIconExample {

public static void main(String[] args) {

JFrame frame = new JFrame("Frame Icon Example");

[Link](JFrame.EXIT_ON_CLOSE);

[Link](400, 300);

// Set the icon

ImageIcon icon = new ImageIcon("[Link]"); // Replace with your image file path

[Link]([Link]());

[Link](true);

}
PRACTICAL 2
AIM :- WAP Show Dialog Box in Java using Swing Dialogs?

import [Link].*;

public class DialogBoxExample {

public static void main(String[] args) {

// Create a frame (required for dialogs to be attached)

JFrame frame = new JFrame("Dialog Box Example");

[Link](JFrame.EXIT_ON_CLOSE);

[Link](300, 200);

[Link](true);

// Show a simple dialog box

[Link](frame, "Hello! This is a dialog box.");

// Optional: Close the frame after showing dialog

[Link]();

}
}

PRACTICAL 3
import [Link].*;

public class MessageAndConfirmDialogExample {

public static void main(String[] args) {

// Create a frame (required for dialogs)

JFrame frame = new JFrame("Message and Confirm Dialog Example");

[Link](JFrame.EXIT_ON_CLOSE);

[Link](300, 200);

[Link](true);

// Show a Message Dialog


[Link](frame, "This is a Message Dialog!", "Message",
JOptionPane.INFORMATION_MESSAGE);

// Show a Confirm Dialog

int response = [Link](frame, "Do you want to continue?", "Confirm",


JOptionPane.YES_NO_OPTION);

// Check the user's response

if (response == JOptionPane.YES_OPTION) {

[Link](frame, "You chose YES!");

} else if (response == JOptionPane.NO_OPTION) {

[Link](frame, "You chose NO!");

} else {

[Link](frame, "You closed the dialog without choosing!");

// Close the frame after showing dialogs

[Link]();

}
PRACTICAL 4

2. WAP Changing the Label of a JButton Component in Java using Swing?


import [Link].*;
import [Link].*;

public class ChangeButtonLabelExample {


public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Change Button Label Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);
[Link](null); // using absolute layout

// Create a button
JButton button = new JButton("Click Me");
[Link](90, 50, 120, 40);

// Add ActionListener to change label when clicked


[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Clicked!");
}
});

// Add button to the frame


[Link](button);

// Set frame visible


[Link](true);
}
}

OUTPUT
PRACTICAL 5
3. WAP Setting Multi-Line label on the Button using Swing?

import [Link].*;

public class MultiLineButtonExample {

public static void main(String[] args) {

// Create a frame

JFrame frame = new JFrame("Multi-Line Button Example");

[Link](JFrame.EXIT_ON_CLOSE);

[Link](300, 200);

[Link](null); // Absolute layout

// Create a button with multi-line text using HTML

JButton button = new JButton("<html>This is<br>Multi-Line<br>Button</html>");

[Link](70, 50, 160, 80);

// Add the button to frame

[Link](button);
// Set frame visible

[Link](true);

Practical 5
4. WAP Setting Multi-Line label on the Button using Swing?
import [Link].*;
import [Link];
import [Link];

public class MultiLineButtonStyled {


public static void main(String[] args) {
JFrame frame = new JFrame("Styled Multi-Line Button");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](350, 250);
[Link](new FlowLayout([Link], 20, 40)); // Center aligned layout

// Create a JButton with multi-line HTML text


JButton button = new JButton("<html><div style='text-align: center;'>First Line<br>Second
Line</div></html>");

// Set bigger font for the button text


[Link](new Font("Arial", [Link], 18));

[Link](button);
[Link](true);
}
}

Practical 6
5. WAP Setting icon on the button in Java using Swing?
import [Link].*;
import [Link].*;

public class ButtonIconExample {


public static void main(String[] args) {
// Create a new frame
JFrame frame = new JFrame("Button Icon Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);
[Link](new FlowLayout());

// Load the image as an icon


ImageIcon icon = new ImageIcon("[Link]"); // Make sure the file is in the project
directory

// Create a button with text and icon


JButton button = new JButton("Settings", icon);
// Optionally, you can set text position relative to the icon
[Link]([Link]);
[Link]([Link]);

// Add the button to the frame


[Link](button);

// Set frame visibility


[Link](true);
}
}

Practical 7
. WAP Changing the Label of a JButton Component in Java using Swing?
import [Link].*;
import [Link].*;
import [Link].*;

public class ChangeButtonLabel {


public static void main(String[] args) {
JFrame frame = new JFrame("Button Label Change");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);
[Link](new FlowLayout());

JButton button = new JButton("Click Me");


[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Clicked!");
}
});

[Link](button);
[Link](true);
}
}
Practical 8
WAP Making a Frame Non Resizable in Java using Swing?
import [Link].*;

public class NonResizableFrame {


public static void main(String[] args) {
JFrame frame = new JFrame("Non-Resizable Frame");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](400, 300);

// Make the frame non-resizable


[Link](false);

[Link](true);
}
}

Practical 9
WAP Remove Minimize and Maximize Button of Frame in Java using Swing?
import [Link].*;
import [Link].*;

public class RemoveMinMaxButtons {


public static void main(String[] args) {
JFrame frame = new JFrame("Remove Minimize and Maximize Buttons");

// Set default close operation and size


[Link](JFrame.EXIT_ON_CLOSE);
[Link](400, 300);

// Disable resizing of the frame


[Link](false);

// Remove maximize and minimize buttons only, keep the title bar
[Link]().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);

[Link](true);
}
}

Practical 10
WAP Removing the Title Bar of a Frame using Swing?

import [Link];
import [Link];
import [Link];

public class RemoveTitleBarExample {


public static void main(String[] args) {
// Create a new frame
JFrame frame = new JFrame();

// Remove the title bar and borders


[Link](true);

// Set layout manager


[Link](new FlowLayout());

// Add a label
[Link](new JLabel("Frame without Title Bar!"));

// Set size of the frame


[Link](300, 200);

// Close the application when frame is closed


[Link](JFrame.EXIT_ON_CLOSE);

// Center the frame on screen


[Link](null);

// Make frame visible


[Link](true);
}
}

Pratical 11
WAP Setting Bounds for a maximized frame using Swing?
import [Link];
import [Link];
import [Link];

public class MaximizeFrameWithBounds {


public static void main(String[] args) {
// Create a new frame
JFrame frame = new JFrame("Maximized Frame with Custom Bounds");
// Set default close operation
[Link](JFrame.EXIT_ON_CLOSE);

// Maximize the frame


[Link](JFrame.MAXIMIZED_BOTH);

// Get the screen size


Dimension screenSize = [Link]().getScreenSize();

// Set custom bounds (for example, center 80% width and height)
int width = (int) ([Link] * 0.8);
int height = (int) ([Link] * 0.8);
int x = ([Link] - width) / 2;
int y = ([Link] - height) / 2;

// Set the bounds


[Link](x, y, width, height);

// Make the frame visible


[Link](true);
}
}

Practical 12
Write a java Program to create a simple servlet and run it using tomcat server?

// [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

// This servlet will handle requests to /hello


@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

// Handles GET requests (when you open it in browser)


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<!DOCTYPE html>");
[Link]("<html><head><title>Hello Servlet</title></head>");
[Link]("<body>");
[Link]("<h1>Hello, World! This is a simple servlet running on Tomcat!</h1>");
[Link]("</body></html>");
}
}

Practical 13
Write a java Program to create a servlet to read information from client Registration page?
1. [Link]
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<h2>Register Here</h2>
<form action="register" method="post">
<label>First Name:</label>
<input type="text" name="firstName" required><br><br>

<label>Last Name:</label>
<input type="text" name="lastName" required><br><br>

<label>Email:</label>
<input type="email" name="email" required><br><br>

<label>Password:</label>
<input type="password" name="password" required><br><br>

<input type="submit" value="Register">


</form>
</body>
</html>
2. [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@WebServlet("/register")
public class RegistrationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Reading form data from client


String firstName = [Link]("firstName");
String lastName = [Link]("lastName");
String email = [Link]("email");
String password = [Link]("password"); // Should be encrypted/stored
safely!

// Setting response type


[Link]("text/html");

// Sending back response


PrintWriter out = [Link]();
[Link]("<html><head><title>Registration Successful</title></head><body>");
[Link]("<h2>Thank You for Registering!</h2>");
[Link]("<p><strong>First Name:</strong> " + firstName + "</p>");
[Link]("<p><strong>Last Name:</strong> " + lastName + "</p>");
[Link]("<p><strong>Email:</strong> " + email + "</p>");
[Link]("</body></html>");
}
}

Practical 14
Write a java Program to create a JSP page to display a simple message along with current
Date?
<%@ page import="[Link]" %>
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome to My Website!</h1>

<p>Current Date and Time is: <strong><%= new Date() %></strong></p>


</body>
</html>

Practical 15
Write a java Program to create a JSP page to display the random number?
<%@ page import="[Link]" %>
<!DOCTYPE html>
<html>
<head>
<title>Random Number Generator</title>
</head>
<body>
<h1>Random Number Generator</h1>

<p>Your Random Number is: <strong><%= new Random().nextInt(1000) %></strong></p>


</body>
</html>
Practical 16
Write a java Program to create a User request page in JSP?
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>User Request Page</title>
</head>
<body>
<h2>Welcome! Please Enter Your Name</h2>

<form method="post" action="[Link]">


<input type="text" name="username" placeholder="Enter your name" required>
<input type="submit" value="Submit">
</form>

<%
// Read the submitted name
String name = [Link]("username");

if (name != null && ![Link]().isEmpty()) {


%>
<h3>Hello, <%= name %>! Welcome to our website.</h3>
<%
}
%>
</body>
</html>

You might also like