Factorial en java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CopyOfFactorialButton extends JFrame
{
private staticfinal int WIDTH = 300;
private static final int HEIGTH = 100;
private JTextField xBox;
private JTextField xfBox;
public CopyOfFactorialButton()
{
setTitle("factorial Calculator");setSize(WIDTH, HEIGTH);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContens();
setVisible(true);
}
private void createContens()
{
JLabel xLabel = newJLabel("x:");
JLabel xfLabel = new JLabel("x!:");
JButton btn = new JButton("Factorial");
Listener listener = new Listener ();
xBox = new JTextField(2);
xfBox= new JTextField(10);
xfBox.setEditable(false);
add(xLabel);
add(xBox);
add(xfLabel);
add(xfBox);
add(btn);
xBox.addActionListener(listener);btn.addActionListener(listener);
}
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int x;
int xf;try
{
x = Integer.parseInt(xBox.getText());
}
catch (NumberFormatException nfe)
{
x = -1;
}if (x < 0)
{
xfBox.setText("undefined");
}
else
{
if ( x == 0 || x == 1){
xf = 1;
}else
{
xf=1;
for (int i=2; i<=x; i++)
{
xf *= i;
}}
xfBox.setText(Integer.toString(xf));
}
}
}
//****************************************************************************
public...
Regístrate para leer el documento completo.