import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class faktorial extends JFrame implements ActionListener
{
Container con = new Container();
JButton hapus,ok;
JTextField tex1,tex2;
JLabel input,hasil;
public faktorial(String Judul)
{
super(Judul);
setSize(290,200);
ok = new JButton ("proses");
hapus = new JButton ("hapus");
input=new JLabel("Nilai Input");
hasil=new JLabel("faktorialtorialnya");
ok.addActionListener(this);
hapus.addActionListener(this);
JPanel tombol = new JPanel();
hapus.setEnabled (false);
tombol.setLayout (new GridLayout(2,1,10,10));
tombol.add(ok);
tombol.add(hapus);
tex1 = new JTextField("");
tex2 = new JTextField("");
tex2.setEditable(false);
JPanel tex = new JPanel();
tex.setLayout(new GridLayout(4,1,0,0));
tex.add(input);
tex.add(tex1);
tex.add(hasil);
tex.add(tex2);
con = getContentPane();
con.setLayout(null);
tex.setBounds(20,20,100,100);
tombol.setBounds(150,50,100,60);
con.add(tombol);
con.add(tex);
show();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==ok)
{
hapus.setEnabled(true);
String a=tex1.getText();
int b=Integer.parseInt (a);
long faktorial=1;// rumus faktorialtorial
for(int i=1;i<=b;i++){
faktorial=faktorial*i;
}
tex2.setText(String.valueOf( faktorial));
}
if (e.getSource()==hapus)
{
tex1.setText("");
tex2.setText("");
}
}
public static void main(String args[])
{
new faktorial("faktorialtorial");
}
}
Tidak ada komentar:
Posting Komentar