Tuesday, July 30, 2024

JAVA-I Assignment -5

 Assignment 5: GUI Designing, Event Handling

Set A

a) Write a java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -, *, % operations. Add a text field to display the result.

Simple Calculator



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

public class SimpleCalculator extends JFrame implements ActionListener {
    JTextField txt1;
    JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bsum, bsub, bmult, bdiv,bdot, beql;
    JPanel p1, p2;
    String opt = "", s1 = "";
    float n1, n2;
    int s2;
    int aft = 0;

    SimpleCalculator() {
        setSize(300, 300);
        setVisible(true);
        setTitle("Simple Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        
        p1 = new JPanel();
        p2 = new JPanel();

        p1.setLayout(new FlowLayout());

        txt1 = new JTextField(15);
        p1.add(txt1);
        p2.setLayout(new GridLayout(4, 4, 6, 6));

        b1 = new JButton("1");
        p2.add(b1);
        b1.addActionListener(this);
        b2 = new JButton("2");
        p2.add(b2);
        b2.addActionListener(this);
        b3 = new JButton("3");
        p2.add(b3);
        b3.addActionListener(this);
        bsum = new JButton("+");
        p2.add(bsum);
        bsum.addActionListener(this);
        b4 = new JButton("4");
        p2.add(b4);
        b4.addActionListener(this);
        b5 = new JButton("5");
        p2.add(b5);
        b5.addActionListener(this);
        b6 = new JButton("6");
        p2.add(b6);
        bsub = new JButton("-");
        p2.add(bsub);
        bsub.addActionListener(this);
        b6.addActionListener(this);
        b7 = new JButton("7");
        p2.add(b7);
        b7.addActionListener(this);
        b8 = new JButton("8");
        p2.add(b8);
        b8.addActionListener(this);
        b9 = new JButton("9");
        p2.add(b9);
        b9.addActionListener(this);
        bmult = new JButton("*");
        p2.add(bmult);
        bmult.addActionListener(this);
        b0 = new JButton("0");
        p2.add(b0);
        b0.addActionListener(this);
        bdot = new JButton(".");
        p2.add(bdot);
        bdot.addActionListener(this);
        beql = new JButton("=");
        p2.add(beql);
        beql.addActionListener(this);
        bdiv = new JButton("/");
        p2.add(bdiv);
        bdiv.addActionListener(this);

        add(p1, BorderLayout.NORTH);
        add(p2, BorderLayout.SOUTH);

    }

    public static void main(String[] args) {
        SimpleCalculator sc = new SimpleCalculator();
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == b1)
            s1 = s1 + b1.getText();
        if (e.getSource() == b2)
            s1 = s1 + b2.getText();
        if (e.getSource() == b3)
            s1 = s1 + b3.getText();
        if (e.getSource() == b4)
            s1 = s1 + b4.getText();
        if (e.getSource() == b5)
            s1 = s1 + b5.getText();
        if (e.getSource() == b6)
            s1 = s1 + b6.getText();
        if (e.getSource() == b7)
            s1 = s1 + b7.getText();
        if (e.getSource() == b8)
            s1 = s1 + b8.getText();
        if (e.getSource() == b9)
            s1 = s1 + b9.getText();
        if (e.getSource() == b0)
            s1 = s1 + b0.getText();
        if (e.getSource() == bdot)
            s1 = s1 + bdot.getText();

        txt1.setText(s1);

        if (e.getSource() == bsum) {
            opt = "+";
            txt1.setText("");
            n1 = Float.parseFloat(s1);
            s1 = "";
        }
        if (e.getSource() == beql) {
            if (opt.equals("+")) {
                n2 = Float.parseFloat(s1);
                txt1.setText((n1 + n2) + "");
                s1 = "";

            }
        } else if (e.getSource() == bsub) {
            opt = "-";
            txt1.setText("");
            n1 = Float.parseFloat(s1);
            s1 = "";
        }
        if (e.getSource() == beql) {
            if (opt.equals("-")) {
                n2 = Float.parseFloat(s1);
                txt1.setText((n1 - n2) + "");
                s1 = "";
            }
        } else if (e.getSource() == bmult) {
            opt = "*";
            txt1.setText("");
            n1 = Float.parseFloat(s1);
            s1 = "";
        }
        if (e.getSource() == beql) {
            if (opt.equals("*")) {
                n2 = Float.parseFloat(s1);
                txt1.setText((n1 * n2) + "");
                s1 = "";
            }
        }

        else if (e.getSource() == bdiv) {
            opt = "/";
            txt1.setText("");
            n1 = Float.parseFloat(s1);
            s1 = "";
        }
        if (e.getSource() == beql) {
            if (opt.equals("/")) {
                n2 = Float.parseFloat(s1);
                txt1.setText((n1 / n2) + "");
                s1 = "";
            }
        }
    }
}


b) Design a screen to handle the Mouse Events such as MOUSE_MOVED and MOUSE_CLICK and display the position of the Mouse_Click in a TextField.



// import java.awt.event.MouseListener;

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;


public class MouseEvents extends JFrame implements MouseListener, MouseMotionListener {

    JTextField txt1;


    MouseEvents() {

        setSize(500, 500);

        // setLayout(null);

        setLayout(new FlowLayout());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        txt1 = new JTextField(30);

        add(txt1);

        addMouseListener(this);

        addMouseMotionListener(this);

        setVisible(true);

    }


    // MouseMotionListener Mothods

    @Override

    public void mouseDragged(MouseEvent e) {


    }


    @Override

    public void mouseMoved(MouseEvent e) {

        txt1.setText("Mouse Moved " + e.getX() + "," + e.getY());


    }

    // MouseListener Mothods


    @Override

    public void mouseClicked(MouseEvent e) {

        txt1.setText("Mouse Clicked " + e.getX() + "," + e.getY());

        

    }


    @Override

    public void mousePressed(MouseEvent e) {


    }


    @Override

    public void mouseReleased(MouseEvent e) {


    }


    @Override

    public void mouseEntered(MouseEvent e) {


    }


    @Override

    public void mouseExited(MouseEvent e) {


    }


    public static void main(String[] args) {

        MouseEvents mouseEvents = new MouseEvents();

    }

}


Set B

a) Create the following GUI screen using appropriate layout managers. Accept the name, class , hobbies of the user and apply the changes and display the selected options in a text box.


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;


class SetB1 extends JFrame implements ActionListener

{

    JLabel name,class1,hobbies,font,style,size;

    JButton clear_button;

    JRadioButton r1,r2,r3;

    JCheckBox c1,c2,c3;

    JTextField name_textfield,final_textfield;

    ButtonGroup b1;

    JPanel p1,p2;

    JComboBox fontcb,sizecb;

    JCheckBox bold,italic,underline;

    static int cnt;

    private StringBuffer s1 = new StringBuffer();

    

    SetB1()

    {

        b1=new ButtonGroup();

        p1=new JPanel();

        p2=new JPanel();

        clear_button=new JButton("clear");

        clear_button.addActionListener(this);

        

        name=new JLabel("Your name");

        class1=new JLabel("Your class");

        hobbies=new JLabel("Your hobbies");

        font=new JLabel("font");

        style=new JLabel("style");

        size=new JLabel("size");

        name_textfield=new JTextField(20);

        final_textfield=new JTextField(30);

        

        

        r1=new JRadioButton("FY");

        r2=new JRadioButton("SY");

        r3=new JRadioButton("TY");

        

        r1.addActionListener(this);

        r2.addActionListener(this);

        r3.addActionListener(this);

        

        c1=new JCheckBox("Music");

        c2=new JCheckBox("Dance");

        c3=new JCheckBox("Sports");

        

        c1.addActionListener(this);

        c2.addActionListener(this);

        c3.addActionListener(this);

        

        fontcb=new JComboBox();

        fontcb.addItem("Arial");

        fontcb.addItem("Sans");

        fontcb.addItem("Monospace");

        

        bold=new JCheckBox("Bold");

        italic=new JCheckBox("Italic");

        underline=new JCheckBox("Underline");

        

        size=new JLabel("Size");

        

        sizecb=new JComboBox();

        sizecb.addItem("10");

        sizecb.addItem("15");

        sizecb.addItem("18");

        

        p1.setLayout(new GridLayout(5,2));

        

        p1.add(name);

        p1.add(name_textfield);

        

        p1.add(class1);

        p1.add(hobbies);

        p1.add(style);

        p1.add(font);

        

        p1.add(r1);

        p1.add(c1);

        p1.add(bold);

        p1.add(fontcb);

        

        p1.add(r2);

        p1.add(c2);

        p1.add(italic);

        p1.add(size);

        

        p1.add(r3);

        p1.add(c3);

        p1.add(underline);

        p1.add(sizecb);

        

        p2.setLayout(new FlowLayout());

        p2.add(clear_button);

        p2.add(final_textfield);

        

        setLayout(new BorderLayout());

        add(p1,BorderLayout.NORTH);

        add(p2,BorderLayout.EAST);

        

        setSize(400,200);

        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        

    }

    

    public void actionPerformed(ActionEvent e)

    {

        if(e.getSource()==r1)

        {

            cnt++;

            if(cnt==1)

            {

                String s = name_textfield.getText();

                s1.append("Name : ");

                s1.append(s);

            }

            s1.append("Class : FY");

        }

        else if(e.getSource()==r2)

        {

            cnt++;

            if(cnt==1)

            {

                String s = name_textfield.getText();

                s1.append("Name : ");

                s1.append(s);

            }

            s1.append("Class : SY");

        }

        else if(e.getSource()==r3)

        {

            cnt++;

            if(cnt==1)

            {

                String s = name_textfield.getText();

                s1.append("Name : ");

                s1.append(s);

            }

            s1.append("Class : TY");

        }

        

        else if(e.getSource()==c1)

        {

            s1.append("Hobbies : MUsic");

        }

        else if(e.getSource()==c2)

        {

            s1.append("Hobbies : Dance");

        }

        else if(e.getSource()==c3)

        {

            s1.append("Hobbies : Sports");

        }

        

        final_textfield.setText(new String(s1));

        

        if(e.getSource()==clear_button)

        {

            final_textfield.setText(" ");

            name_textfield.setText(" ");

        }

        

        String f = (String)fontcb.getSelectedItem();

        System.out.println("font = "+f);

        final_textfield.setFont(new Font(f,Font.BOLD,10));

        String no = (String)sizecb.getSelectedItem();

        int num=Integer.parseInt(no);

        

        if(bold.isSelected())

        {

            final_textfield.setFont(new Font(f,Font.BOLD,num));

        }

        if(italic.isSelected())

        {

            final_textfield.setFont(new Font(f,Font.ITALIC,num));

        }

    }

    

    public static void main(String args[])

    {

        SetB1 s = new SetB1();

    }

}


b) Write a Java program to design a screen using Awt that will take a user name and password. If the user name and password are not same, raise an Exception with appropriate message. User can have 3 login chances only. Use clear button to clear the TextFields.

import java.awt.*;

import java.awt.event.*;

class InvalidPasswordException extends Exception

{

InvalidPasswordException()

{

System.out.println(” User name and Password is not same”);

}

}

public class PasswordDemo extends Frame implements ActionListener

{

Label uname,upass;

TextField nametext;

TextField passtext,msg;

Button login,Clear;

Panel p;

int attempt=0;

char c= ‘ * ‘ ;


public void login()

{

p=new Panel();

uname=new Label(“Use Name: ” ,Label.CENTER);

upass=new Label (“Password: “,Label.RIGHT);


nametext=new TextField(20);

passtext =new TextField(20);

passtext.setEchoChar(c);

msg=new TextField(10);

msg.setEditable(false);


login=new Button(“Login”);

Clear=new Button(“Clear”);

login.addActionListener(this);

Clear.addActionListener(this);


p.add(uname);

p.add(nametext);

p.add(upass);

p.add(passtext);

p.add(login);

p.add(Clear);

p.add(msg);

add(p);


setTitle(“Login “);

setSize(290,200);

setResizable(false);

setVisible(true);

}


public void actionPerformed(ActionEvent ae)

{

Button btn=(Button)(ae.getSource());

if(attempt<3)

{

if((btn.getLabel())==”Clear”)

{

nametext.setText(“”);

passtext.setText(“”);

}

if((btn.getLabel()).equals(“Login”))

{

try

{

String user=nametext.getText();

String upass=passtext.getText();


if(user.compareTo(upass)==0)

{

msg.setText(“Valid”);

System.out.println(“Username is valid”);

}

else

{

throw new InvalidPasswordException();

}

}

catch(Exception e)

{

msg.setText(“Error”);

}

attempt++;

}

}

else

{

System.out.println(“you are using 3 attempt”);

System.exit(0);

}

}

public static void main(String args[])

{

PasswordDemo pd=new PasswordDemo();

pd.login();

}

}

Set C

a) Write a java program to create the following GUI for user registration form



If above conditions are met, display “Registration Successful” otherwise “Registration Failed” after the user clicks the Submit button.


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

public class CowinFrame extends JFrame
{
JTextField adhar, byear, phone, hosp;
JPanel p1, p2, p3, p4;
JButton add, update, delete, view, search;
JRadioButton r1, r2, r3, r4, r5, r6, r7, r8;
JComboBox hos;
String s[] = { "Tambe Hospital", "Daima Hospital", "Nighute Hospital" };


CowinFrame() {
setTitle("Cowin Registration");

setSize(800, 600);

setLayout(new GridLayout(8, 2, 40, 40));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel adharno = new JLabel("Adhar Card Number: ");
add(adharno);
adhar = new JTextField(10);
add(adhar);

JLabel Byear = new JLabel("Birth Year: ");
add(Byear);
byear = new JTextField(10);
add(byear);

JLabel phoneNo = new JLabel("Mobile Number: ");
add(phoneNo);
phone = new JTextField(10);
add(phone);

// Age Radio Button
p1 = new JPanel();
p1.setLayout(new FlowLayout());

JLabel Age = new JLabel("Age Group : ");
add(Age);
r1 = new JRadioButton("18 & above");
p1.add(r1);
r2 = new JRadioButton("45 & above");
p1.add(r2);
add(p1);

JLabel hospital = new JLabel("Select Hospital: ");
add(hospital);
hos = new JComboBox(s);
add(hos);

// Vaccines Radio Button
p2 = new JPanel();
p2.setLayout(new FlowLayout());

JLabel Vaccines = new JLabel("Vaccines : : ");
add(Vaccines);

r3 = new JRadioButton("Covishield");
p2.add(r3);
r4 = new JRadioButton("Covaxin");
p2.add(r4);
r5 = new JRadioButton("Sputnik V");
p2.add(r5);
add(p2);

// TimeSlot Radio Button
p3 = new JPanel();
p3.setLayout(new FlowLayout());

JLabel Time = new JLabel("Time Slot : : ");
add(Time);
r6 = new JRadioButton("Morning");
p3.add(r6);
r7 = new JRadioButton("Afternoon");
p3.add(r7);
r8 = new JRadioButton("Evening");
p3.add(r8);
add(p3);

// Button
p4 = new JPanel();
p4.setLayout(new FlowLayout());

add = new JButton("Add");
p4.add(add);
update = new JButton("Update");
p4.add(update);
delete = new JButton("Delete");
p4.add(delete);
view = new JButton("View");
p4.add(view);
search = new JButton("Search");
p4.add(search);
add(p4);
setVisible(true);
}

public static void main(String[] args) {
new CowinFrame();
}// MAIN

}// CLASS

b) Write a program to display the following menus and sub-menus.



import javax.swing.*;


public class SwingMenu{

public static void main(String[] args) {

SwingMenu s = new SwingMenu();

}


public SwingMenu(){

JFrame frame = new JFrame("Creating a JMenuBar, JMenu, JMenuItem and seprator Component");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar menubar = new JMenuBar();

JMenu filemenu = new JMenu("File");

filemenu.add(new JSeparator());

JMenu editmenu = new JMenu("Edit");

editmenu.add(new JSeparator());

JMenuItem fileItem1 = new JMenuItem("New");

JMenuItem fileItem2 = new JMenuItem("Open");

JMenuItem fileItem3 = new JMenuItem("Close");

fileItem3.add(new JSeparator());

JMenuItem fileItem4 = new JMenuItem("Save");

JMenuItem editItem1 = new JMenuItem("Cut");

JMenuItem editItem2 = new JMenuItem("Copy");

editItem2.add(new JSeparator());

JMenuItem editItem3 = new JMenuItem("Paste");

JMenuItem editItem4 = new JMenuItem("Insert");

filemenu.add(fileItem1);

filemenu.add(fileItem2);

filemenu.add(fileItem3);

filemenu.add(fileItem4);

editmenu.add(editItem1);

editmenu.add(editItem2);

editmenu.add(editItem3);

editmenu.add(editItem4);

menubar.add(filemenu);

menubar.add(editmenu);

frame.setJMenuBar(menubar);

frame.setSize(400,400);

frame.setVisible(true);

}

}


No comments:

Post a Comment