Kamis, 17 Februari 2011

Source code Java

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

class Prob extends JFrame implements ActionListener,MouseListener{
double hasil_per, hasil_kom;
Container con = new Container();
JButton hapus , ok ;
JTextField n, r, per, kom ;
JLabel pe, ko, nn, rr;

double fakt(double angka) {
if (angka == 0)
return 1;
else
return angka * fakt(angka - 1);
}

public Prob() {
super("Program Permutasi & Kombinasi");
setSize(400,300);
ok = new JButton("hitung");
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);
n = new JTextField("");
r = new JTextField("");
r.addMouseListener(this);
per = new JTextField("hasil permutasi");
kom = new JTextField("hasil kombinasi");
per.setEditable(false);
kom.setEditable(false);
nn = new JLabel("Nilai n :");
rr = new JLabel("Nilai r :");
pe = new JLabel("P(n,r) :");
ko = new JLabel("C(n,r) :");
JPanel text = new JPanel();
text.setLayout(new GridLayout(4,2,10,10));
text.add(nn); text.add(n);
text.add(rr); text.add(r);
text.add(pe); text.add(per);
text.add(ko); text.add(kom);
con = getContentPane();
con.setLayout(null);
text.setBounds(100,30,200,150);
tombol.setBounds(100,200,200,30);
con.add(tombol);
con.add(text);
show();
}

public void actionPerformed(ActionEvent e) {
try {
if(e.getSource() == ok) {
hapus.setEnabled(true);
Double nilai_n = Double.parseDouble(n.getText());
Double nilai_r = Double.parseDouble(r.getText());
hasil_per = fakt(nilai_n)/fakt(nilai_n-nilai_r);
hasil_kom = fakt(nilai_n)/(fakt(nilai_n-nilai_r)*fakt(nilai_r));
per.setText(Double.toString(hasil_per));
kom.setText(Double.toString(hasil_kom));
}
if(e.getSource() == hapus) {
n.setText("");
r.setText("");
per.setText("");
kom.setText("");
hapus.setEnabled(false);
}
}
catch(Exception ex) {
hapus.setEnabled(false);
JOptionPane.showMessageDialog(this,"masukkan harus numerik!!");
}

}
public void mouseClicked(MouseEvent ec) {
JOptionPane.showMessageDialog(this,"nilai n harus lebih besar dari r!");
}
public void mouseExited(MouseEvent ec) {
}
public void mouseEntered(MouseEvent ec){
}
public void mousePressed(MouseEvent ec){
}
public void mouseReleased(MouseEvent ec){
}
public static void main(String[] test) {
Prob q = new Prob();
q.setResizable(false);
q.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}s

0 komentar:

Posting Komentar