Kalkulator adalah sebuah alat hitung
yang digunakan manusia untuk membantu proses perhitungan dalam kegiatan
sehari-hari.
Kalkulator yang sering kita gunakan
adalah kalkulator hard were yang didalamnya terdapat proses apa ketika kita
gunakan kita tidak tahu,,yang kita tahu hanya keluar hasilnya dengan cepat
lebih cepat dari perhitungan kita secara manual. Kali ini saya akan menjelaskan
bagaimana cara membuat kalkulator dengan eclipse dengan bahasa Java, dan juga
proses apa yag ada didalamnya.
1. Pertama kita buka eclipse,
sebelumnya anda install dulu java jdk-Nya.
Buat project java file - New -
Project misal kita kasih nama "Kalkulator"
2. Buat Package untuk menaruh meneu
utama (main.java) misal kita buat package dengan nama calculator file - New –
Package
3. Buat class utama dalam package
calculator misal Main.java
4. file - new - class
5. Kita isikan script ini di
view.java
package calculator;
import view.CalculatorView;
public class Main {
public static void main(String[] args) {
new CalculatorView().setVisible(true);
}
}
6. Buat package view untuk menaruh
class CalculatorView.java . Caranya file - new - package seperti membuat
package diatas.
7. Buat class utama dalam package
view misal CalculatorView.java
8. file - new - class seperti
membuat class diatas.
9. Kita isikan script ini di
CalculatorView.java
package view;
import java.text.DecimalFormat;
import model.CalculatorModel;
public class CalculatorView extends
javax.swing.JFrame {
private static final long serialVersionUID =
1L;
public CalculatorView() {
initComponents();
}
CalculatorModel model = new CalculatorModel();
String operand="";
public void getOperand(javax.swing.JButton button){
operand+=button.getText();
model.setOperand(operand);
resultLabel.setText(operand);
}
private void getOperator(int opt){
model.setOperator(opt);
operand="";
}
private void process(){
DecimalFormat df = new
DecimalFormat("#,###.########");
model.process();
operand = "";
resultLabel.setText(df.format(model.getResult())+"");
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
resultLabel = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
button7 = new javax.swing.JButton();
button4 = new javax.swing.JButton();
button1 = new javax.swing.JButton();
buttonKoma = new javax.swing.JButton();
button11 = new javax.swing.JButton();
button12 = new javax.swing.JButton();
button2 = new javax.swing.JButton();
button3 = new javax.swing.JButton();
button5 = new javax.swing.JButton();
button6 = new javax.swing.JButton();
button8 = new javax.swing.JButton();
button9 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
buttonBagi = new javax.swing.JButton();
buttonKali = new javax.swing.JButton();
buttonKurang = new
javax.swing.JButton();
buttonTambah = new
javax.swing.JButton();
buttonAC = new javax.swing.JButton();
buttonModulus = new
javax.swing.JButton();
buttonSeper = new
javax.swing.JButton();
buttonSamaDengan = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Kalkulator Java with
Eclipse");
jPanel1.setBackground(new
java.awt.Color(150, 250, 200));
resultLabel.setBackground(new
java.awt.Color(255, 255, 200));
resultLabel.setFont(new
java.awt.Font("Microsoft Sans Serif", 0, 36));
resultLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
resultLabel.setText("0");
javax.swing.GroupLayout jPanel1Layout =
new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(resultLabel,
javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(resultLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2.setBackground(new
java.awt.Color(150, 250, 200));
button7.setText("7");
button7.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button7ActionPerformed(evt);
}
});
button4.setText("4");
button4.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button4ActionPerformed(evt);
}
});
button1.setText("1");
button1.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
buttonKoma.setText(".");
buttonKoma.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
buttonKomaActionPerformed(evt);
}
});
button11.setText("0");
button11.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button11ActionPerformed(evt);
}
});
button12.setText("C");
button12.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button12ActionPerformed(evt);
}
});
button2.setText("2");
button2.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button2ActionPerformed(evt);
}
});
button3.setText("3");
button3.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button3ActionPerformed(evt);
}
});
button5.setText("5");
button5.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button5ActionPerformed(evt);
}
});
button6.setText("6");
button6.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button6ActionPerformed(evt);
}
});
button8.setText("8");
button8.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button8ActionPerformed(evt);
}
});
button9.setText("9");
button9.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
button9ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout =
new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(button7,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button8,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button9,
javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(button4,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button5,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button6,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(button1,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button2,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button3,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(buttonKoma, javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button11,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button12,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button7,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button8,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button9,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button4,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button5,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button6,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button1,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button2,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button3,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonKoma,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button11,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button12,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
jPanel3.setBackground(new
java.awt.Color(150, 250, 200));
buttonBagi.setText("/");
buttonBagi.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
buttonBagiActionPerformed(evt);
}
});
buttonKali.setText("*");
buttonKali.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
buttonKaliActionPerformed(evt);
}
});
buttonKurang.setText("-");
buttonKurang.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
buttonKurangActionPerformed(evt);
}
});
buttonTambah.setText("+");
buttonTambah.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
buttonTambahActionPerformed(evt);
}
});
buttonAC.setText("AC");
buttonAC.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
buttonACActionPerformed(evt);
}
});
buttonModulus.setText("%");
buttonModulus.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
buttonModulusActionPerformed(evt);
}
});
buttonSeper.setText("1/x");
buttonSeper.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
buttonSeperActionPerformed(evt);
}
});
buttonSamaDengan.setText("=");
buttonSamaDengan.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
buttonSamaDenganActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout =
new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(buttonKurang, javax.swing.GroupLayout.PREFERRED_SIZE, 45,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(buttonSeper, javax.swing.GroupLayout.PREFERRED_SIZE, 45,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(buttonTambah, javax.swing.GroupLayout.PREFERRED_SIZE, 45,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(buttonSamaDengan, javax.swing.GroupLayout.PREFERRED_SIZE,
45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
jPanel3Layout.createSequentialGroup()
.addComponent(buttonKali,
javax.swing.GroupLayout.PREFERRED_SIZE, 45,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(buttonModulus, javax.swing.GroupLayout.PREFERRED_SIZE, 45,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
jPanel3Layout.createSequentialGroup()
.addComponent(buttonBagi, javax.swing.GroupLayout.PREFERRED_SIZE, 45,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(buttonAC, javax.swing.GroupLayout.PREFERRED_SIZE, 45,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonBagi,
javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonAC,
javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonKali,
javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonModulus, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonKurang,
javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonSeper,
javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonTambah,
javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonSamaDengan, javax.swing.GroupLayout.PREFERRED_SIZE,
33, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(14,
Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE, 60,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jPanel3,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jPanel2,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void button1ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_button1ActionPerformed
getOperand(button1);
}
private void button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button2ActionPerformed
getOperand(button2);
}
private void button3ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_button3ActionPerformed
getOperand(button3);
}
private void button4ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_button4ActionPerformed
getOperand(button4);
}
private void button5ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_button5ActionPerformed
getOperand(button5);
}
private void button6ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_button6ActionPerformed
getOperand(button6);
}
private void button7ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_button7ActionPerformed
getOperand(button7);
}
private void button8ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_button8ActionPerformed
getOperand(button8);
}
private void button9ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_button9ActionPerformed
getOperand(button9);
}
private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_buttonTambahActionPerformed
getOperator(1);
}
private void buttonKurangActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_buttonKurangActionPerformed
getOperator(2);
}
private void buttonKaliActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_buttonKaliActionPerformed
getOperator(3);
}
private void buttonBagiActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_buttonBagiActionPerformed
getOperator(4);
}
private void buttonModulusActionPerformed(java.awt.event.ActionEvent
evt) {//GEN-FIRST:event_buttonModulusActionPerformed
getOperator(5);
}
private void buttonSeperActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_buttonSeperActionPerformed
getOperator(6);
}
private void buttonSamaDenganActionPerformed(java.awt.event.ActionEvent
evt) {//GEN-FIRST:event_buttonSamaDenganActionPerformed
process();
}
private void button11ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_button11ActionPerformed
getOperand(button11);
}
private void buttonKomaActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_buttonKomaActionPerformed
getOperand(buttonKoma);
}
private void button12ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_button12ActionPerformed
if(operand.length()>1){
operand = operand.substring(0,
operand.length()-1);
model.setOperand(operand);
resultLabel.setText(operand);
}else{
operand = "";
model.setOperand(operand);
resultLabel.setText("0");
}
}
private void buttonACActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_buttonACActionPerformed
operand = "";
model.setOperator(0);
model.setResult(0);
resultLabel.setText("0");
}
private javax.swing.JButton button1;
private javax.swing.JButton button11;
private javax.swing.JButton button12;
private javax.swing.JButton button2;
private javax.swing.JButton button3;
private javax.swing.JButton button4;
private javax.swing.JButton button5;
private javax.swing.JButton button6;
private javax.swing.JButton button7;
private javax.swing.JButton button8;
private javax.swing.JButton button9;
private javax.swing.JButton buttonAC;
private javax.swing.JButton buttonBagi;
private javax.swing.JButton buttonKali;
private javax.swing.JButton buttonKoma;
private javax.swing.JButton buttonKurang;
private javax.swing.JButton buttonModulus;
private javax.swing.JButton buttonSamaDengan;
private javax.swing.JButton buttonSeper;
private javax.swing.JButton buttonTambah;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JLabel resultLabel;
}
10. Buat package model untuk menaruh
class model caranya seperti cara membuat package diatas file - new - package.
11. Buat class CalculatorModel.java
di package model seperti membuat class diatas
12. file - new - class
13. Isikan script ini di class
CalculatorModel.java
package model;
public class CalculatorModel {
int operator=0;
double operand1=0;
double operand2=0;
double result=0;
public void setOperand(String opr) {
if(!opr.equals("")){
if(operator==0){
operand1=Double.valueOf(opr);
}else{
operand2=Double.valueOf(opr);
}
}
}
public void setOperator(int operator) {
this.operator = operator;
}
public double getResult() {
return result;
}
public void setResult(double hasil) {
this.result = hasil;
}
public void process(){
switch (operator){
case 1:
result = operand1 + operand2;
break;
case 2:
result = operand1 - operand2;
break;
case 3:
result = operand1 * operand2;
break;
case 4:
result = operand1 / operand2;
break;
case 5:
result = operand1 % operand2;
break;
case 6:
result = 1/operand1;
break;
default: result = operand1;
}
operand1=result;
}
public void setOperand1(double operand1) {
this.operand1 = operand1;
}
public void setOperand2(double operand2) {
this.operand2 = operand2;
}
public void clear(){
setOperand1(0);
setOperand2(0);
setResult(0);
setOperator(0);
}
}
11. Run program dengan cara klik
tombol segitiga hijau di bagian atas
dan hasilnya ini.
Sekian turtorial cara membuat
kalkulator dengan java.
0 komentar:
Posting Komentar