Sekarang, kita akan membahas Java Program Netbeans menggunakan JFrame ( From ) Sederhana dengan 1 Class,
Listing Program
package tugasjava;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class NewMain extends JFrame implements ActionListener, ItemListener{
private JLabel lblTanggal,lblNama,lblAlamat,lblTelp,lblNo,lblKeterangan,lblSatuan,lblHarga,lblTotal,lblNo1,lblNo2,lblNo3,lblNo4,lblKg1,lblKg2,lblKg3,lblKg4,lblJumlah,lblUangmuka,lblSisa;
private JTextField txtNama,txtAlamat,txtTelp,txtSat1,txtSat2,txtSat3,txtSat4,txtHarga1,txtHarga2,txtHarga3,txtHarga4,txtTotal1,txtTotal2,txtTotal3,txtTotal4,txtJml,txtUangmuka,txtSisa;
private JComboBox cmbTanggal,cmbBulan,cmbTahun,cmbKet1,cmbKet2,cmbKet3,cmbKet4;
private JCheckBox cbxTotal1,cbxTotal2,cbxTotal3,cbxTotal4;
private JButton btnTotal, btnSisa, btnReset;
public NewMain (){
setTitle("FORM PEMBAYARAN LOUNDRY");
setSize(700,550);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
lblTanggal = new JLabel("Tanggal");
lblNama = new JLabel("Nama");
lblAlamat = new JLabel("Alamat");
lblTelp = new JLabel("Telp.");
lblNo = new JLabel("NO");
lblNo1 = new JLabel("1");
lblNo2 = new JLabel("2");
lblNo3 = new JLabel("3");
lblNo4 = new JLabel("4");
lblKg1 = new JLabel(" Kg");
lblKg2 = new JLabel(" Kg");
lblKg3 = new JLabel(" Kg");
lblKg4 = new JLabel(" Kg");
lblKeterangan = new JLabel("Keterangan");
lblSatuan = new JLabel("Satuan");
lblHarga = new JLabel("Harga / Kg");
lblTotal = new JLabel("Total");
lblJumlah = new JLabel("Jumlah");
lblUangmuka = new JLabel("Uang Muka");
lblSisa = new JLabel("Sisa Bayar");
String [] tanggal ={"","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
String [] bulan ={"","01","02","03","04","05","06","07","08","09","10","11","12"};
String [] tahun ={"","2011","2012","2013","2014","2015","2016","2017","2018","2009","2020"};
String[] ket1 = {"","Komplit (cuci + setrika)","Cuci+Kering","Setrika","Cepat komplit (6 jam selesai)"};
String[] ket2 = {"","Komplit (cuci + setrika)","Cuci+Kering","Setrika","Cepat komplit (6 jam selesai)"};
String[] ket3 = {"","Komplit (cuci + setrika)","Cuci+Kering","Setrika","Cepat komplit (6 jam selesai)"};
String[] ket4 = {"","Komplit (cuci + setrika)","Cuci+Kering","Setrika","Cepat komplit (6 jam selesai)"};
cmbTanggal = new JComboBox(tanggal);
cmbBulan = new JComboBox(bulan);
cmbTahun = new JComboBox(tahun);
cmbKet1 = new JComboBox(ket1);
cmbKet2 = new JComboBox(ket2);
cmbKet3 = new JComboBox(ket3);
cmbKet4 = new JComboBox(ket4);
txtNama = new JTextField("");
txtAlamat = new JTextField("");
txtTelp = new JTextField("");
txtSat1 = new JTextField("");
txtSat2 = new JTextField("");
txtSat3 = new JTextField("");
txtSat4 = new JTextField("");
txtHarga1 = new JTextField("");
txtHarga1.setEditable(false);
txtHarga2 = new JTextField("");
txtHarga2.setEditable(false);
txtHarga3 = new JTextField("");
txtHarga3.setEditable(false);
txtHarga4 = new JTextField("");
txtHarga4.setEditable(false);
txtTotal1 = new JTextField("0");
txtTotal1.setEditable(false);
txtTotal2 = new JTextField("0");
txtTotal2.setEditable(false);
txtTotal3 = new JTextField("0");
txtTotal3.setEditable(false);
txtTotal4 = new JTextField("0");
txtTotal4.setEditable(false);
txtJml = new JTextField("");
txtJml.setEditable(false);
txtUangmuka = new JTextField("");
txtSisa = new JTextField("");
txtSisa.setEditable(false);
cbxTotal1 = new JCheckBox();
cbxTotal2 = new JCheckBox();
cbxTotal3 = new JCheckBox();
cbxTotal4 = new JCheckBox();
btnTotal = new JButton("TOTAL");
btnSisa = new JButton("SISA BAYAR");
btnReset = new JButton("RESET");
add(lblTanggal);
add(lblNama);
add(lblAlamat);
add(lblTelp);
add(lblNo);
add(lblNo1);
add(lblNo2);
add(lblNo3);
add(lblNo4);
add(lblKeterangan);
add(lblSatuan);
add(lblHarga);
add(lblTotal);
add(lblJumlah);
add(lblUangmuka);
add(lblSisa);
add(lblKg1);
add(lblKg2);
add(lblKg3);
add(lblKg4);
add(txtNama);
add(txtAlamat);
add(txtTelp);
add(txtSat1);
add(txtSat2);
add(txtSat3);
add(txtSat4);
add(txtHarga1);
add(txtHarga2);
add(txtHarga3);
add(txtHarga4);
add(txtTotal1);
add(txtTotal2);
add(txtTotal3);
add(txtTotal4);
add(txtJml);
add(txtUangmuka);
add(txtSisa);
add(cmbTanggal);
add(cmbBulan);
add(cmbTahun);
add(cmbKet1);
add(cmbKet2);
add(cmbKet3);
add(cmbKet4);
add(cbxTotal1);
add(cbxTotal2);
add(cbxTotal3);
add(cbxTotal4);
add(btnTotal);
add(btnSisa);
add(btnReset);
lblTanggal.setBounds(30,40,150,25);
lblNama.setBounds(30,70,150,25);
lblAlamat.setBounds(30,100,150,25);
lblTelp.setBounds(30,130,150,25);
lblNo.setBounds(30,180,150,25);
lblNo1.setBounds(32,210,580,25);
lblNo2.setBounds(32,240,50,25);
lblNo3.setBounds(32,270,50,25);
lblNo4.setBounds(32,300,50,25);
lblKeterangan.setBounds(120,180,150,25);
lblSatuan.setBounds(290,180,150,25);
lblHarga.setBounds(380,180,150,25);
lblTotal.setBounds(520,180,150,25);
lblJumlah.setBounds(390,350,100,25);
lblUangmuka.setBounds(390,380,100,25);
lblSisa.setBounds(390,410,100,25);
lblKg1.setBounds(325,210,40,25);
lblKg2.setBounds(325,240,40,25);
lblKg3.setBounds(325,270,40,25);
lblKg4.setBounds(325,300,40,25);
cmbTanggal.setBounds(100,40,40,20);
cmbBulan.setBounds(150,40,40,20);
cmbTahun.setBounds(200,40,55,20);
cmbKet1.setBounds(60,210,200,25);
cmbKet2.setBounds(60,240,200,25);
cmbKet3.setBounds(60,270,200,25);
cmbKet4.setBounds(60,300,200,25);
txtNama.setBounds(100,70,200,25);
txtAlamat.setBounds(100,100,200,25);
txtTelp.setBounds(100,130,200,25);
txtSat1.setBounds(280,210,40,25);
txtSat2.setBounds(280,240,40,25);
txtSat3.setBounds(280,270,40,25);
txtSat4.setBounds(280,300,40,25);
txtHarga1.setBounds(360,210,100,25);
txtHarga2.setBounds(360,240,100,25);
txtHarga3.setBounds(360,270,100,25);
txtHarga4.setBounds(360,300,100,25);
txtTotal1.setBounds(485,210,100,25);
txtTotal2.setBounds(485,240,100,25);
txtTotal3.setBounds(485,270,100,25);
txtTotal4.setBounds(485,300,100,25);
txtJml.setBounds(485,350,100,25);
txtUangmuka.setBounds(485,380,100,25);
txtSisa.setBounds(485,410,100,25);
cbxTotal1.setBounds(600,210,100,25);
cbxTotal2.setBounds(600,240,100,25);
cbxTotal3.setBounds(600,270,100,25);
cbxTotal4.setBounds(600,300,100,25);
btnTotal.setBounds(270,450,100,25);
btnSisa.setBounds(380,450,110,25);
btnReset.setBounds(530,450,100,25);
cmbKet1.addItemListener(this);
cmbKet2.addItemListener(this);
cmbKet3.addItemListener(this);
cmbKet4.addItemListener(this);
cbxTotal1.addItemListener(this);
cbxTotal2.addItemListener(this);
cbxTotal3.addItemListener(this);
cbxTotal4.addItemListener(this);
btnTotal.addActionListener(this);
btnSisa.addActionListener(this);
btnReset.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object tombol = e.getSource();
if(tombol==btnTotal){
int n1=Integer.parseInt(txtTotal1.getText());
int n2=Integer.parseInt(txtTotal2.getText());
int n3=Integer.parseInt(txtTotal3.getText());
int n4=Integer.parseInt(txtTotal4.getText());
int hasil=n1+n2+n3+n4;
txtJml.setText(""+hasil);
}else if(tombol==btnSisa){
int n3=Integer.parseInt(txtJml.getText());
int n4=Integer.parseInt(txtUangmuka.getText());
int akhir=n3-n4;
txtSisa.setText(""+akhir);
}else if (tombol==btnReset){
txtNama.setText("");
txtAlamat.setText("");
txtTelp.setText("");
txtSat1.setText("");
txtSat2.setText("");
txtSat3.setText("");
txtSat4.setText("");
txtHarga1.setText("");
txtHarga2.setText("");
txtHarga3.setText("");
txtHarga4.setText("");
txtTotal1.setText("0");
txtTotal2.setText("0");
txtTotal3.setText("0");
txtTotal4.setText("0");
txtJml.setText("");
txtUangmuka.setText("");
txtSisa.setText("");
cmbTanggal.setSelectedIndex(0);
cmbBulan.setSelectedIndex(0);
cmbTahun.setSelectedIndex(0);
cmbKet1.setSelectedIndex(0);
cmbKet2.setSelectedIndex(0);
cmbKet3.setSelectedIndex(0);
cmbKet4.setSelectedIndex(0);
cbxTotal1.isSelected();
}
}
public void itemStateChanged(ItemEvent ie){
Object pil = ie.getSource();
Object tot = ie.getSource();
if(tot == cbxTotal1){
if(cbxTotal1.isSelected()){
int t1=Integer.parseInt(txtSat1.getText());
int t2=Integer.parseInt(txtHarga1.getText());
int total=t1*t2;
txtTotal1.setText(""+total);
}
}
if(tot == cbxTotal2){
if(cbxTotal2.isSelected()){
int t1=Integer.parseInt(txtSat2.getText());
int t2=Integer.parseInt(txtHarga2.getText());
int total=t1*t2;
txtTotal2.setText(""+total);
}
}
if(tot == cbxTotal3){
if(cbxTotal3.isSelected()){
int t1=Integer.parseInt(txtSat3.getText());
int t2=Integer.parseInt(txtHarga3.getText());
int total=t1*t2;
txtTotal3.setText(""+total);
}
}
if(tot == cbxTotal4){
if(cbxTotal4.isSelected()){
int t1=Integer.parseInt(txtSat4.getText());
int t2=Integer.parseInt(txtHarga4.getText());
int total=t1*t2;
txtTotal4.setText(""+total);
}
}
if(pil == cmbKet1){
if(cmbKet1.getSelectedIndex()==0){
txtHarga1.setText("");
}else if(cmbKet1.getSelectedIndex()==1){
txtHarga1.setText("3000");
}else if(cmbKet1.getSelectedIndex()==2){
txtHarga1.setText("2500");
}else if(cmbKet1.getSelectedIndex()==3){
txtHarga1.setText("2000");
}else if(cmbKet1.getSelectedIndex()==4){
txtHarga1.setText("6000");
}
}
if(pil == cmbKet2){
if(cmbKet2.getSelectedIndex()==0){
txtHarga2.setText("");
}else if(cmbKet2.getSelectedIndex()==1){
txtHarga2.setText("3000");
}else if(cmbKet2.getSelectedIndex()==2){
txtHarga2.setText("2500");
}else if(cmbKet2.getSelectedIndex()==3){
txtHarga2.setText("2000");
}else if(cmbKet2.getSelectedIndex()==4){
txtHarga2.setText("6000");
}
}
if(pil == cmbKet3){
if(cmbKet3.getSelectedIndex()==0){
txtHarga3.setText("");
}else if(cmbKet3.getSelectedIndex()==1){
txtHarga3.setText("3000");
}else if(cmbKet3.getSelectedIndex()==2){
txtHarga3.setText("2500");
}else if(cmbKet3.getSelectedIndex()==3){
txtHarga3.setText("2000");
}else if(cmbKet3.getSelectedIndex()==4){
txtHarga3.setText("6000");
}
}
if(pil == cmbKet4){
if(cmbKet4.getSelectedIndex()==0){
txtHarga4.setText("");
}else if(cmbKet4.getSelectedIndex()==1){
txtHarga4.setText("3000");
}else if(cmbKet4.getSelectedIndex()==2){
txtHarga4.setText("2500");
}else if(cmbKet4.getSelectedIndex()==3){
txtHarga4.setText("2000");
}else if(cmbKet4.getSelectedIndex()==4){
txtHarga4.setText("6000");
}
}
}public static void main (String args[]){
NewMain lndry = new NewMain ();
lndry.setVisible(true);
}
}
Nih Lihat Output Running-nya.
SELAMAT MENCOBA...!!