Sedikit share source code untuk membuat kalkulator. Tampilannya kira-kira seperti ini:
Tapi masih banyak kekurangannya, jadi mohon koreksinya ![]()
/*
* Kalkulator.java
*
* Created on March 26, 2011, 8:22 PM
*/
package kalkulator_sederhana;
/**
*
* @author cursedcode
*/
public class Kalkulator extends javax.swing.JFrame {
String angka;
Double total,angka1,angka2;
int pilih;
/** Creates new form Kalkulator */
public Kalkulator() {
initComponents();
angka="";
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {
cmdtampil = new javax.swing.JTextField();
cmd1 = new javax.swing.JButton();
cmd2 = new javax.swing.JButton();
cmd3 = new javax.swing.JButton();
cmd4 = new javax.swing.JButton();
cmd5 = new javax.swing.JButton();
cmd6 = new javax.swing.JButton();
cmd7 = new javax.swing.JButton();
cmd8 = new javax.swing.JButton();
cmd9 = new javax.swing.JButton();
cmdpersen = new javax.swing.JButton();
cmd0 = new javax.swing.JButton();
cmdkoma = new javax.swing.JButton();
cmdtambah = new javax.swing.JButton();
cmdbagi = new javax.swing.JButton();
cmdkurang = new javax.swing.JButton();
cmdkali = new javax.swing.JButton();
cmdminplus = new javax.swing.JButton();
cmdclear = new javax.swing.JButton();
cmdsamadengan = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
cmd1.setText("1");
cmd1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd1ActionPerformed(evt);
}
});
cmd2.setText("2");
cmd2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd2ActionPerformed(evt);
}
});
cmd3.setText("3");
cmd3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd3ActionPerformed(evt);
}
});
cmd4.setText("4");
cmd4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd4ActionPerformed(evt);
}
});
cmd5.setText("5");
cmd5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd5ActionPerformed(evt);
}
});
cmd6.setText("6");
cmd6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd6ActionPerformed(evt);
}
});
cmd7.setText("7");
cmd7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd7ActionPerformed(evt);
}
});
cmd8.setText("8");
cmd8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd8ActionPerformed(evt);
}
});
cmd9.setText("9");
cmd9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd9ActionPerformed(evt);
}
});
cmdpersen.setText("%");
cmdpersen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdpersenActionPerformed(evt);
}
});
cmd0.setText("0");
cmd0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd0ActionPerformed(evt);
}
});
cmdkoma.setText(".");
cmdkoma.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdkomaActionPerformed(evt);
}
});
cmdtambah.setText("+");
cmdtambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdtambahActionPerformed(evt);
}
});
cmdbagi.setText("/");
cmdbagi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdbagiActionPerformed(evt);
}
});
cmdkurang.setText("-");
cmdkurang.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdkurangActionPerformed(evt);
}
});
cmdkali.setText("*");
cmdkali.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdkaliActionPerformed(evt);
}
});
cmdminplus.setText("+/-");
cmdminplus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdminplusActionPerformed(evt);
}
});
cmdclear.setText("C");
cmdclear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdclearActionPerformed(evt);
}
});
cmdsamadengan.setText("=");
cmdsamadengan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdsamadenganActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, cmdtampil)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(cmdpersen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmd0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmdkoma, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmdsamadengan, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(cmd1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmd2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmd3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(cmd4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmd5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmd6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(cmd7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmd8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmd9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(cmdtambah, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmdbagi, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(cmdkurang, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmdkali, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(cmdminplus, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmdclear, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(cmdtampil, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(19, 19, 19)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(cmd7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmd8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmd9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(cmd4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmd5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmd6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(cmd1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmd2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmd3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(cmdpersen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmd0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmdkoma, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmdsamadengan, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(cmdminplus, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmdclear, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(cmdkurang, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmdkali, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(cmdtambah, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(cmdbagi, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
);
pack();
}//
private void cmd2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText(angka +="2");
}
private void cmd3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText(angka +="3");
}
private void cmd5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText(angka +="5");
}
private void cmd6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText(angka +="6");
}
private void cmd8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText(angka +="8");
}
private void cmd9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText(angka +="9");
}
private void cmd0ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText(angka +="0");
}
private void cmdkomaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText(angka +=".");
}
private void cmdtambahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka1=Double.parseDouble(angka);
cmdtampil.setText("+");
angka="";
pilih=1;
}
private void cmdbagiActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka1=Double.parseDouble(angka);
cmdtampil.setText("/");
angka="";
pilih=4;
}
private void cmdkurangActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka1=Double.parseDouble(angka);
cmdtampil.setText("-");
angka="";
pilih=2;
}
private void cmdkaliActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka1=Double.parseDouble(angka);
cmdtampil.setText("*");
angka="";
pilih=3;
}
private void cmdminplusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka2 = Double.parseDouble(angka);
total = 0 - angka2;
angka = Double.toString(total);
cmdtampil.setText(angka);
}
private void cmdclearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka1=0.0;
angka2=0.0;
total=0.0;
angka="";
cmdtampil.setText("");
}
private void cmdsamadenganActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
switch(pilih)
{case 1:
angka2 = Double.parseDouble(angka);
total = angka1 + angka2;
angka = Double.toString(total);
cmdtampil.setText(angka);
break;
case 2:
angka2 = Double.parseDouble(angka);
total = angka1 - angka2;
angka = Double.toString(total);
cmdtampil.setText(angka);
break;
case 3:
angka2 = Double.parseDouble(angka);
total = angka1 * angka2;
angka = Double.toString(total);
cmdtampil.setText(angka);
break;
case 4:
angka2 = Double.parseDouble(angka);
total = angka1 / angka2;
angka = Double.toString(total);
cmdtampil.setText(angka);
break;
default:
break;}
}
private void cmd7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText(angka +="7");
}
private void cmd1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText(angka +="1");
}
private void cmd4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cmdtampil.setText (angka +="4");
}
private void cmdpersenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka2 = Double.parseDouble(angka);
total = angka2 / 100;
angka = Double.toString(total);
cmdtampil.setText(angka);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Kalkulator().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton cmd0;
private javax.swing.JButton cmd1;
private javax.swing.JButton cmd2;
private javax.swing.JButton cmd3;
private javax.swing.JButton cmd4;
private javax.swing.JButton cmd5;
private javax.swing.JButton cmd6;
private javax.swing.JButton cmd7;
private javax.swing.JButton cmd8;
private javax.swing.JButton cmd9;
private javax.swing.JButton cmdbagi;
private javax.swing.JButton cmdclear;
private javax.swing.JButton cmdkali;
private javax.swing.JButton cmdkoma;
private javax.swing.JButton cmdkurang;
private javax.swing.JButton cmdminplus;
private javax.swing.JButton cmdpersen;
private javax.swing.JButton cmdsamadengan;
private javax.swing.JButton cmdtambah;
private javax.swing.JTextField cmdtampil;
// End of variables declaration
}


