TYBBA(CA) Core Java Slip No 14 Q B

Q. 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.*;

// First Frame
class EmpInput extends Frame implements ActionListener {

    TextField t1, t2, t3;
    Button b1;

    EmpInput() {
        setTitle("Employee Input");

        Label l1 = new Label("Emp No:");
        l1.setBounds(50, 50, 80, 30);
        add(l1);

        t1 = new TextField();
        t1.setBounds(140, 50, 120, 30);
        add(t1);

        Label l2 = new Label("Emp Name:");
        l2.setBounds(50, 90, 80, 30);
        add(l2);

        t2 = new TextField();
        t2.setBounds(140, 90, 120, 30);
        add(t2);

        Label l3 = new Label("Salary:");
        l3.setBounds(50, 130, 80, 30);
        add(l3);

        t3 = new TextField();
        t3.setBounds(140, 130, 120, 30);
        add(t3);

        b1 = new Button("Display");
        b1.setBounds(110, 180, 80, 30);
        add(b1);

        b1.addActionListener(this);

        setSize(320, 260);
        setLayout(null);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        String eno = t1.getText();
        String ename = t2.getText();
        String sal = t3.getText();

        new EmpDisplay(eno, ename, sal);
    }

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

// Second Frame
class EmpDisplay extends Frame {

    EmpDisplay(String eno, String ename, String sal) {
        setTitle("Employee Details");

        Label l1 = new Label("Emp No: " + eno);
        l1.setBounds(50, 60, 200, 30);
        add(l1);

        Label l2 = new Label("Emp Name: " + ename);
        l2.setBounds(50, 100, 200, 30);
        add(l2);

        Label l3 = new Label("Salary: " + sal);
        l3.setBounds(50, 140, 200, 30);
        add(l3);

        setSize(300, 250);
        setLayout(null);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });
    }
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top