0% found this document useful (0 votes)
10 views9 pages

AJP Codesdocx

The document contains a series of practical programming exercises in Java, demonstrating the use of GUI components such as Radio Buttons, Checkboxes, TextFields, and Menus. Each practical includes code snippets for creating applications that perform specific tasks, such as selecting languages, displaying numbers, and handling mouse events. Additionally, there are examples of using special keys and retrieving IP addresses using the InetAddress class.

Uploaded by

adityakumkar28
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)
10 views9 pages

AJP Codesdocx

The document contains a series of practical programming exercises in Java, demonstrating the use of GUI components such as Radio Buttons, Checkboxes, TextFields, and Menus. Each practical includes code snippets for creating applications that perform specific tasks, such as selecting languages, displaying numbers, and handling mouse events. Additionally, there are examples of using special keys and retrieving IP addresses using the InetAddress class.

Uploaded by

adityakumkar28
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
You are on page 1/ 9

A

JP Practicals

Practical no1 – Design an applet/application to demonstrate the use of Radio


Button and Checkbox .
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RadioCheckboxDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Radio Button and Checkbox Demo");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel label = new JLabel("Choose your preferences:");
JRadioButton rb1 = new JRadioButton("Option 1");
JRadioButton rb2 = new JRadioButton("Option 2");
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
JCheckBox cb1 = new JCheckBox("Checkbox 1");
JCheckBox cb2 = new JCheckBox("Checkbox 2");
frame.add(label);
frame.add(rb1);
frame.add(rb2);
frame.add(cb1);
frame.add(cb2);
frame.setVisible(true);
}
}

Practical no2 – Develop a program to select multiple languages known to user.


(e.g Marathi, Hindi, English, Sanskrit).

import java.awt.*;

import javax.swing.*;

public class LanguageSelector {

public static void main(String[] args) {

JFrame frame = new JFrame("Language Selector");

frame.setSize(400, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new FlowLayout());

JLabel label = new JLabel("Select languages you know:");

JCheckBox cb1 = new JCheckBox("Marathi");

JCheckBox cb2 = new JCheckBox("Hindi");

JCheckBox cb3 = new JCheckBox("English");

JCheckBox cb4 = new JCheckBox("Sanskrit");

frame.add(label);

frame.add(cb1);

frame.add(cb2);

frame.add(cb3);
frame.add(cb4);

frame.setVisible(true);

Practical no3 – Develop a program to create a Student Data Form using


TextField, TextArea, Button and Label.

import java.awt.*;

import javax.swing.*;

public class StudentDataForm {

public static void main(String[] args) {

JFrame frame = new JFrame("Student Data Form");

frame.setSize(400, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new FlowLayout());

JLabel nameLabel = new JLabel("Name:");

JTextField nameField = new JTextField(20);

JLabel addressLabel = new JLabel("Address:");

JTextArea addressArea = new JTextArea(5, 20);

JButton submitButton = new JButton("Submit");

frame.add(nameLabel);

frame.add(nameField);
frame.add(addressLabel);

frame.add(new JScrollPane(addressArea));

frame.add(submitButton);

frame.setVisible(true);

Practical no4 – Develop applet/application to select multiple names of news


papers.

import java.awt.*;

import javax.swing.*;

public class NewspaperSelector {

public static void main(String[] args) {

JFrame frame = new JFrame("Newspaper Selector");

frame.setSize(400, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new FlowLayout());

JLabel label = new JLabel("Select newspapers:");

JCheckBox cb1 = new JCheckBox("Times of India");

JCheckBox cb2 = new JCheckBox("Indian Express");

JCheckBox cb3 = new JCheckBox("Hindustan Times");

JCheckBox cb4 = new JCheckBox("The Hindu");


frame.add(label);

frame.add(cb1);

frame.add(cb2);

frame.add(cb3);

frame.add(cb4);

frame.setVisible(true);

Practical no5 – Write a program to display the number on button from 0 to 9.


import java.awt.*;
import javax.swing.*;
public class NumberButtons {
public static void main(String[] args) {
JFrame frame = new JFrame("Number Buttons");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2, 5));
for (int i = 0; i <= 9; i++) {
frame.add(new JButton(String.valueOf(i)));
}
frame.setVisible(true);
}
}

Practical no6 – Write a program which creates menu of different colors and
disable menu item for Black color.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorMenu {
public static void main(String[] args) {
JFrame frame = new JFrame("Color Menu");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu colorMenu = new JMenu("Colors");
JMenuItem red = new JMenuItem("Red");
JMenuItem blue = new JMenuItem("Blue");
JMenuItem green = new JMenuItem("Green");
JMenuItem black = new JMenuItem("Black");
black.setEnabled(false);
colorMenu.add(red);
colorMenu.add(blue);
colorMenu.add(green);
colorMenu.add(black);
menuBar.add(colorMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}

Practical no7 – Develop a program which will implement special keys such as
function keys and arrow keys.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SpecialKeysDemo extends JFrame implements KeyListener {

JLabel label;

public SpecialKeysDemo() {

label = new JLabel("Press any key");

add(label);

addKeyListener(this);

setSize(400, 300);

setLayout(new FlowLayout());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public void keyPressed(KeyEvent e) {

label.setText("Key Pressed: " + e.getKeyCode());

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

public static void main(String[] args) {

new SpecialKeysDemo();

}
}

Practical no8 – Write a program to change the background color of applet when
user performs events using Mouse.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MouseEventDemo extends JFrame implements MouseListener {

public MouseEventDemo() {

setSize(400, 300);

addMouseListener(this);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public void mouseClicked(MouseEvent e) {

getContentPane().setBackground(Color.YELLOW);

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public static void main(String[] args) {


new MouseEventDemo();

Practical no9 – Develop a program using InetAddress class to retrieve IP


address of computer when hostname is entered by the user.

import java.net.*;

import java.util.Scanner;

public class IPAddressRetriever {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter hostname: ");

String hostname = sc.nextLine();

try {

InetAddress ip = InetAddress.getByName(hostname);

System.out.println("IP Address: " + ip.getHostAddress());

} catch (Exception e) {

System.out.println("Error: " + e.getMessage());

You might also like