问题描述:下面是我使用Java编程所实现的图形化的能求输入的任意数的阶乘。
程序源码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Factor implements ActionListener
{
JTextArea jta=new JTextArea(1,15);
JFrame frame=new JFrame("求阶乘");
Factor()
{
JButton OK=new JButton("Ok");
OK.addActionListener(this);
JButton Cancel=new JButton("Cancel");
Cancel.addActionListener(this);
JButton Clear=new JButton("Clear");
Clear.addActionListener(this);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(jta);
frame.getContentPane().add(OK);
frame.getContentPane().add(Cancel);
frame.getContentPane().add(Clear);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
frame.setSize(300,250);
frame.setLocation(400,200);
}
public static void main(String[] args)
{
Factor fac=new Factor();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="Ok");
{
calcu();
}
if(e.getActionCommand()=="Clear")
{
jta.setText(" ");
}
if(e.getActionCommand()=="Cancel")
{
System.exit(0);
}
}
void calcu()
{
Integer a;
a=Integer.valueOf(jta.getText()).intValue();
for(int i=a-1; i>=1;i--)
{
a=a*i;
}
jta.setText(a.toString());
}
}