hola compañeros me trabe en este programa es sobre un factorial lo que nesecito es que un jtexfiel me mande el resultado de del factorial a un jlebel desde un boton este es mi codigo
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
public class FactorialH extends JFrame implements ActionListener {
public static void main(String[] args) {
FactorialH programa = new FactorialH();
programa.setVisible(true);
}
private JLabel resultado;
private JButton calcular;
private JTextField numero;
public FactorialH() {
super("Factorial");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new FlowLayout());
setSize(400, 100);
setVisible(true);
resultado = new JLabel();
calcular = new JButton("Calcular");
numero = new JTextField(10);
add(numero);
add(resultado);
add(calcular);
calcular.addActionListener(this);
}
private int factorial(int n) {
if (n < 2) {
return 1;
}
else {
return n * factorial(n - 1);
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == calcular) {
try {
int miNumero = 0;
miNumero = Integer.parseInt(numero.getText());
resultado.setText(String.valueOf(numero));
File archivo = new File("C:/Users/cedo/Desktop/archivo.txt");
try (FileWriter grabar = new FileWriter(archivo)) {
BufferedWriter escribir = new BufferedWriter(grabar);
escribir.write(resultado.getText());
escribir.flush();
escribir.close();
}
} catch (Exception e) {
numero.setText("");
resultado.setText("");
e.printStackTrace();
}
}
}
}
el problema cuando lo ejcuto no me sale nada ayudenme porfavor se los agradeceria
Buenas!!
Te falta calcular el factorial y algunas cosillas más :P
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
public class FactorialH extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel resultado;
private JButton calcular;
private JTextField numero;
public static void main(String[] args) {
FactorialH programa = new FactorialH();
programa.setVisible(true);
}
public FactorialH() {
super("Factorial");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new FlowLayout());
setSize(400, 100);
setVisible(true);
resultado = new JLabel();
calcular = new JButton("Calcular");
numero = new JTextField(10);
add(numero);
add(resultado);
add(calcular);
calcular.addActionListener(this);
}
private int factorial(int resul) {
if (resul < 2) {
return 1;
} else {
return resul * factorial(resul - 1);
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == calcular) {
try {
int resul = Integer.parseInt(numero.getText().toString());
resul = factorial(resul);
resultado.setText(String.valueOf(resul));
File archivo = new File("archivo.txt");
FileWriter grabar;
BufferedWriter escribir = null;
try {
grabar = new FileWriter(archivo, true);
escribir = new BufferedWriter(grabar);
escribir.write(numero.getText().toString() + " --> " + resultado.getText().toString());
escribir.newLine();
} catch (Exception e) {
numero.setText("");
resultado.setText("");
e.printStackTrace();
} finally {
escribir.flush();
escribir.close();
}
} catch(Exception e) {
numero.setText("");
resultado.setText("");
e.printStackTrace();
}
}
}
}
Si alguien ve algo que se pueda corregir/mejorar que lo diga que estoy aprendiendo!!!!
Saludos
Bueno, yo lo hice de esta manera y de manera recursiva (OJO: Me falto validar con los bloques Try Catch para atrapar las excepciones)
package ejercicio1207;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Ventana extends JFrame implements ActionListener {
private JTextField campoTexto;
private JLabel resultado;
private JButton boton;
public Ventana(){
super("Calculadora de factorial");
setLayout(new FlowLayout());
campoTexto = new JTextField("Escriba aqui el numero...");
resultado = new JLabel("El factorial es: ");
boton = new JButton("Calcular");
add(campoTexto);
add(resultado);
add(boton);
boton.addActionListener(this);
setSize(350,80);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
int numero = Integer.parseInt(campoTexto.getText());
resultado.setText("El factorial es: " + Integer.toString(factorial(numero)));
}
public int factorial(int numero){
if(numero == 1){
return 1;
}
return numero * factorial(numero-1);
}
}