Fatorial integrado em Frame: socorro!

3 respostas
J

Olá, estou a tentar criar uma caixa de diálogo com a mensagem do número de cliques e o fatorial do numero de cliques efectuados. Mas o resultado final não aparece com o factorial?

Porquê? Alguém me pode ajudar?

Muito obrigada!

Código:

import javax.swing.<em>;

import java.awt.</em>;

import java.awt.event.*;

class Factorial
{

public static void main (String [] args)
{

int fat = 1;
int valor = numClicks;

for( int i = 2; i <= valor; i++ )

{

fat *= i;   }

}

}

class Frame3 extends JFrame
{

public static String labelPrefix = "Number of clicks: N =";
public int numClicks = 0; 
	JPanel contentPane=new JPanel();
	JLabel jLabel1 = new JLabel(labelPrefix + "0 ");
	JButton jButton1=new JButton ();


/*Contruction of a frame*/
public Frame3() 
{
/*Text Label*/
	jLabel1.setText("Press this button.");
	jLabel1.setBounds(14, 39, 400, 35);
	contentPane = (JPanel)this.getContentPane();
	contentPane.setLayout(null);
	this.setSize(500,150);
	this.setTitle("exe3: button exercise");

/* Setting a button */
	jButton1.setText ("Ok");
	jButton1.setBounds(38, 83, 96, 41);
	
	jButton1.addActionListener(new ActionListener()
	{
	public void actionPerformed(ActionEvent e) 
	{
		numClicks++;
		jLabel1.setText(labelPrefix + numClicks + "    Factorial of N: N! = " + fat);
		}
	});
		/* End of action listener */
	contentPane.add(jButton1, null);
	contentPane.add(jLabel1, null);
}

}

public class AppFrame33e

{

public static void main (String[]args)

{

Frame3 frame = new Frame3();

//Quit to shell by clicking “x” button of window

frame.setDefaultCloseOperation(Frame3.EXIT_ON_CLOSE);

frame.setVisible(true);

3 Respostas

pimenta

Joana, coloca seu código entre as tags e .

pimenta

Desculpa, Joana, mas onde está a ligação da sua classe Fatorial com a sua GUI.
Nesse caso seria melhor, ao invés de criar uma classe, você criar um método fatorial.

J

A ligação está aqui:

int fat = 1;
int valor = numClicks;

em que a variável valor assim o número de cliques.

Como posso criar o método? Não é mais complicado? Acho que não sei fazer… : (

Criado 1 de julho de 2008
Ultima resposta 1 de jul. de 2008
Respostas 3
Participantes 2