Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views38 pages

Week6 - Appplet - Awt

Uploaded by

kalaskarrutuja0
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)
21 views38 pages

Week6 - Appplet - Awt

Uploaded by

kalaskarrutuja0
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/ 38

Week6

11. B) Write an applet application to display Table lamp. The color of lamp should get change randomly.

[25 M]

import java.awt.*;

import java.applet.*;

// Define a class named "slip11b" that extends the "Applet" class

public class slip11b extends Applet {

// Declare three floating-point variables for color components R, G, and B

public float R, G, B;

// Declare a Graphics object

Graphics gl;

// Initialize the applet

public void init() {

// Call repaint to trigger a repaint of the applet

repaint();

// Paint method where graphics are drawn

public void paint(Graphics g) {

// Generate random values for R, G, and B components

R = (float) Math.random();

G = (float) Math.random();

B = (float) Math.random();

// Create a Color object with the random RGB values


Color cl = new Color(R, G, B);

// Draw a rectangle

g.drawRect(0, 250, 290, 290);

// Draw two vertical lines

g.drawLine(125, 250, 125, 160);

g.drawLine(175, 250, 175, 160);

// Draw two arcs

g.drawArc(85, 157, 130, 50, -65, 312);

g.drawArc(85, 87, 130, 50, 62, 58);

// Draw two diagonal lines

g.drawLine(85, 177, 119, 89);

g.drawLine(215, 177, 181, 89);

// Set the color to the random color

g.setColor(cl);

// Fill two arcs and an oval with the random color

g.fillArc(78, 120, 40, 40, 63, -174);

g.fillOval(120, 96, 40, 40);

g.fillArc(173, 100, 40, 40, 110, 180);

/*

HTML code to embed the applet:

<applet code="slip11b.class" width="300" height="300">


</applet>

*/

/*12.B) Write a java program to display multiplication table of a given number into the List

box by clicking on button. [25 M] */

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

// Define a class named "slip12b" that extends the "Applet" class and implements "ActionListener"

public class slip12b extends Applet implements ActionListener {

// Declare a Button named "b1" with label "Show"

Button b1 = new Button("Show");

// Declare a List named "Multi" to hold items

List Multi = new List();

// Declare a String variable "str"

String str = "";

// Initialize the applet

public void init() {

// Add items "1" through "10" to the List

Multi.add("1");

Multi.add("2");

Multi.add("3");

Multi.add("4");

Multi.add("5");
Multi.add("6");

Multi.add("7");

Multi.add("8");

Multi.add("9");

Multi.add("10");

// Add the List and the Button to the applet

add(Multi);

add(b1);

// Add an ActionListener to the Button

b1.addActionListener(this);

// Paint method where graphics are drawn

public void paint(Graphics g) {

int count = 100;

// Parse the selected item from the List as an integer

int num = Integer.parseInt(Multi.getSelectedItem());

// Display the multiplication table for the selected number

for (int i = 1; i <= 10; i++) {

int a = num * i;

g.drawString(i + " * " + i + " = " + a, 100, count);

count = count + 20;

}
// ActionListener method for the Button

public void actionPerformed(ActionEvent e) {

repaint();

/*

HTML code to embed the applet:

<applet code="slip12b.class" width="300" height="300">

</applet>

*/

//15.B) Write an applet application to display smiley face. [25 M]

import java.applet.Applet;

import java.awt.Graphics;

// Define a class named "slip15b" that extends the "Applet" class

public class slip15b extends Applet {

// Paint method where graphics are drawn

public void paint(Graphics g) {

// Draw an oval for the face with coordinates (80, 70) and size 150x150

g.drawOval(80, 70, 150, 150);

// Fill two ovals for the eyes at (120, 120) and (170, 120) with a size of 15x15

g.fillOval(120, 120, 15, 15);

g.fillOval(170, 120, 15, 15);

// Draw an arc for the smile at (130, 180) with a size of 50x20 and angles from 180 to 360 degrees
g.drawArc(130, 180, 50, 20, 180, 180);

/*

HTML code to embed the applet:

<applet code="slip15b.class" width="300" height="300">

</applet>

*/

/*B) Design a screen in Java to handle the Mouse Events such as MOUSE_MOVED and

MOUSE_CLICK and display the position of the Mouse_Click in a TextField. [25 M] */

import java.awt.*;

import java.awt.event.*;

// Create a class named "MyFrame" that extends the "Frame" class

class MyFrame extends Frame {

TextField t, t1; // Declare two text fields for input/output

Label l, l1; // Declare two labels for displaying text

int x, y; // Declare integer variables for coordinates

Panel p; // Declare a panel for organizing components

// Constructor for the "MyFrame" class

MyFrame(String title) {

super(title); // Call the constructor of the base "Frame" class and set the title

setLayout(new FlowLayout()); // Set the layout manager for the frame


p = new Panel(); // Create a panel for organizing components

p.setLayout(new GridLayout(2, 2, 5, 5)); // Set the layout for the panel

t = new TextField(20); // Create a text field with a width of 20 characters

l = new Label("Co-ordinates of clicking"); // Create a label

l1 = new Label("Co-ordinates of movement"); // Create another label

t1 = new TextField(20); // Create another text field with a width of 20 characters

p.add(l); // Add the first label to the panel

p.add(t); // Add the first text field to the panel

p.add(l1); // Add the second label to the panel

p.add(t1); // Add the second text field to the panel

add(p); // Add the panel to the frame

addMouseListener(new MyClick()); // Add a mouse listener for mouse clicks

addMouseMotionListener(new MyMove()); // Add a mouse motion listener for mouse movement

setSize(500, 500); // Set the size of the frame

setVisible(true); // Make the frame visible

// Inner class "MyClick" that extends "MouseAdapter" for mouse click events

class MyClick extends MouseAdapter {

public void mouseClicked(MouseEvent me) {

x = me.getX(); // Get the X coordinate of the mouse click

y = me.getY(); // Get the Y coordinate of the mouse click

t.setText("X=" + x + " Y=" + y); // Display the coordinates in the first text field

}
}

// Inner class "MyMove" that extends "MouseMotionAdapter" for mouse movement events

class MyMove extends MouseMotionAdapter {

public void mouseMoved(MouseEvent me) {

x = me.getX(); // Get the X coordinate of the mouse movement

y = me.getY(); // Get the Y coordinate of the mouse movement

t1.setText("X=" + x + " Y=" + y); // Display the coordinates in the second text field

// Main class "Slip4" for running the program

class Slip4 {

public static void main(String args[]) {

MyFrame f = new MyFrame("Slip Number 4"); // Create an instance of the "MyFrame" class

}
/*B) Write a java program using Applet for bouncing ball. Ball should change its color

for each bounce. [25 M]*/

import java.applet.*;

import java.awt.*;

// Create a class named "slip29b" that extends the "Applet" class and implements the "Runnable" interface

public class slip29b extends Applet implements Runnable {

Thread t = null; // Declare a thread

int x1 = 10;

int y1 = 300;

int flagx1, flagy1;

int R, G, B;

// The "start" method is called when the applet is started

public void start() {

t = new Thread(this); // Create a new thread and pass the current object (this)

t.start(); // Start the thread

// Method to generate a random color

public void color() {

R = (int) (Math.random() * 256);

G = (int) (Math.random() * 256);

B = (int) (Math.random() * 256);

// The "run" method is part of the Runnable interface and contains the main logic of the applet

public void run() {


for (;;) {

try {

repaint(); // Request the applet to repaint itself

if (y1 <= 50) {

flagx1 = 0;

color(); // Generate a random color

} else if (y1 >= 300) {

flagx1 = 1;

color(); // Generate a random color

if (x1 <= 10) {

flagy1 = 0;

color(); // Generate a random color

} else if (x1 >= 400) {

flagy1 = 1;

color(); // Generate a random color

Thread.sleep(10); // The thread will sleep for 10 milliseconds

} catch (InterruptedException e) {

// The "paint" method is called to draw graphics on the applet

public void paint(Graphics g) {

Color color = new Color(R, G, B); // Create a color using RGB values

g.setColor(color); // Set the drawing color to the generated color

g.fillOval(x1, y1, 20, 20); // Fill an oval shape with the specified color and position
if (flagx1 == 1)

y1 -= 2; // Move the oval upward if flagx1 is 1

else if (flagx1 == 0)

y1 += 2; // Move the oval downward if flagx1 is 0

if (flagy1 == 0)

x1 += 4; // Move the oval rightward if flagy1 is 0

else if (flagy1 == 1)

x1 -= 4; // Move the oval leftward if flagy1 is 1

/*

* <applet code="slip29b.class" width="420" height="320">

* </applet>

*/

//B) Write a java program using Applet to implement a simple arithmetic calculator. [25M]

import java.awt.*;

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

// Create a class named "slip4b" that extends the "Applet" class and implements the "ActionListener"
interface

public class slip4b extends Applet implements ActionListener {

Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16;

String s1 = "", s2;

Frame f;

Panel p2;

TextField t;

int n1, n2;

// The "init" method is called when the applet is initialized

public void init() {

setLayout(new BorderLayout());

Frame title = (Frame) this.getParent().getParent();

title.setTitle("Simple Calc"); // Set the title of the parent frame

t = new TextField(); // Create a text field for input/output

p2 = new Panel(); // Create a panel for organizing components

p2.setLayout(new GridLayout(6, 4, 2, 1)); // Set a grid layout for the panel

p2.setFont(new Font("Times New Roman", Font.BOLD, 15)); // Set the font for the panel

// Create buttons and add action listeners to them

b1 = new Button("1");

b1.addActionListener(this);

b2 = new Button("2");

b2.addActionListener(this);

b3 = new Button("3");

b3.addActionListener(this);
b4 = new Button("+");

b4.addActionListener(this);

b5 = new Button("4");

b5.addActionListener(this);

b6 = new Button("5");

b6.addActionListener(this);

b7 = new Button("6");

b7.addActionListener(this);

b8 = new Button("-");

b8.addActionListener(this);

b9 = new Button("7");

b9.addActionListener(this);

b10 = new Button("8");

b10.addActionListener(this);

b11 = new Button("9");

b11.addActionListener(this);

b12 = new Button("*");

b12.addActionListener(this);

b13 = new Button("Clear");

b13.addActionListener(this);

b14 = new Button("0");

b14.addActionListener(this);

b15 = new Button("/");

b15.addActionListener(this);

b16 = new Button("=");

b16.addActionListener(this);

// Add components to the applet

add(t, "North");
p2.add(b9);

p2.add(b10);

p2.add(b11);

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.add(b7);

p2.add(b8);

p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(b12);

p2.add(new Label(""));

p2.add(b14);

p2.add(new Label(""));

p2.add(b15);

p2.add(new Label(""));

p2.add(new Label(""));

p2.add(new Label(""));

p2.add(b16);

add(p2);

p2.add(new Label(""));

p2.add(b13);

// The "actionPerformed" method handles action events from buttons

public void actionPerformed(ActionEvent e1) {

String str = e1.getActionCommand();

if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")) {


String str1 = t.getText();

s2 = str;

n1 = Integer.parseInt(str1);

s1 = "";

} else if (str.equals("=")) {

String str2 = t.getText();

n2 = Integer.parseInt(str2);

int sum = 0;

if (s2.equals("+"))

sum = n1 + n2;

else if (s2.equals("-"))

sum = n1 - n2;

else if (s2.equals("*"))

sum = n1 * n2;

else if (s2.equals("/"))

sum = n1 / n2;

String str1 = String.valueOf(sum);

t.setText("" + str1);

s1 = "";

} else if (str.equals("Clear")) {

t.setText(""); // Clear the text field

} else {

s1 += str;

t.setText("" + s1); // Update the text field with the input

/*
* <applet code="slip4b.class" height=250 width=250>

* </applet>

*/

Try it with the help of above program.

B) Write a java program that asks the user name, and then greets the user by name. Before outputting the
user's name, convert it to upper case letters. For example, if the user's name is Raj, then the program should
respond "Hello, RAJ, nice to meet you!". [25 M]

Try it with the help of above program.

//14.B) Write a java program to accept the details of employee (Eno, EName, Sal) and

//display it on next frame using appropriate event . [25 M]


import java.awt.*;

import java.awt.event.*;

// Create a class named "Emp_details" that implements the "ActionListener" interface

class Emp_details implements ActionListener {

Frame f; // Declare a frame for entering employee details

Label empno, empname, sal; // Declare labels for employee details

TextField tempno, tempname, tsal; // Declare text fields for entering details

Button next; // Declare a button for moving to the next frame

Emp_details() {

f = new Frame("\t Employee Details:"); // Create the main frame with a title

empno = new Label("\t Employee Id:"); // Create a label for Employee ID

empname = new Label("\t Employee Name:"); // Create a label for Employee Name

sal = new Label("\t Employee Sal:"); // Create a label for Employee Salary

tempno = new TextField(25); // Create a text field for Employee ID

tempname = new TextField(25); // Create a text field for Employee Name

tsal = new TextField(25); // Create a text field for Employee Salary

next = new Button("Next"); // Create a button for moving to the next frame

f.add(empno); // Add the Employee ID label to the main frame

f.add(tempno); // Add the Employee ID text field to the main frame

f.add(empname); // Add the Employee Name label to the main frame

f.add(tempname); // Add the Employee Name text field to the main frame

f.add(sal); // Add the Employee Salary label to the main frame

f.add(tsal); // Add the Employee Salary text field to the main frame

f.add(next); // Add the "Next" button to the main frame

next.addActionListener(this); // Register the "Next" button for action events


f.setLayout(new FlowLayout()); // Set the layout manager for the main frame

f.setSize(400, 400); // Set the size of the main frame

f.setVisible(true); // Make the main frame visible

// ActionListener implementation for handling button clicks

public void actionPerformed(ActionEvent ae) {

String empno, empname, sal; // Declare variables to store employee details

empno = tempno.getText(); // Get the Employee ID from the text field

empname = tempname.getText(); // Get the Employee Name from the text field

sal = tsal.getText(); // Get the Employee Salary from the text field

f.setVisible(false); // Hide the main frame

new FrameDetails(empno, empname, sal); // Create the next frame with employee details

// Create a class named "FrameDetails" that extends the "Frame" class

class FrameDetails extends Frame {

Frame f; // Declare a frame for displaying employee details

Label empno, empname, sal; // Declare labels for displaying employee details

TextField tempno, tempname, tsal; // Declare text fields for displaying details

FrameDetails(String no, String name, String s) {

f = new Frame("Employee Details:"); // Create the frame for displaying details

empno = new Label("Employee ID:"); // Create a label for Employee ID

empname = new Label("Employee Name:"); // Create a label for Employee Name

sal = new Label("Employee Salary:"); // Create a label for Employee Salary

tempno = new TextField(25); // Create a text field for Employee ID

tempname = new TextField(25); // Create a text field for Employee Name


tsal = new TextField(25); // Create a text field for Employee Salary

f.add(empno); // Add the Employee ID label to the frame

f.add(tempno); // Add the Employee ID text field to the frame

f.add(empname); // Add the Employee Name label to the frame

f.add(tempname); // Add the Employee Name text field to the frame

f.add(sal); // Add the Employee Salary label to the frame

f.add(tsal); // Add the Employee Salary text field to the frame

tempno.setText(no); // Set the Employee ID text field with the received value

tempname.setText(name); // Set the Employee Name text field with the received value

tsal.setText(s); // Set the Employee Salary text field with the received value

f.setLayout(new FlowLayout()); // Set the layout manager for the frame

f.setSize(400, 400); // Set the size of the frame

f.setVisible(true); // Make the frame visible

// Main class "slip14b" for running the program

class slip14b {

public static void main(String args[]) {

new Emp_details(); // Create an instance of the "Emp_details" class

}
/* 19.B) Create an Applet that displays the x and y position of the cursor movement

using Mouse and Keyboard. (Use appropriate listener) [25 M] */

import java.io.*;

import java.awt.event.*;

import java.applet.*;

import java.awt.*;

// Create a class named "slip19b" that extends the "Applet" class and implements the "MouseListener" and
"KeyListener" interfaces

public class slip19b extends Applet implements MouseListener, KeyListener {

private int x, y; // Declare variables for storing mouse coordinates

private String str = " "; // Declare a string for displaying messages

// The "init" method is called when the applet is initialized

public void init() {

this.addMouseListener(this); // Register the applet as a mouse listener

this.addKeyListener(this); // Register the applet as a key listener

// The "paint" method is called to draw graphics on the applet

public void paint(Graphics g) {

g.drawString(str, x, y); // Display the string at the specified coordinates (x, y)

// MouseListener methods

public void mouseClicked(MouseEvent m) {

x = m.getX(); // Get the X coordinate of the mouse click

y = m.getY(); // Get the Y coordinate of the mouse click


str = "x =" + x + ",y=" + y; // Update the string with mouse coordinates

repaint(); // Request the applet to repaint itself

public void mousePressed(MouseEvent m) {

// Other mouse event methods (mousePressed, mouseReleased, mouseEntered, mouseExited)

// are implemented in a similar way.

public void mouseReleased(MouseEvent m) {

// Implementation for mouseReleased

public void mouseEntered(MouseEvent m) {

// Implementation for mouseEntered

public void mouseExited(MouseEvent m) {

// Implementation for mouseExited

// KeyListener methods

public void keyPressed(KeyEvent ke) {

showStatus("key pressed"); // Display "key pressed" status in the applet status bar

public void keyReleased(KeyEvent ke) {

showStatus("key released"); // Display "key released" status in the applet status bar

}
public void keyTyped(KeyEvent ke) {

str += ke.getKeyChar(); // Append the typed character to the string

repaint(); // Request the applet to repaint itself

/*

<applet code="slip19b.class" width="300" height="300">

</applet>

*/

//B) Write a java program using applet to draw Temple. [25 M]

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

// Create a class named "slip26b" that extends the "Applet" class

public class slip26b extends Applet {

// The "init" method is called when the applet is initialized

public void init() {

setBackground(Color.BLACK); // Set the background color of the applet to black


}

// The "paint" method is called to draw graphics on the applet

public void paint(Graphics g) {

g.setColor(Color.WHITE); // Set the drawing color to white

g.drawRect(100, 150, 90, 120); // Draw a white rectangle

g.drawRect(130, 230, 20, 40); // Draw another white rectangle

g.drawLine(150, 100, 100, 150); // Draw a white line

g.drawLine(150, 100, 190, 150); // Draw another white line

g.drawLine(150, 50, 150, 100); // Draw a third white line

g.setColor(Color.ORANGE); // Set the drawing color to orange

g.drawRect(150, 50, 20, 20); // Draw an orange rectangle

/*

<applet code="slip26b.class" width="300" height="300">

</applet>

*/
/*B) Write a java program to accept directory name in TextField and display list of files

and subdirectories in List Control from that directory by clicking on Button. [25 M] */

import java.awt.*;

import java.awt.event.*;

import java.io.*;

// Create a class named "slip27b" that extends the "Frame" class and implements the "ActionListener"
interface

public class slip27b extends Frame implements ActionListener {

Graphics g;

List l;

TextField t1;

Button b1;

Label l1;

public slip27b() {

this.setLayout(new FlowLayout()); // Set the layout manager for the frame

this.setSize(400, 400); // Set the size of the frame

this.setVisible(true); // Make the frame visible

l1 = new Label("Enter Directory "); // Create a label for entering directory

t1 = new TextField(20); // Create a text field for entering directory path

l = new List(10); // Create a list for displaying directory contents

b1 = new Button("Display"); // Create a button for displaying directory contents

l1.setBounds(50, 100, 80, 80); // Set the bounds of the label

t1.setBounds(50, 150, 80, 80); // Set the bounds of the text field
b1.setBounds(50, 200, 80, 80); // Set the bounds of the button

l.setBounds(50, 300, 100, 100); // Set the bounds of the list

add(l1); // Add the label to the frame

add(t1); // Add the text field to the frame

add(b1); // Add the button to the frame

add(l); // Add the list to the frame

b1.addActionListener(this); // Register the button for action events

// ActionListener method for handling button clicks

public void actionPerformed(ActionEvent e) {

if (e.getSource() == b1) {

try {

String nm = t1.getText(); // Get the directory path from the text field

File f1 = new File(nm + ":"); // Create a File object with the directory path

String s1[] = f1.list(); // Get a list of files and directories in the specified path

if (s1 == null) {

l.add("Dir not exist"); // Display a message if the directory doesn't exist

} else {

for (int i = 0; i < s1.length; i++) {

l.add(s1[i]); // Add each file/directory name to the list

} catch (Exception ee) {}

// Main method for running the program

public static void main(String args[]) {


new slip27b(); // Create an instance of the "slip27b" class

/*Slip 21 B) Create a hash table containing city name & STD code. Display the details of the hash table.

Also search for a specific city and display STD code of that city. */

import java.util.*;

import java.io.*;

public class slip21b {

public static void main(String args[])

Hashtable h1=new Hashtable();

/*A Hashtable is an array of a list. Each list is known as a bucket.

The position of the bucket is identified by calling the hashcode() method.

A Hashtable contains values based on the key. Java Hashtable class contains unique elements. */
Enumeration en;

//An enumeration (enum for short) in Java is a special data type which contains a set of predefined constants

int i,n,std,val,max=0;

String nm, cname, str, s=null;

DataInputStream dr = new DataInputStream(System.in);

//A data input stream lets an application read primitive Java data types from an underlying input stream in a
machine-independent way.

try

System.out.print("Enter the Now Many Record You Want : ");

n = Integer.parseInt(dr.readLine());

System.out.print("Enter the City Name & STD Code : ");

for(i=0; i<n; i++)

cname = dr.readLine();

std = Integer.parseInt(dr.readLine());

h1.put(cname,std);

System.out.print("Enter city name to search : ");

nm = dr.readLine();

en=h1.keys();

while(en.hasMoreElements())

str=(String)en.nextElement();

val=(Integer)h1.get(str);

if(str.equals(nm))
{

System.out.print("STD Code : " + val);

} catch (Exception e) {}

23.

import java.awt.event.*;

import javax.swing.*;

public class slip23b extends JFrame implements ActionListener {

public static void main(String s[]) {

new slip23b();
}

public slip23b() {

// Set the size and location of the JFrame

this.setSize(600, 500);

this.setLocation(200, 200);

// Create a menu bar

JMenuBar menuBar = new JMenuBar();

// Create menu items for File, Edit, and Search

JMenu filMenu = new JMenu("File");

JMenu filEdit = new JMenu("Edit");

JMenu filSearch = new JMenu("Search");

// Create menu items for File menu

JMenuItem OpenItem = new JMenuItem("Open");

JMenuItem SaveItem = new JMenuItem("Save");

JMenuItem QuitItem = new JMenuItem("Quit");

// Create menu items for Edit menu

JMenuItem UndoItem = new JMenuItem("Undo");

JMenuItem RedoItem = new JMenuItem("Redo");

JMenuItem CutItem = new JMenuItem("Cut");

JMenuItem CopyItem = new JMenuItem("Copy");

JMenuItem PasteItem = new JMenuItem("Paste");

// Create icons for menu items

ImageIcon OpenIcon = new ImageIcon("icons/open.png");


ImageIcon SaveIcon = new ImageIcon("icons/Save.png");

ImageIcon QuitIcon = new ImageIcon("icons/Delete.png");

ImageIcon UndoIcon = new ImageIcon("icons/Undo.png");

ImageIcon RedoIcon = new ImageIcon("icons/Redo.png");

ImageIcon CutIcon = new ImageIcon("icons/Cut.png");

ImageIcon CopyIcon = new ImageIcon("icons/Copy.png");

ImageIcon PasteIcon = new ImageIcon("icons/Past.png");

// Add menu items to File menu

filMenu.add(OpenItem);

filMenu.add(SaveItem);

filMenu.add(QuitItem);

// Add menu items to Edit menu

filEdit.add(UndoItem);

filEdit.add(RedoItem);

filEdit.add(CutItem);

filEdit.add(CopyItem);

filEdit.add(PasteItem);

// Set icons for menu items

OpenItem.setIcon(OpenIcon);

SaveItem.setIcon(SaveIcon);

QuitItem.setIcon(QuitIcon);

UndoItem.setIcon(UndoIcon);

RedoItem.setIcon(RedoIcon);

CutItem.setIcon(CutIcon);

CopyItem.setIcon(CopyIcon);

PasteItem.setIcon(PasteIcon);
// Add menus to the menu bar

menuBar.add(filMenu);

menuBar.add(filEdit);

menuBar.add(filSearch);

// Set the menu bar for the JFrame

this.setJMenuBar(menuBar);

// Make the JFrame visible

this.setVisible(true);

public void actionPerformed(ActionEvent e) {

// TODO: Implement action handling for menu items

28. B) Write a java Program to accept the details of 5 employees (Eno, Ename, Salary) and display it onto the
JTable.
import javax.swing.*;

import javax.swing.table.DefaultTableModel;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class slip28b extends JFrame implements ActionListener {

private DefaultTableModel tableModel; // Table model to store data for JTable

private JTable table; // JTable to display employee details

private JTextField enoField, enameField, salaryField; // Text fields for data entry

private JButton addButton; // Button to add employee details

private int rowCount = 0; // Count of rows added to the table

public slip28b() {

setTitle("Employee Details"); // Set the title of the JFrame

setSize(500, 400); // Set the size of the JFrame

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close operation when the window is closed

// Create a table model with columns: Eno, Ename, Salary

tableModel = new DefaultTableModel();

tableModel.addColumn("Eno");

tableModel.addColumn("Ename");

tableModel.addColumn("Salary");

// Create a JTable with the model

table = new JTable(tableModel);

// Create labels and text fields for data entry

JLabel enoLabel = new JLabel("Employee Number:");


JLabel enameLabel = new JLabel("Employee Name:");

JLabel salaryLabel = new JLabel("Salary:");

enoField = new JTextField(10);

enameField = new JTextField(10);

salaryField = new JTextField(10);

// Create an "Add" button

addButton = new JButton("Add");

addButton.addActionListener(this); // Register ActionListener for the button

// Create a panel for input components

JPanel inputPanel = new JPanel();

inputPanel.setLayout(new GridLayout(4, 2));

inputPanel.add(enoLabel);

inputPanel.add(enoField);

inputPanel.add(enameLabel);

inputPanel.add(enameField);

inputPanel.add(salaryLabel);

inputPanel.add(salaryField);

inputPanel.add(addButton);

// Create a scroll pane for the JTable

JScrollPane scrollPane = new JScrollPane(table);

// Add components to the frame

Container container = getContentPane();

container.setLayout(new BorderLayout());

container.add(inputPanel, BorderLayout.NORTH);
container.add(scrollPane, BorderLayout.CENTER);

setVisible(true); // Make the JFrame visible

// ActionListener implementation for the "Add" button

public void actionPerformed(ActionEvent e) {

if (e.getSource() == addButton) {

String eno = enoField.getText();

String ename = enameField.getText();

String salary = salaryField.getText();

// Add data to the table model

tableModel.addRow(new String[]{eno, ename, salary});

// Clear input fields

enoField.setText("");

enameField.setText("");

salaryField.setText("");

rowCount++; // Increment the row count

// Check if 5 employees' details have been added and disable the "Add" button

if (rowCount == 5) {

addButton.setEnabled(false);

}
public static void main(String[] args) {

new slip28b(); // Create an instance of the application

30.

Try with the help of given program.

7. A) Write a Java program to display the Label with the text “Dr. D Y Patil College”, a background color Red,
and a font size of 20 on the frame. [15 M]

import java.awt.*;

import java.awt.event.*;

public class slip7a extends Frame {


// The paint method is called to draw on the Frame

public void paint(Graphics g) {

// Create a Font object with "Georgia" font, plain style, and size 20

Font f = new Font("Georgia", Font.PLAIN, 20);

// Set the font for graphics context

g.setFont(f);

// Draw the string "Dr D Y Patil College" at coordinates (50, 70)

g.drawString("Dr D Y Patil College", 50, 70);

// Set the background color of the Frame to red

setBackground(Color.RED);

public static void main(String args[]) {

// Create an instance of the slip7a class (a Frame)

slip7a sl = new slip7a();

// Make the Frame visible

sl.setVisible(true);

// Set the size of the Frame to 200 pixels in width and 300 pixels in height

sl.setSize(200, 300);

}
/* 20A) Write a java program using AWT to create a Frame with

title TYBBACA, background color RED. If user clicks on close

button then frame should close. [15 M] */

import javax.swing.*;

import java.awt.*;

class slip20a {

public static void main(String args[]) {

// Create a JFrame with the title "TYBBACA"

JFrame frame = new JFrame("TYBBACA");

// Set the size of the JFrame to 400 pixels in width and 400 pixels in height

frame.setSize(400, 400);

// Specify what happens when the JFrame is closed

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the background color of the content pane to red

frame.getContentPane().setBackground(Color.RED);

// Make the JFrame visible

frame.setVisible(true);

You might also like