Search This Blog

Sunday 21 August 2022

B8. NEP Java Program to create Menu Bar and Pull-Down Menus.

// ProgB8. NEP Java Program to create Menu Bar and Pull-Down Menus.

 

import java.awt.*; 

class ProgB8 

{ 

    ProgB8()

    { 

        Frame f= new Frame("Menu Bar and Pull Down Menus"); 

        MenuBar mb=new MenuBar(); 

        Menu menu=new Menu("MainMenu"); 

        Menu submenu=new Menu("SubMenu"); 

        MenuItem i1=new MenuItem("Item1"); 

        MenuItem i2=new MenuItem("Item2"); 

        MenuItem i3=new MenuItem("Item3"); 

        MenuItem i4=new MenuItem("Item4"); 

        MenuItem i5=new MenuItem("Item5"); 

        menu.add(i1); 

        menu.add(i2); 

        menu.add(i3); 

        submenu.add(i4); 

        submenu.add(i5); 

        menu.add(submenu); 

        mb.add(menu); 

        f.setMenuBar(mb); 

        f.setSize(500,500); 

        f.setLayout(null); 

        f.setVisible(true);

        f.setBackground(Color.blue);

    } 

    public static void main(String args[]) 

    { 

              new ProgB8(); 

    } 

}

B7. NEP Java program to demonstrate various mouse handling events using suitable example.

// ProgB7. NEP Java program to demonstrate various mouse handling events using suitable example.

 

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/*

<applet code="ProgB7" height="300" width="500">

</applet>

*/

public class ProgB7 extends Applet implements MouseListener

{

    Font f1;

    String msg="Demonstration of Mouse Events";

    public void init()

    {

        f1 = new Font("Georgia",Font.BOLD,22);

        addMouseListener(this);

    }

    public void mouseClicked(MouseEvent me)

    {

        msg = "Mouse Clicked";

        repaint();

    }

    public void mousePressed(MouseEvent me)

    {

        msg = "Mouse Pressed";

        repaint();

    }

    public void mouseReleased(MouseEvent me)

    {

        msg = "Mouse Released";

        repaint();

    }

    public void mouseEntered(MouseEvent me)

    {

        msg = "Mouse Entered";

        repaint();

    }

    public void mouseExited(MouseEvent me)

    {

        msg = "Mouse Exited";

        repaint();

    }

    public void paint(Graphics g)

    {

        g.setColor(Color.blue);

        g.setFont(f1);

        g.drawString(msg,20,20);

    }

}

B6. NEP Java Program to create a window when we press M or m the window displays Good Morning, A or a the window displays Good After Noon

/* B6. NEP Java Program to create a window when we press M or m the window displays Good Morning, A or a the window displays Good After Noon E or e the window displays Good Evening, N or n the window displays Good Night */

 

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*<applet code="ProgB6" width=500 height=500> </applet>*/

public class ProgB6 extends Applet implements KeyListener

{

        String msg="Press M or A or E or N";

        int X=10,Y=20;

        Font f1;

        public void init()

        {

        f1= new Font("Georgia",Font.BOLD, 22);

                  addKeyListener(this);

        }

        public void keyPressed(KeyEvent ke)

        {

                  int key=ke.getKeyCode();

                  switch(key)

                  {

 

             case KeyEvent.VK_M: msg="Good Morning!";

                            break;

             case KeyEvent.VK_A:     msg="Good Afternoon!";

                            break;

             case KeyEvent.VK_E:     msg="Good Evening!";

                            break;

             case KeyEvent.VK_N:     msg="Good Night!";

                            break;

                           

        }

        repaint();

        }

        public void keyReleased(KeyEvent ke)

        {

                //showStatus("Key UP");

        }

        public void keyTyped(KeyEvent ke)

        {

               repaint();

        }

        public void paint(Graphics g)

        {

        g.setFont(f1);

        g.setColor(Color.blue);

                  g.drawString(msg,X,Y);

        }

}

B5. NEP Java Program to move any one shape according to the arrow key pressed.

/* B5. NEP Java Program to move any one  shape according to the arrow key pressed. */

 

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*<applet code="ProgB55" width=500 height=500> </applet>*/

public class ProgB55 extends Applet implements KeyListener

{

        int X=150,Y=160, a=80,b=80;

        public void init()

        {

                addKeyListener(this);

        }

        public void keyPressed(KeyEvent ke)

        {

               

                int key=ke.getKeyCode();

                switch(key)

                {

        case KeyEvent.VK_RIGHT: X=200;

                            Y=160;

                            a=80;

                            b=80;

                            showStatus("Right Key");

                                break;

 

         case KeyEvent.VK_LEFT: X=100;

                            Y=160;

                            a=80;

                            b=80;

                            showStatus("Left Key");

                                break;

                       

        case KeyEvent.VK_UP:   X=150;

                            Y=110;

                            a=80;

                            b=80;

                            showStatus("Up Key");

                            break;

 

        case KeyEvent.VK_DOWN:  X=150;

                            Y=210;

                            a=80;

                            b=80;

                            showStatus("Down Key");

                            break;

               }

               repaint();

        }

        public void keyReleased(KeyEvent ke)

        {

                //showStatus("Key UP");

        }

        public void keyTyped(KeyEvent ke)

        {

                repaint();

        }

        public void paint(Graphics g)

        {

             g.setColor(Color.red);

             g.drawString("USE ARROW KEYS TO MOVE SHAPE",10,20);

             g.setColor(Color.blue);

             g.fillRect(X,Y,a,b);

 

        }

}

B4. NEP Java Program which creates a frame with two buttons father and mother.

//B4. NEP Java Program which creates a frame with two buttons father and mother. When we click the father button the name of the father, his age and designation must appear. When we click mother similar details of mother also appear. */

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

class FM extends JFrame implements ActionListener

{

    // Declaration of object for JButton class.

    JButton b1, b2;

    

    // Constructor of FM class

    FM()

    {

        // Setting layout as null for JFrame.

        this.setLayout(null);

        

        // Initialization of objects for "JButton" class.

        b1 = new JButton("Father");

        b2 = new JButton("Mother");

 

        // Setting Bounds for JButtons.

        b1.setBounds(100, 05, 80, 50);

        b2.setBounds(200, 05, 80, 50);

 

        // Adding JButton on JFrame.

        this.add(b1);

        this.add(b2);

        

        // Adding Listener to JButton.

        b1.addActionListener(this);

        b2.addActionListener(this);

    }

 

    public void actionPerformed(ActionEvent ae)

    {

        if (ae.getSource() == b1)

        {

            // Code To popup Dialog.

            JOptionPane.showMessageDialog(this, "Name of the Father: abc\nAge:50\nDesignation:Engineer");

        }

        if(ae.getSource() == b2)

        {

            // Code To popup Dialog.

            JOptionPane.showMessageDialog(this, "Name of the Mother: xyz\nAge:48\nDesignation:Lawyer");;

        }

    }

}

 

class Progb4

{

    public static void main(String args[])

    {

        // Creating Object for FM class.

        FM  obj = new FM();

        

        // Setting Bounds for a Frame.

        obj.setBounds(200, 200, 400, 300);

        

        // Set Resizable status of frame as false

        obj.setResizable(false);

        

        // Set Visible status of frame as true.

        obj.setVisible(true);

    }

}

FREE Hit Counters