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

public class factorialSwing {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Factorial Calculator");
        frame.setSize(300, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel label1 = new JLabel("Enter a number:");
        JLabel label2 = new JLabel("Result:");

        JTextField textField1 = new JTextField(5);
        JTextField textField2 = new JTextField(10);
        textField2.setEditable(false);

        JButton button = new JButton("Compute");

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    int num = Integer.parseInt(textField1.getText());
                    long factorial = 1;

                    if (num < 0) {
                        textField2.setText("Wrong input");
                    } else {
                        for (int i = num; i > 1; i--) {
                            factorial *= i;
                        }
                        textField2.setText(String.valueOf(factorial));
                    }
                } catch (NumberFormatException ex) {
                    textField2.setText("Invalid input");
                }
            }
        });

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(label1);
        panel.add(textField1);
        panel.add(button);
        panel.add(label2);
        panel.add(textField2);

        frame.add(panel);
        frame.setVisible(true);
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: