Ajp Harsh Ajmire 8 To 11.
Ajp Harsh Ajmire 8 To 11.
import java.awt.event.*;
Label l;
TextArea area;
KeyListenerExample()
l = new Label();
add(area);
\ setVisible (true);
new KeyListenerExample();
Output:-
Program:
import javax.swing.*;
import java.awt.event.*;
public MouseEventDemo() {
setSize(400, 300);
setDefaultCloseOpera on(JFrame.EXIT_ON_CLOSE);
textArea.setEditable(false);
add(new JScrollPane(textArea));
textArea.addMouseListener(this);
textArea.addMouseMo onListener(this):
setVisible(true);
}
@Override
@Override
@Override
@Override
@Override
@Override
@Override
Output:
AIM:-write a program to demonstrate the use of JTex ield and JPasswordField using Listener
Interface
Program:-
import javax.swing.*;
import java.awt.event.Ac onEvent;
import java.awt.event.Ac onListener;
public class SimpleTextFieldPasswordFieldDemo extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JTextArea outputArea;
private JBu on submitBu on;
public SimpleTextFieldPasswordFieldDemo() {
setTitle("TextField and PasswordField Demo");
setSize(400, 200);
setDefaultCloseOpera on(JFrame.EXIT_ON_CLOSE);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("Username:"));
usernameField = new JTextField(20);
usernamePanel.add(usernameField);
JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("Password:"));
passwordField = new JPasswordField(20);
passwordPanel.add(passwordField);
submitBu on = new JBu on("Submit");
submitBu on.addAc onListener(new Ac onListener() {
@Override
public void ac onPerformed(Ac onEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
outputArea.setText("Username: " + username + "\nPassword: " + password);
}
});
outputArea = new JTextArea(5, 30);
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
Aim:- Write a Pragram to demonstrate the use of InetAddress class and its
factory methods
Program:-
import java.net.InetAddress;
import java.net.UnknownHostExcep on;
public class InetAddressDemo {
public sta c void main(String[] args) {
try {
InetAddress localAddress = InetAddress.getLocalHost();
System.out.println("Local IP Address: " + localAddress.getHostAddress());
System.out.println("Local Hostname: " + localAddress.getHostName());
String hostname = "www.google.com";
InetAddress hostAddress = InetAddress.getByName(hostname);
System.out.println("IP Address of " + hostname + ": " + hostAddress.getHostAddress());
System.out.println("Hostname: " + hostAddress.getHostName());
String domain = "example.com";
InetAddress[] domainAddresses = InetAddress.getAllByName(domain);
System.out.println("IP Addresses of " + domain + ":");
for (InetAddress address : domainAddresses) {
System.out.println(" " + address.getHostAddress());
}
} catch (UnknownHostExcep on e) {
System.err.println("Host could not be resolved: " + e.getMessage());
}
}
}
Output: