0% found this document useful (0 votes)
23 views16 pages

AJP Prac (1 To 19)

codes of Advance java

Uploaded by

1603swatisingh
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)
23 views16 pages

AJP Prac (1 To 19)

codes of Advance java

Uploaded by

1603swatisingh
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/ 16

Q.

1 Design an application to create a form using TextField, textArea, Button and label
Code:
import java.awt.*;
public class SimpleForm {
public static void main(String[] args) {
Frame frame = new Frame("Form Example");
Label label = new Label("Enter Name:");
TextField textField = new TextField(30);
Label l2 = new Label("Enter Address:");
TextArea textArea = new TextArea(,5,20);
Button button = new Button("Submit");
frame.setLayout(new FlowLayout());
frame.add(label);
frame.add(textField);
frame.add(l2);
frame.add(textArea);
frame.add(button);
frame.setSize(300, 200);
frame.setVisible(true);
}
}

2. Develop a program to select multiple languages known to the user (Marathi Hindi, English,
Sanskrit) and gender of the user.
Code:
import java.awt.*;
public class LanguageGenderSelection {
public static void main(String[] args) {
Frame frame = new Frame("Language and Gender");
Checkbox marathi = new Checkbox("Marathi");
Checkbox hindi = new Checkbox("Hindi");
Checkbox english = new Checkbox("English");
Checkbox sanskrit = new Checkbox("Sanskrit");
CheckboxGroup genderGroup = new CheckboxGroup();
Checkbox male = new Checkbox("Male", genderGroup, false);
Checkbox female = new Checkbox("Female", genderGroup, false);
frame.setLayout(new FlowLayout());
frame.add(marathi);
frame.add(hindi);
frame.add(english);
frame.add(sanskrit);
frame.add(male);
frame.add(female);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Q3 Develop an applet/application to select multiple names of news Papers using List.
import java.awt.*;
public class NewspaperSelection {
public static void main(String[] args) {
Frame frame = new Frame("Newspaper Selection");
List newspapers = new List(4, true);
newspapers.add("Times of India");
newspapers.add("Hindustan Times");
newspapers.add("Indian Express");
newspapers.add("The Hindu");
frame.add(newspapers);
frame.setSize(200, 200);
frame.setVisible(true);
}
}

Q4 Develop an applet using List components to add names of 10 different cities.


import java.applet.*;
import java.awt.*;
//<applet code="CityListApplet" width=300 height=200> </applet>
public class CityListApplet extends Applet {
public void init() {
Label label = new Label("Select a City:");
add(label);
List cityList = new List(5, false);
cityList.add("Mumbai");
cityList.add("Delhi");
cityList.add("Bengaluru");
cityList.add("Chennai");
cityList.add("Kolkata");
cityList.add("Hyderabad");
cityList.add("Ahmedabad");
cityList.add("Pune");
cityList.add("Jaipur");
cityList.add("Lucknow");
add(cityList);
}
}
//<applet code="CityListApplet.class" width="300" height="200"></applet>
Q5 Design a program to demonstrate the use of Border layout (Constants used to specify areas:
CENTER, EAST, NORTH, SOUTH, WEST)
import java.awt.*;
public class BorderLayoutExample {
public static void main(String[] args) {
Frame frame = new Frame("BorderLayout Example");
frame.setLayout(new BorderLayout());
frame.add(new Button("North"), BorderLayout.NORTH);
frame.add(new Button("South"), BorderLayout.SOUTH);
frame.add(new Button("East"), BorderLayout.EAST);
frame.add(new Button("West"), BorderLayout.WEST);
frame.add(new Button("Center"), BorderLayout.CENTER);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
Q6. Write a program to create a grid using GridLayout with rows and columns. Add buttons or
labels to the grid.
import java.awt.*;
public class GridLayoutExample {
public static void main(String[] args) {
Frame frame = new Frame("Grid Layout Example");
frame.setLayout(new GridLayout(3, 3));
for (int i = 1; i <= 9; i++) {
frame.add(new Button("Button " + i));
}
frame.setSize(300, 300);
frame.setVisible(true);
}}
Q7 Write a program which creates Menu of different colors and disables menu items for Black
color.
import java.awt.*;
public class menu extends Frame
{
public static void main(String[] args)
{
menu ob = new menu();
ob.setVisible(true);
MenuBar mb = new MenuBar();
ob.setMenuBar(mb);
Menu m1 = new Menu("Red");
Menu m2 = new Menu("Blue");
Menu m3 = new Menu("Black");
mb.add(m1);
mb.add(m2);
mb.add(m3);
m3.setEnabled(false);
}
}
Q8 Write a program to perform addition of two no using event handling
import java.awt.event.*;
public class AddTwoNumbers {
public static void main(String[] args) {
Frame frame = new Frame("Addition");
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
Button addButton = new Button("Add");
Label result = new Label();
addButton.addActionListener(e -> {
int a = Integer.parseInt(num1.getText());
int b = Integer.parseInt(num2.getText());
result.setText("Result: " + (a + b));
});

frame.setLayout(new FlowLayout());
frame.add(num1);
frame.add(num2);
frame.add(addButton);
frame.add(result);

frame.setSize(300, 200);
frame.setVisible(true);
}
}
Q9 Develop a program using AWT to create a menubar in an applet window.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//<applet code="MenuDemo1" width="500" height="500"></applet>
public class MenuDemo1 extends Applet {
MenuBar mb;
MenuItem m1, m2, m3, m4;
Menu mn;
public void init() {
mb = new MenuBar();
mn = new Menu("File");
m1 = new MenuItem("New...");
m2 = new MenuItem("Open...");
m3 = new MenuItem("Save As...");
MenuShortcut ms = new MenuShortcut(KeyEvent.VK_X);
m4 = new MenuItem("Exit", ms);
mn.add(m1);
mn.add(m2);
mn.add(m3);
mn.addSeparator();
mn.add(m4);
mb.add(mn);
Frame frame = new Frame("Menu Demo");
frame.setMenuBar(mb);
frame.setSize(500, 500);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
frame.dispose();
}
});
}
}
Q10 Write a program using swing to display a ComboBox in an applet
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
// <applet code ="prac61" width =500 height= 500> </applet>
public class prac61 extends Applet implements ItemListener
{
Choice c;
Label d;
public void init()
{
c = new Choice();
c.add("Mumbai");
c.add("Solapur");
c.add("Pune");
c.add("Banglore");
d = new Label("You are in Mumbai");
add(c);
add(d);
c.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{
String selectedCity = c.getSelectedItem();
d.setText("You are in " + selectedCity);
}
}
Q11 Create a simple stopwatch application using JLabel to display time in seconds, and JButton
for start, stop, and reset. Use a Timer to update the time every second and allow the user to
start, stop, and reset the stopwatch with the corresponding buttons.
import javax.swing.*;
import java.awt.event.*;
public class Stopwatch {
static int seconds = 0;
static Timer timer;
public static void main(String[] args) {
JFrame frame = new JFrame("Stopwatch");
JLabel label = new JLabel("Time: 0 seconds");
JButton start = new JButton("Start");
JButton stop = new JButton("Stop");
JButton reset = new JButton("Reset");
start.addActionListener(e -> {
timer = new Timer(1000, ev -> {
seconds++;
label.setText("Time: " + seconds + " seconds");
});
timer.start();
});
stop.addActionListener(e -> timer.stop());
reset.addActionListener(e -> {
timer.stop();
seconds = 0;
label.setText("Time: 0 seconds");
});
frame.setLayout(new java.awt.FlowLayout());
frame.add(label);
frame.add(start);
frame.add(stop);
frame.add(reset);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Q12 Write a Jtree program to show the root directory and its subFolders of your System.
import javax.swing.*;
import java.io.File;
public class JTreeExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTree Example");
File root = new File("/");
JTree tree = new JTree(root.listFiles());
frame.add(new JScrollPane(tree));
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Q13 Write a program to create a table of the name of a student, percentage and grade of 10
students using JTable.
import javax.swing.*;
public class JTableExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Student Table");
String[][] data = {
{"1", "John", "85", "A"},
{"2", "Alice", "78", "B"},
{"3", "Bob", "92", "A"},
{"4", "Cathy", "65", "C"}
};
String[] columns = {"Roll No", "Name", "Percentage", "Grade"};
JTable table = new JTable(data, columns);
frame.add(new JScrollPane(table));
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Q14 Develop a Program to display the key pressed, key typed and key released event on Applet
Window.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
// <applet code="KeyEventApplet" width="500" height="500"></applet>
public class KeyEventApplet extends Applet implements KeyListener {
Label keyPressedLabel = new Label("Key Pressed: ");
Label keyTypedLabel = new Label("Key Typed: ");
Label keyReleasedLabel = new Label("Key Released: ");
public void init() {
setLayout(new GridLayout(3, 1));
addKeyListener(this);
setFocusable(true);
add(keyPressedLabel);
add(keyTypedLabel);
add(keyReleasedLabel);
}
public void keyPressed(KeyEvent e) {
keyPressedLabel.setText("Key Pressed: " + KeyEvent.getKeyText(e.getKeyCode()));
}
public void keyTyped(KeyEvent e) {
keyTypedLabel.setText("Key Typed: " + e.getKeyChar());
}
public void keyReleased(KeyEvent e) {
keyReleasedLabel.setText("Key Released: " + KeyEvent.getKeyText(e.getKeyCode()));
}
}
Q15 Develop a program to accept two numbers and display the product of two numbers when
the user presses the "Multiply" button.
import java.awt.*;
import java.awt.event.*;
public class MultiplyNumbers {
public static void main(String[] args) {
Frame frame = new Frame("Multiply Numbers");
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
Button multiplyButton = new Button("Multiply");
Label result = new Label();
multiplyButton.addActionListener(e -> {
int a = Integer.parseInt(num1.getText());
int b = Integer.parseInt(num2.getText());
result.setText("Result: " + (a * b));
});
frame.setLayout(new FlowLayout());
frame.add(num1);
frame.add(num2);
frame.add(multiplyButton);
frame.add(result);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Q16 Write a program using JPasswordField and JTextField to demonstrate the use of user
authentication.
import javax.swing.*;
import java.awt.event.*;
public class UserAuthentication {
public static void main(String[] args) {
JFrame frame = new JFrame("Authentication");
JTextField username = new JTextField(10);
JPasswordField password = new JPasswordField(10);
JButton login = new JButton("Login");
JLabel status = new JLabel();
login.addActionListener(e -> {
String user = username.getText();
String pass = new String(password.getPassword());
if (user.equals("admin") && pass.equals("password")) {
status.setText("Login Successful!");
} else {
status.setText("Invalid Credentials.");
}
});
frame.setLayout(new java.awt.FlowLayout());
frame.add(new JLabel("Username:"));
frame.add(username);
frame.add(new JLabel("Password:"));
frame.add(password);
frame.add(login);
frame.add(status);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Q 17 Write a program to count the number of clicks performed by the user in a frame.
import java.awt.*;
import java.awt.event.*;
public class ClickCounter {
static int count = 0;
public static void main(String[] args) {
Frame frame = new Frame("Click Counter");
Button button = new Button("Click Me");
Label label = new Label("Clicks: 0");
button.addActionListener(e -> {
count++;
label.setText("Clicks: " + count);
});
frame.setLayout(new FlowLayout());
frame.add(button);
frame.add(label);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Q 18 Develop a program using InetAddress class to retrieve the IP address of a computer when
hostname is entered by the user.
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class IPAddressFinder {
public static void main(String[] args) {
Frame frame = new Frame("IP Finder");
TextField hostnameField = new TextField(20);
Button findButton = new Button("Find IP");
Label result = new Label();
findButton.addActionListener(e -> {
try {
String hostname = hostnameField.getText();
InetAddress address = InetAddress.getByName(hostname);
result.setText("IP: " + address.getHostAddress());
} catch (Exception ex) {
result.setText("Error: " + ex.getMessage());
}
});
frame.setLayout(new FlowLayout());
frame.add(new Label("Hostname:"));
frame.add(hostnameField);
frame.add(findButton);
frame.add(result);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Q19 Write a program using URL class to retrieve the host, protocol, port and the file of URL
http://www.msble.org.in
import java.awt.*;
import java.net.*;
public class URLDetails {
public static void main(String[] args) {
Frame frame = new Frame("URL Details");
Label label = new Label();
frame.add(label);
try {
URL url = new URL("http://www.msble.org.in");
label.setText("Host: " + url.getHost() + ", Protocol: " + url.getProtocol() + ", Port: " + url.getPort() +
", File: " + url.getFile());
}
catch (Exception e) {
label.setText("Error: " + e.getMessage());
e.printStackTrace();
}
frame.setSize(400, 100);
frame.setVisible(true);
}
}

You might also like