Pertama Kita buat Javaclass dengan nama "FormBarang"
package javaform;
public class FormBarang {
public String kd,nm;
public int jml;
public double hrg,ttl,ub,uk;
public void setKd(String val)
{ kd=val; }
public String getKd()
{ return kd; }
public void setSeleksiKd()
{ if (kd.equalsIgnoreCase("B001"))
{ nm="PENSIL";
hrg=3000; }
else if (kd.equalsIgnoreCase("B002"))
{ nm="BUKU";
hrg=5000; }
else if (kd.equalsIgnoreCase("B003"))
{ nm="PENGGARIS";
hrg=1000; }
else if (kd.equalsIgnoreCase("B004"))
{ nm="PENGHAPUS";
hrg=500; }
else
{ nm="";
hrg=0;}
}
public void setNm(String val)
{ nm = val; }
public String getNm()
{ return nm; }
public void setHrg(Double val)
{ hrg = val; }
public double getHrg()
{ return hrg; }
public void setJml(int val)
{ jml=val; }
public int getJml()
{ return jml; }
public void setTot(Double val)
{ ttl=val; }
public double getTot()
{ ttl=hrg*jml;
return(ttl); }
public void setUb(Double val)
{ ub=val; }
public double getUb()
{ return ub; }
public double getUk()
{ uk=ub-ttl;
return uk; }
}
Kedua, buat JFrame dengan nama "FormBarang1"
package javaform;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
public class FormBarang1 extends javax.swing.JFrame {
FormBarang brg = new FormBarang();
public FormBarang1() {
initComponents();
bersih();
nonaktif();
setLocationRelativeTo(this); //untuk tampil ditengah layar
}
private void bbersihActionPerformed(java.awt.event.ActionEvent evt) {
aktif();
bersih();
ckode.requestFocus();
}
private void bkeluarActionPerformed(java.awt.event.ActionEvent evt) {
int x;
x=JOptionPane.showConfirmDialog(null,"Yakin Mau Keluar?","Konfirmasi",JOptionPane.YES_NO_OPTION);
if (x==JOptionPane.YES_OPTION)
dispose();
else
return;
}
private void tjumlahKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode()==KeyEvent.VK_ENTER)
{ brg.setJml(Integer.parseInt(tjumlah.getText()));
ttotal.setText(Double.toString(brg.getTot()));
tubay.grabFocus();
}
}
private void tubayKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode()==KeyEvent.VK_ENTER)
{ brg.setUb(Double.parseDouble(tubay.getText()));
tukem.setText(Double.toString(brg.getUk()));
if(brg.getUb()
tukem.setText("");
tubay.setText("");}
else { return; }
}
}
private void ckodeActionPerformed(java.awt.event.ActionEvent evt) {
brg.setKd(ckode.getSelectedItem().toString());
brg.setSeleksiKd();
tnama.setText((brg.getNm()));
tharga.setText(Double.toString(brg.getHrg()));
tjumlah.requestFocus();
}
void bersih()
{ ckode.setSelectedItem("--Pilih--");
tnama.setText("");
tharga.setText("");
tjumlah.setText("");
ttotal.setText("");
tubay.setText("");
tukem.setText(""); }
void aktif()
{ ckode.setEnabled(true);
tjumlah.setEnabled(true);
tubay.setEnabled(true); }
void nonaktif()
{ ckode.setEnabled(false);
tnama.setEnabled(false);
tharga.setEnabled(false);
tjumlah.setEnabled(false);
ttotal.setEnabled(false);
tubay.setEnabled(false);
tukem.setEnabled(false); }
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FormBarang1().setVisible(true);
}
});
}}
Untuk Running, silahkan tekan Ctrl+F6 pada JFrame "FormBarang1"
Tidak ada komentar:
Posting Komentar