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

PART-B Programs

JAVA B part lab programs

Uploaded by

nagabhushanaat
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)
23 views9 pages

PART-B Programs

JAVA B part lab programs

Uploaded by

nagabhushanaat
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

1.

import java.io.*;

import java.util.*;

class pb1

public static void main(String args[])

try

int[] array=new int[-5];

System.out.println("Array length:"+array.length);

catch(NegativeArraySizeException ne)

System.out.println("Negative array size exception occured");

ne.printStackTrace();

2.

import javax.swing.*;

class pb2 {

public static void main(String[] args) {

JFrame jFrame = new JFrame();

JOptionPane.showMessageDialog(jFrame, "Hello! Have a nice day.");

3.

import javax.swing.*;
import java.awt.*;

public class pb3 extends JPanel {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

// Draw a rectangle

g.drawRect(50, 50, 100, 50);

// Draw an oval

g.drawOval(200, 50, 100, 50);

// Draw a line

g.drawLine(50, 150, 250, 150);

public static void main(String[] args) {

JFrame frame = new JFrame("Shape Drawer");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(400, 300);

frame.setLocationRelativeTo(null); // Center the frame

// Create an instance of the pb3 class

pb3 shapeDrawer = new pb3();

// Add the ShapeDrawer instance to the frame

frame.add(shapeDrawer);

frame.setVisible(true);

4.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class pb4 extends JFrame implements ActionListener {


JLabel infoLabel;

JButton fatherButton, motherButton;

public pb4() {

setTitle("Family Information");

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new FlowLayout());

fatherButton = new JButton("Father");

motherButton = new JButton("Mother");

fatherButton.addActionListener(this);

motherButton.addActionListener(this);

add(fatherButton);

add(motherButton);

infoLabel = new JLabel();

add(infoLabel);

setVisible(true);

@Override

public void actionPerformed(ActionEvent e) {

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

infoLabel.setText("Father: Name - John, Age - 45, Designation - Engineer");

} else if (e.getSource() == motherButton) {

infoLabel.setText("Mother: Name - Mary, Age - 40, Designation - Teacher");

public static void main(String[] args) {

new pb4();

}
5.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class pb5 extends JPanel implements KeyListener {

private int x = 100; // initial x-coordinate of the shape

private int y = 100; // initial y-coordinate of the shape

private final int SQUARE_SIZE = 50; // size of the square

private final int STEP = 10; // step size for movement

public pb5() {

setPreferredSize(new Dimension(400, 400));

setBackground(Color.WHITE);

setFocusable(true);

addKeyListener(this);

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(Color.RED);

g.fillRect(x, y, SQUARE_SIZE, SQUARE_SIZE); // draw the square

@Override

public void keyPressed(KeyEvent e) {

int keyCode = e.getKeyCode();

switch (keyCode) {

case KeyEvent.VK_UP:

y -= STEP; // move up

break;

case KeyEvent.VK_DOWN:

y += STEP; // move down


break;

case KeyEvent.VK_LEFT:

x -= STEP; // move left

break;

case KeyEvent.VK_RIGHT:

x += STEP; // move right

break;

repaint(); // redraw the shape

@Override

public void keyTyped(KeyEvent e) {}

@Override

public void keyReleased(KeyEvent e) {}

public static void main(String[] args) {

JFrame frame = new JFrame("Shape Mover");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new pb5());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

6.

import javax.swing.*;

import java.awt.event.*;

public class pb6 extends JFrame implements KeyListener {

JLabel greetingLabel;

public pb6() {

setTitle("Greeting Window");
setSize(300, 100);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

greetingLabel = new JLabel("Press M, A, E, or N");

add(greetingLabel);

addKeyListener(this);

setVisible(true);

@Override

public void keyPressed(KeyEvent e) {

char key = e.getKeyChar();

switch (key) {

case 'M':

case 'm':

greetingLabel.setText("Good Morning!");

break;

case 'A':

case 'a':

greetingLabel.setText("Good Afternoon!");

break;

case 'E':

case 'e':

greetingLabel.setText("Good Evening!");

break;

case 'N':

case 'n':

greetingLabel.setText("Good Night!");

break;

default:

break;

}
}

@Override

public void keyTyped(KeyEvent e) {}

@Override

public void keyReleased(KeyEvent e) {}

public static void main(String[] args) {

new pb6();

7.

import java.awt.*;

import java.awt.event.*;

class pb7 extends Frame implements MouseListener{

Label l;

pb7(){

addMouseListener(this);

l=new Label();

l.setBounds(20,50,100,20);

add(l);

setSize(300,300);

setLayout(null);

setVisible(true);

public void mouseClicked(MouseEvent e) {

l.setText("Mouse Clicked");

public void mouseEntered(MouseEvent e) {

l.setText("Mouse Entered");
}

public void mouseExited(MouseEvent e) {

l.setText("Mouse Exited");

public void mousePressed(MouseEvent e) {

l.setText("Mouse Pressed");

public void mouseReleased(MouseEvent e) {

l.setText("Mouse Released");

public static void main(String[] args) {

new pb7();

8.

import java.awt.*;

import java.awt.event.*;

class pb8 extends Frame implements ActionListener

Menu states, cities;

public pb8()

MenuBar mb = new MenuBar(); // begin with creating menu bar

setMenuBar(mb); // add menu bar to frame

states = new Menu("Indian States"); // create menus

cities = new Menu("Indian Cities");

mb.add(states); // add menus to menu bar

mb.add(cities);

states.addActionListener(this); // link with ActionListener for event handling

cities.addActionListener(this);
states.add(new MenuItem("Himachal Pradesh"));

states.add(new MenuItem("Rajasthan"));

states.add(new MenuItem("West Bengal"));

states.addSeparator();// separates from north Indian states from south Indian

states.add(new MenuItem("Andhra Pradesh"));

states.add(new MenuItem("Tamilnadu"));

states.add(new MenuItem("Karnataka"));

cities.add(new MenuItem("Delhi"));

cities.add(new MenuItem("Jaipur"));

cities.add(new MenuItem("Kolkata"));

cities.addSeparator(); // separates north Indian cities from south Indian

cities.add(new MenuItem("Hyderabad"));

cities.add(new MenuItem("Chennai"));

cities.add(new MenuItem("Bengaluru"));

setTitle("Simple Menu Program"); // frame creation methods

setSize(300, 300);

setVisible(true);

public void actionPerformed(ActionEvent e)

String str = e.getActionCommand(); // know the menu item selected by the user

System.out.println("You selected " + str);

public static void main(String args[])

new pb8();

You might also like