Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  "factorial.java"
Language: JAVA
Code:
import javax.swing.*;
import java.awt.event.*; 
import java.awt.*;
public class factorial extends JFrame
 {
      private JTextField txtNum;
      private JLabel lblNum, lblRes,lblJake;
       private JButton btnSolve, btnAbout, btnClear;
       public static int ComputeFactorial(int number)
       {
           int n = number-1;
           do
           {
                number = number*n;
                n--;
           }
          while(n>=1);
                return number;
      } 
      public factorial()
      {
      super("Factorial GUI in Java "); 
      Container c = getContentPane(); 
      c.setLayout(new FlowLayout()); 
      lblNum = new JLabel("Enter an integer: ");
      txtNum = new JTextField(10);
      lblRes = new JLabel();
      lblJake= new JLabel();
      btnSolve = new JButton("SOLVE");
      btnAbout = new JButton("ABOUT");
      btnClear = new JButton("CLEAR");
      btnSolve.addActionListener(new ActionListener()
      {
           public void actionPerformed(ActionEvent e)
           {
           String str = txtNum.getText();
           int tmp = Integer.parseInt(str);
           tmp = ComputeFactorial(tmp);
           lblRes.setText("The factorial of "+str+" is "+tmp);
           }
      }
      );
 
      btnAbout.addActionListener(new ActionListener()
      {
           public void actionPerformed(ActionEvent e)
           {
                lblJake.setText("Created By: Jake Rodriguez Pomperada, MAED-IT");
           }
      }
      );
      btnClear.addActionListener(new ActionListener()
      {
           public void actionPerformed(ActionEvent e)
           {
                lblNum.setText("");
                lblRes.setText("");
                lblJake.setText("");
                txtNum.setText("");
           }
      }
      );
      c.add(lblNum);
      c.add(txtNum);
      c.add(btnSolve); 
      c.add(btnAbout);
      c.add(btnClear);
      c.add(lblRes);
      c.add(lblJake);
      setSize(210,230);
     show();
 }
 public static void main(String args[])
 {
      factorial app = new factorial();
      app.setResizable(true);
      app.setLocation(400,200);
      app.addWindowListener(new WindowAdapter()
      {
           public void windowClosing(WindowEvent e)
           {
                System.exit(0);
           }
      }
     );
 }
}     
Comments: