AJP Practical 10 12
AJP Practical 10 12
Aim:
Overview of Components :
1. JTextField:
o A JTextField is a component that allows users to enter and edit a single
line of text.
o It is commonly used for inputs like usernames, email addresses, or any
other single-line data.
o You can retrieve the inputted text using the getText() method.
o
2. JPasswordField:
o A JPasswordField is similar to JTextField, but it masks the input
characters (typically displaying asterisks or dots).
o This component is designed for sensitive data, like passwords, to
prevent onlookers from seeing what is being typed.
o You can retrieve the entered password using the getPassword()
method, which returns a character array for security reasons.
o
Listener Interface
ActionListener: This interface is part of the Java AWT event package and is
used to handle action events (like button clicks).
When the user triggers an action (for example, clicking a button), the
actionPerformed method is invoked, allowing you to define the behavior in response
to that action.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JTextAdd()
{
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(200,200);
f.setLayout(new FlowLayout());
tf1.addActionListener(this);
f.add(jl);
f.add(tf);
f.add(jl1);
f.add(tf1);
f.add(res);
tf.setBounds(0,0,100,70);
tf1.setBounds(0,0,100,70);
}
public static void main(String[] args) {
JTextAdd jt = new JTextAdd();
}
Output :
import javax.swing.*;
import java.awt.*;
pf.setEchoChar('#');
f.add(pf);
}
Output :
Aim:
Write a Program to demonstrate the use of InetAddress class and its factory
methods.
Theory:
Overview of InetAddress :
The InetAddress class in Java is part of the java.net package and is used to
represent an Internet Protocol (IP) address. It provides methods to handle both IPv4
and IPv6 addresses, allowing for operations such as hostname resolution and IP
address manipulation.
Purpose of InetAddress
Key Features
Static Factory Methods: The InetAddress class provides several static factory
methods for creating instances of InetAddress.
Retrieving Local Host Information: You can get the IP address and hostname
of the local machine.
Network Utilities: Useful for network programming, such as server-client
communication.
Program :
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;
try
{
InetAddress ip = InetAddress.getByName(host);
}
}
Output :
Aim:
Write a Program to demonstrate the use of URL and URLConnection class and its
methods.
Theory:
.
URLConnection
URL Class
Creating a URL: You can create a URL object from a string
representation of a URL.
Parsing Components: The URL class provides methods to retrieve
different parts of the URL, such as:
o getProtocol(): Returns the protocol (e.g., "http").
o getHost(): Returns the host name or IP address.
o getPort(): Returns the port number.
o getPath(): Returns the file path.
o getQuery(): Returns the query string.
o
URLConnection Class
Program :
import java.net.URL;
import java.net.MalformedURLException;
public class URLRetrive
{
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("https://www.spotify.com/");
System.out.println("Authority: "+ url.getAuthority());
System.out.println("Default Port: "+ url.getDefaultPort());
System.out.println("File: "+ url.getFile());
System.out.println("Path: "+ url.getPath());
System.out.println("Protocol: "+ url.getProtocol());
System.out.println("Reference: "+ url.getRef());
}
}
Output :