RSS

Program Faktorial dengan GUI

26 Nov

LISTING PROGRAM:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class GUIfak extends JFrame implements ActionListener
{
double hasil;
Container con = new Container();
JButton hapus, ok;
JTextField tex1, tex2;
double fakt(double angka)
{
if(angka == 0)
{
return 1;
}
else
{
return angka*fakt(angka-1);
}
}
public GUIfak()
{
super("PROGRAM FAKTORIAL");
setSize (300,250);
ok = new JButton ("Faktorial");
hapus = new JButton ("Hapus");
ok.addActionListener (this);
hapus.addActionListener (this);
JPanel tombol = new JPanel ();
hapus.setEnabled (false);
tombol.setLayout (new GridLayout(1,2,10,10));
tombol.add (hapus);
tombol.add(ok);
tex1 = new JTextField("");
tex2 = new JTextField("");
tex2.setEditable (false);
JPanel tex = new JPanel();
tex.setLayout(new GridLayout (2,1,10,10));
tex.add(tex1);
tex.add(tex2);
con = getContentPane();
con.setLayout(null);
tex.setBounds(100,80,100,50);
tombol.setBounds(50,150,200,30);
con.add(tombol);
con.add(tex);
show();
}

public void actionPerformed (ActionEvent e)
{
try
{
if (e.getSource()==ok)
{
hapus.setEnabled(true);
double angka = Double.parseDouble(tex1.getText());
hasil = fakt(angka);
}
tex2.setText(Double.toString(hasil));
if (e.getSource() == hapus)
{
tex1.setText("");
tex2.setText("");
hapus.setEnabled(false);
}
}
catch(Exception ex)
{
hapus.setEnabled(false);
JOptionPane.showMessageDialog(this, "Masukkan nilai yang benar..!!");
}
}

public static void main(String[] args)
{
GUIfak q = new GUIfak();
q.setResizable(false);
q.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

OUTPUT PROGRAM:

Masukkan nilai yang ingin difaktorialkan

 

Hasil perhitungan faktorial


 
2 Comments

Posted by on 26 November 2010 in Program-Program Java

 

2 responses to “Program Faktorial dengan GUI

  1. Sokhi

    10 January 2011 at 11:28 am

    Program yang menarik, dan cukup bagus…….Think’s

     
  2. damaisubimawanto

    15 January 2011 at 11:51 pm

    Iya, terima kasih, sokhi..
    Masih banyak yg harus diperbaiki programnya, msh bnyak kekurangannya..

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: