Assalamu'alaikum pembaca
Sudah lama saya tidak menulis di blog ini dan sudah lama pula saya tidak membagi ilmu, emtah itu tentang komputer atau tentang dunia islam. Saat ini saya mau share coding pembuatan kalkulator dengan Java GUI. Walaupun coding ini saya bikin sendiri tapi agak ribet menjelaskan codingnya, jadi saya share codingnya aj ya.
Sebelum itu saya akan memperlihatkan outputnya terlebih dahulu, outputnya seperti gambar disamping. Seperti kita lihat kalkulator yang saya buat dengan java ini sudah dilengkapi dengan operasi trigonomerti, berbeda dengan kalkulator yang pernah saya buat dengan VB.
Sekarang kita cocokin dulu variable name dari JButton yang akan digunakan, berikut nama variable dari JButton tersebut:
- tombol 1 => b1
- tombol 2 => b2
- tombol 3 => b3
- tombol 4 => b4
- tombol 5 => b5
- tombol 5 => b6
- tombol 7 => b7
- tombol 8 => b8
- tombol 9 => b9
- tombol 0 => b0
- tombol . => desimal
- tombol C =>hapus
- tombol -/+ =>negative
- tombol + =>plus
- tombol - =>min
- tombol x => multy
- tombol / => div
- tombol ^ => pangkat
- tombol % => percent
- tombol 1/x => balik
- tombol ! => factorial
- tombol sin => sin
- tombol cos => cos
- tombol tan => tan
- tombol = => res
wow baru tombolnya aj dah panjang ni post, ok kalo dah sama berikut adalah source codenya
baris 1 sampai 10
package guicode1; //nama project
public class calc extends javax.swing.JFrame {
double operator,hasil,bil1;// variable global
/** Creates new form calc */
public calc() {
super("tyocalcjava");// optional
initComponents();
}
@SuppressWarnings("unchecked")
public class calc extends javax.swing.JFrame {
double operator,hasil,bil1;// variable global
/** Creates new form calc */
public calc() {
super("tyocalcjava");// optional
initComponents();
}
@SuppressWarnings("unchecked")
nah kalo pakai Netbeans baris selanjutnya yaitu baris 11 itu udah ada dari sananya dan baris itu di hide agar tidak dirubah, jadi saya tidak perlu menuliskannya disini jadi lanjut ke baris setelahnya aja ya
// dibawah ini untuk tombol angkanya
private void b1ActionPerformed(java.awt.event.ActionEvent
evt) {
txt.setText(txt.getText()+1);
}
private void
b2ActionPerformed(java.awt.event.ActionEvent evt) {
txt.setText(txt.getText()+2);
}
private void
b3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
txt.setText(txt.getText()+3);
}
private void
b4ActionPerformed(java.awt.event.ActionEvent evt) {
txt.setText(txt.getText()+4);
}
private void
b5ActionPerformed(java.awt.event.ActionEvent evt) {
txt.setText(txt.getText()+5);
}
private void
b6ActionPerformed(java.awt.event.ActionEvent evt) {
txt.setText(txt.getText()+6);
}
private void
b7ActionPerformed(java.awt.event.ActionEvent evt) {
txt.setText(txt.getText()+7);
}
private void
b8ActionPerformed(java.awt.event.ActionEvent evt) {
txt.setText(txt.getText()+8);
}
private void
b9ActionPerformed(java.awt.event.ActionEvent evt) {
txt.setText(txt.getText()+9);
}
private void b0ActionPerformed(java.awt.event.ActionEvent
evt) {
txt.setText(txt.getText()+0);
}
private void
desimalActionPerformed(java.awt.event.ActionEvent evt) {
txt.setText(txt.getText()+".");
}
//dibawah ini untuk operatornya
private void
plusActionPerformed(java.awt.event.ActionEvent evt) {
operator=1;
bil1=
Double.parseDouble(txt.getText());
txt.setText("");
}
private void minActionPerformed(java.awt.event.ActionEvent
evt) {
operator=2;
bil1=
Double.parseDouble(txt.getText());
txt.setText("");
}
private void
multyActionPerformed(java.awt.event.ActionEvent evt) {
operator=3;
bil1=
Double.parseDouble(txt.getText());
txt.setText("");
}
private void
divActionPerformed(java.awt.event.ActionEvent evt) {
operator=4;
bil1=
Double.parseDouble(txt.getText());
txt.setText("");
}
private void
resActionPerformed(java.awt.event.ActionEvent evt) {
if(operator==1){
hasil=
bil1+ Double.parseDouble(txt.getText());
txt.setText(String.valueOf(hasil));}
else if
(operator==2){
hasil=
bil1- Double.parseDouble(txt.getText());
txt.setText(String.valueOf(hasil));}
else if
(operator==3){
hasil=
bil1* Double.parseDouble(txt.getText());
txt.setText(String.valueOf(hasil));}
else if
(operator==4){
hasil=
bil1/ Double.parseDouble(txt.getText());
txt.setText(String.valueOf(hasil));}
else if
(operator==5){
if(hasil<0){
txt.setText("syntax error");
}
else{
hasil=
Math.pow(bil1, Double.parseDouble(txt.getText()));
txt.setText(String.valueOf(hasil));
}
}
}
//di bawah ini menghapus angka
private void
hapusActionPerformed(java.awt.event.ActionEvent evt) {
txt.setText("");
}
//di bawah ini operator tambahan
private void
percentActionPerformed(java.awt.event.ActionEvent evt) {
hasil=
Double.parseDouble(txt.getText())/100;
txt.setText(String.valueOf(hasil));
}
private void
pangkatActionPerformed(java.awt.event.ActionEvent evt) {
operator=5;
bil1=Double.parseDouble(txt.getText());
txt.setText("");
}
private void
balikActionPerformed(java.awt.event.ActionEvent evt) {
hasil =
1/Double.parseDouble(txt.getText());
txt.setText(String.valueOf(hasil));
}
private void
factorialActionPerformed(java.awt.event.ActionEvent evt) {
double a,b;
b=1;
hasil=Double.parseDouble(txt.getText());
for(a=1;a<=hasil;a++){
b=b*a;
}
txt.setText(String.valueOf(b));
}
private void
negativeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
hasil=Double.parseDouble(txt.getText())*-1;
txt.setText(String.valueOf(hasil));
}
//di bawah ini operator trigonometri
private void
sinActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
if(Double.parseDouble(txt.getText())==30){
txt.setText("0.5");
}
else
if(Double.parseDouble(txt.getText())==0){
txt.setText("0.0");
}
else if
(Double.parseDouble(txt.getText())==90){
txt.setText("1.0");
}
else{
hasil =
Math.sin(Double.parseDouble(txt.getText())/180*22/7);
txt.setText(String.valueOf(hasil));
}
}
private void
cosActionPerformed(java.awt.event.ActionEvent evt) {
if(Double.parseDouble(txt.getText())==60){
txt.setText("0.5");
}
else
if(Double.parseDouble(txt.getText())==90){
txt.setText("0.0");
}
else if
(Double.parseDouble(txt.getText())==0){
txt.setText("1.0");
}
else{
hasil =
Math.cos(Double.parseDouble(txt.getText())/180*22/7);
txt.setText(String.valueOf(hasil));
}
}
private void
tanActionPerformed(java.awt.event.ActionEvent evt) {
if(Double.parseDouble(txt.getText())==45){
txt.setText("1.0");
}
else
if(Double.parseDouble(txt.getText())==0){
txt.setText("0.0");
}
else if
(Double.parseDouble(txt.getText())==90){
txt.setText("infinity");
}
else{
hasil =
Math.tan(Double.parseDouble(txt.getText())/180*22/7);
txt.setText(String.valueOf(hasil));
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public
void run() {
new
calc().setVisible(true);
}
});
}
selamat mencoba
Thanks banget,bos
BalasHapusSangat bermanfaat dan Membantu..
seeep laaah
BalasHapusGan minta fileny donk dikirim email aj yah reinert_yosua@yahoo.com
BalasHapusthnks
ditunggu
Knp kuning
BalasHapusKnp kuning
BalasHapus