Slip No 12 Q B

Q.Write a java program to display multiplication table of a given number into the List box by clicking on button. [25 M]

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

public class TableListBox extends Frame implements ActionListener {

    TextField t1;
    Button b1;
    List list;

    TableListBox() {

        // Label
        Label l1 = new Label("Enter Number:");
        l1.setBounds(50, 50, 100, 30);
        add(l1);

        // TextField
        t1 = new TextField();
        t1.setBounds(160, 50, 100, 30);
        add(t1);

        // Button
        b1 = new Button("Show Table");
        b1.setBounds(120, 100, 100, 30);
        add(b1);

        // List Box
        list = new List();
        list.setBounds(100, 150, 150, 150);
        add(list);

        // Register event
        b1.addActionListener(this);

        setSize(350, 350);
        setLayout(null);
        setTitle("Multiplication Table");
        setVisible(true);

        // Close window
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                dispose();
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        list.removeAll();

        int num = Integer.parseInt(t1.getText());

        for(int i = 1; i <= 10; i++) {
            list.add(num + " x " + i + " = " + (num * i));
        }
    }

    public static void main(String[] args) {
        new TableListBox();
    }
}
Spread the love

Leave a Comment

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

Scroll to Top