Main.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kalkulator;
import view.KalkulatorView;
/**
*
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new KalkulatorView().setVisible(true);
}
}
KalkulatorModel.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package model;
/**
*
*/
public class KalkulatorModel {
int operator=0;
double oper1;
double oper2;
double result;
public void setOperand(String opr) {
if(!opr.equals("")){
if(operator==0){
oper1=Double.valueOf(opr);
}else{
oper2=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 = oper1 + oper2;
break;
case 2:
result = oper1 - oper2;
break;
case 3:
result = oper1 * oper2;
break;
case 4:
result = oper1 / oper2;
break;
case 5:
result = oper1 % oper2;
break;
case 6:
result = 1/oper1;
break;
}
oper1=result;
}
}
KalkulatorView.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* KalkulatorView.java
*
*/
package view;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import model.KalkulatorModel;
/**
*
* @author Deden
*/
public class KalkulatorView extends javax.swing.JFrame {
/** Creates new form CalculatorView */
public KalkulatorView() {
initComponents();
}
/** 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.
*/
KalkulatorModel model = new KalkulatorModel();
String oper="";
public void getOperand(javax.swing.JButton button){
oper+=button.getText();
model.setOperand(oper);
resultLabel.setText(oper);
}
private void getOperator(int opt){
model.setOperator(opt);
oper="";
}
private void process(){
DecimalFormat df = new DecimalFormat("#,###.########");
model.process();
oper = "";
resultLabel.setText(df.format(model.getResult())+"");
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
resultLabel = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
button7 = new javax.swing.JButton();
button8 = new javax.swing.JButton();
button9 = new javax.swing.JButton();
button4 = new javax.swing.JButton();
button5 = new javax.swing.JButton();
button6 = new javax.swing.JButton();
button1 = new javax.swing.JButton();
button2 = new javax.swing.JButton();
button3 = new javax.swing.JButton();
buttonKoma = new javax.swing.JButton();
button11 = new javax.swing.JButton();
button12 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
buttonBagi = new javax.swing.JButton();
buttonKali = new javax.swing.JButton();
buttonTambah = new javax.swing.JButton();
buttonKurang = 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 Sederhana");
setBackground(new java.awt.Color(255, 51, 0));
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
resultLabel.setBackground(new java.awt.Color(255, 255, 255));
resultLabel.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 36)); // NOI18N
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, 262, 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(255, 255, 51));
button7.setText("7");
button7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button7ActionPerformed(evt);
}
});
jPanel2.add(button7);
button8.setText("8");
button8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button8ActionPerformed(evt);
}
});
jPanel2.add(button8);
button9.setText("9");
button9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button9ActionPerformed(evt);
}
});
jPanel2.add(button9);
button4.setText("4");
button4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button4ActionPerformed(evt);
}
});
jPanel2.add(button4);
button5.setText("5");
button5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button5ActionPerformed(evt);
}
});
jPanel2.add(button5);
button6.setText("6");
button6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button6ActionPerformed(evt);
}
});
jPanel2.add(button6);
button1.setText("1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
jPanel2.add(button1);
button2.setText("2");
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button2ActionPerformed(evt);
}
});
jPanel2.add(button2);
button3.setText("3");
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button3ActionPerformed(evt);
}
});
jPanel2.add(button3);
buttonKoma.setText(".");
buttonKoma.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonKomaActionPerformed(evt);
}
});
jPanel2.add(buttonKoma);
button11.setText("0");
button11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button11ActionPerformed(evt);
}
});
jPanel2.add(button11);
button12.setText("C");
button12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button12ActionPerformed(evt);
}
});
jPanel2.add(button12);
jPanel3.setBackground(new java.awt.Color(255, 153, 0));
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);
}
});
buttonTambah.setText("+");
buttonTambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonTambahActionPerformed(evt);
}
});
buttonKurang.setText("-");
buttonKurang.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonKurangActionPerformed(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()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(buttonKali, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonBagi, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(buttonSamaDengan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttonSeper, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
.addComponent(buttonKurang, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(buttonModulus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
.addComponent(buttonTambah, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(buttonAC))))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonTambah, 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.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonKurang, 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(buttonKali, 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(buttonBagi, 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(javax.swing.GroupLayout.DEFAULT_SIZE, 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, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap())
);
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(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(32, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(button1);
}
private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(button2);
}
private void button3ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(button3);
}
private void button4ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(button4);
}
private void button5ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(button5);
}
private void button6ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(button6);
}
private void button7ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(button7);
}
private void button8ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(button8);
}
private void button9ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(button9);
}
private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) {
getOperator(1);
}
private void buttonKurangActionPerformed(java.awt.event.ActionEvent evt) {
getOperator(2);
}
private void buttonKaliActionPerformed(java.awt.event.ActionEvent evt) {
getOperator(3);
}
private void buttonBagiActionPerformed(java.awt.event.ActionEvent evt) {
getOperator(4);
}
private void buttonModulusActionPerformed(java.awt.event.ActionEvent evt) {
getOperator(5);
}
private void buttonSeperActionPerformed(java.awt.event.ActionEvent evt) {
getOperator(6);
}
private void buttonSamaDenganActionPerformed(java.awt.event.ActionEvent evt) {
process();
}
private void button11ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(button11);
}
private void buttonKomaActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(buttonKoma);
}
private void button12ActionPerformed(java.awt.event.ActionEvent evt) {
if(oper.length()>1){
oper = oper.substring(0, oper.length()-1);
model.setOperand(oper);
resultLabel.setText(oper);
}else{
oper = "";
model.setOperand(oper);
resultLabel.setText("0");
}
}
private void buttonACActionPerformed(java.awt.event.ActionEvent evt) {
oper = "";
model.setOperator(0);
model.setResult(0);
resultLabel.setText("0");
}
// Variables declaration - do not modify
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;
// End of variables declaration
}
ini lah hasilnya
Tidak ada komentar:
Posting Komentar