Berikut ini adalah coding program Faktorial menggunakan Java GUI. Package yang digunakan untuk menggunakan pada GUI yaitu: import javax.swing.*; , import java.awt.event.*; , import java.awt.*; . Simpan dengan nama file "Faktorial.java"
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;
public Faktorial(String judul)
{
super(judul);
setSize(300,200);
ok=new JButton("PROSES");
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);
JPanel tex=new JPanel();
tex1=new JTextField("");
tex2=new JTextField("");
tex2.setEditable(false);
tex.setLayout(new GridLayout(2,1,10,10));
tex.add(tex1);
tex.add(tex2);
con=getContentPane();
con.setLayout(null);
tex.setBounds(50,40,200,50);
tombol.setBounds(50,110,200,30);
con.add(tombol);
con.add(tex);
show();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ok){
hapus.setEnabled(true);
String a=tex1.getText();
int fak=1;
int f=Integer.parseInt(a);
for(int i=1;i<=f;i++)
fak *=i;
tex2.setText(Integer.toString(fak));
}
if(e.getSource()==hapus){
tex1.setText("");
tex2.setText("");
}
}
public static void main(String args[]){
new Faktorial("FAKTORIAL");
}
}
import java.awt.event.*;
import java.awt.*;
class Faktorial extends JFrame implements ActionListener
{
Container con=new Container();
JButton hapus,ok;
JTextField tex1,tex2;
public Faktorial(String judul)
{
super(judul);
setSize(300,200);
ok=new JButton("PROSES");
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);
JPanel tex=new JPanel();
tex1=new JTextField("");
tex2=new JTextField("");
tex2.setEditable(false);
tex.setLayout(new GridLayout(2,1,10,10));
tex.add(tex1);
tex.add(tex2);
con=getContentPane();
con.setLayout(null);
tex.setBounds(50,40,200,50);
tombol.setBounds(50,110,200,30);
con.add(tombol);
con.add(tex);
show();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ok){
hapus.setEnabled(true);
String a=tex1.getText();
int fak=1;
int f=Integer.parseInt(a);
for(int i=1;i<=f;i++)
fak *=i;
tex2.setText(Integer.toString(fak));
}
if(e.getSource()==hapus){
tex1.setText("");
tex2.setText("");
}
}
public static void main(String args[]){
new Faktorial("FAKTORIAL");
}
}
TAMPILANNYA:
1 komentar:
makasih gan sangat membantu Tugas saya hehe
Post a Comment