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);