Dosen : Romi Satria Wahono, M.Eng
Calculator
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Calculator.java
*
*
*/
package GUI;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
*
*
*/
public class Calculator extends javax.swing.JFrame {
private double operandSatu = 0;
private double operandDua = 0;
private String operator = "";
private String prevOperator = "";
private boolean isFirst = true;
private boolean isHitung = false;
/** Creates new form Calculator */
public Calculator() {
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.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
layarTextField = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
angka1Button = new javax.swing.JButton();
angka2Button = new javax.swing.JButton();
angka3Button = new javax.swing.JButton();
angka6Button = new javax.swing.JButton();
angka5Button = new javax.swing.JButton();
angka4Button = new javax.swing.JButton();
angka9Button = new javax.swing.JButton();
angka8Button = new javax.swing.JButton();
angka7Button = new javax.swing.JButton();
resetButton = new javax.swing.JButton();
angka0Button = new javax.swing.JButton();
titikButton = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
tambahButton = new javax.swing.JButton();
kurangButton = new javax.swing.JButton();
kaliButton = new javax.swing.JButton();
bagiButton = new javax.swing.JButton();
samadenganButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 153, 51));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Eresha Calculator", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(255, 255, 255))); // NOI18N
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(19, Short.MAX_VALUE)
.addComponent(layarTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(layarTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2.setBackground(new java.awt.Color(255, 153, 51));
angka1Button.setText("1");
angka1Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
angka1ButtonActionPerformed(evt);
}
});
angka2Button.setText("2");
angka2Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
angka2ButtonActionPerformed(evt);
}
});
angka3Button.setText("3");
angka3Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
angka3ButtonActionPerformed(evt);
}
});
angka6Button.setText("6");
angka6Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
angka6ButtonActionPerformed(evt);
}
});
angka5Button.setText("5");
angka5Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
angka5ButtonActionPerformed(evt);
}
});
angka4Button.setText("4");
angka4Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
angka4ButtonActionPerformed(evt);
}
});
angka9Button.setText("9");
angka9Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
angka9ButtonActionPerformed(evt);
}
});
angka8Button.setText("8");
angka8Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
angka8ButtonActionPerformed(evt);
}
});
angka7Button.setText("7");
angka7Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
angka7ButtonActionPerformed(evt);
}
});
resetButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
resetButton.setForeground(new java.awt.Color(255, 51, 0));
resetButton.setText("C");
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetButtonActionPerformed(evt);
}
});
angka0Button.setText("0");
angka0Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
angka0ButtonActionPerformed(evt);
}
});
titikButton.setText(".");
titikButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
titikButtonActionPerformed(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(angka1Button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(angka2Button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(angka3Button))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(angka4Button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(angka5Button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(angka6Button))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(angka7Button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(angka8Button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(angka9Button))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(resetButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(angka0Button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(titikButton)))
.addGap(20, 20, 20))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(angka1Button)
.addComponent(angka2Button)
.addComponent(angka3Button))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(angka4Button)
.addComponent(angka5Button)
.addComponent(angka6Button))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(angka7Button)
.addComponent(angka8Button)
.addComponent(angka9Button))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(resetButton)
.addComponent(angka0Button)
.addComponent(titikButton))
.addContainerGap(19, Short.MAX_VALUE))
);
jPanel3.setBackground(new java.awt.Color(255, 153, 0));
tambahButton.setText("+");
tambahButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tambahButtonActionPerformed(evt);
}
});
kurangButton.setText("-");
kurangButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
kurangButtonActionPerformed(evt);
}
});
kaliButton.setText("*");
kaliButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
kaliButtonActionPerformed(evt);
}
});
bagiButton.setText("/");
bagiButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bagiButtonActionPerformed(evt);
}
});
samadenganButton.setText("=");
samadenganButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
samadenganButtonActionPerformed(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.TRAILING)
.addComponent(samadenganButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(kurangButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(tambahButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(bagiButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(kaliButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGap(24, 24, 24))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tambahButton)
.addComponent(kaliButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(kurangButton)
.addComponent(bagiButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(samadenganButton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, 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.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, 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, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.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())
);
pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();
setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
}//
private void setLayar(String angka) {
String isiLayar = layarTextField.getText();
if (isiLayar.length() > 11) {
return;
}
if (isFirst || isHitung) {
isFirst = false;
isHitung = false;
isiLayar = angka + "";
} else {
isiLayar = isiLayar + angka;
}
layarTextField.setText(isiLayar);
isHitung = false;
if (operator.equals("")) {
operandSatu = Double.parseDouble(isiLayar);
} else {
operandDua = Double.parseDouble(isiLayar);
}
}
private void angka1ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_angka1ButtonActionPerformed
// TODO add your handling code here:
setLayar("1");
}//GEN-LAST:event_angka1ButtonActionPerformed
private void angka4ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_angka4ButtonActionPerformed
// TODO add your handling code here:
setLayar("4");
}//GEN-LAST:event_angka4ButtonActionPerformed
private void angka7ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_angka7ButtonActionPerformed
// TODO add your handling code here:
setLayar("7");
// lastangka = true;
}//GEN-LAST:event_angka7ButtonActionPerformed
private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
// TODO add your handling code here:
resetAngka();
}//GEN-LAST:event_resetButtonActionPerformed
private void resetAngka() {
layarTextField.setText("0");
operandSatu = 0;
operandDua = 0;
operator = "";
isFirst = true;
isHitung = false;
}
private void tambahButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tambahButtonActionPerformed
// TODO add your handling code here:
hitung();
operator = "+";
//lastangka = false;
}//GEN-LAST:event_tambahButtonActionPerformed
private void kaliButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kaliButtonActionPerformed
// TODO add your handling code here:
hitung();
operator = "*";
}//GEN-LAST:event_kaliButtonActionPerformed
private void kurangButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kurangButtonActionPerformed
// TODO add your handling code here:
hitung();
operator = "-";
}//GEN-LAST:event_kurangButtonActionPerformed
private void bagiButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bagiButtonActionPerformed
// TODO add your handling code here:
hitung();
operator = "/";
}//GEN-LAST:event_bagiButtonActionPerformed
private void samadenganButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_samadenganButtonActionPerformed
// TODO add your handling code here:
double hasil = hitung();
operator = "";
layarTextField.setText(decimalFormat(hasil, "#.############"));
}//GEN-LAST:event_samadenganButtonActionPerformed
private void angka2ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_angka2ButtonActionPerformed
// TODO add your handling code here:
setLayar("2");
}//GEN-LAST:event_angka2ButtonActionPerformed
private void angka3ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_angka3ButtonActionPerformed
// TODO add your handling code here:
setLayar("3");
}//GEN-LAST:event_angka3ButtonActionPerformed
private void angka5ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_angka5ButtonActionPerformed
// TODO add your handling code here:
setLayar("5");
}//GEN-LAST:event_angka5ButtonActionPerformed
private void angka6ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_angka6ButtonActionPerformed
// TODO add your handling code here:
setLayar("6");
}//GEN-LAST:event_angka6ButtonActionPerformed
private void angka8ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_angka8ButtonActionPerformed
// TODO add your handling code here:
setLayar("8");
}//GEN-LAST:event_angka8ButtonActionPerformed
private void angka9ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_angka9ButtonActionPerformed
// TODO add your handling code here:
setLayar("9");
}//GEN-LAST:event_angka9ButtonActionPerformed
private void angka0ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_angka0ButtonActionPerformed
// TODO add your handling code here:
setLayar("0");
}//GEN-LAST:event_angka0ButtonActionPerformed
private void titikButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_titikButtonActionPerformed
// TODO add your handling code here:
setLayar(".");
}//GEN-LAST:event_titikButtonActionPerformed
private double hitung() {
double ret = 0;
// cetak();
prevOperator = operator;
isHitung = true;
if (prevOperator.equals("+")) {
ret = operandSatu + operandDua;
} else if (prevOperator.equals("-")) {
ret = operandSatu - operandDua;
} else if (prevOperator.equals("*")) {
ret = operandSatu * operandDua;
} else if (prevOperator.equals("/")) {
if (operandDua == 0) {
ret = 999999999999F;
} else {
ret = operandSatu / operandDua;
}
} else {
ret = operandSatu;
return ret;
}
operandSatu = ret;
operandDua = 0;
//cetak();
isFirst = false;
return ret;
}
private String decimalFormat(double number, String pattern) {
NumberFormat formatter = new DecimalFormat(pattern);
String s = formatter.format(number);
return s;
}
private void cetak() {
System.out.println("operandSatu : " + operandSatu);
System.out.println("operandDua : " + operandDua);
}
private void resetNilai() {
operandSatu = 0;
operandDua = 0;
operator = "";
prevOperator = "";
isHitung = false;
isFirst = true;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton angka0Button;
private javax.swing.JButton angka1Button;
private javax.swing.JButton angka2Button;
private javax.swing.JButton angka3Button;
private javax.swing.JButton angka4Button;
private javax.swing.JButton angka5Button;
private javax.swing.JButton angka6Button;
private javax.swing.JButton angka7Button;
private javax.swing.JButton angka8Button;
private javax.swing.JButton angka9Button;
private javax.swing.JButton bagiButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JButton kaliButton;
private javax.swing.JButton kurangButton;
private javax.swing.JTextField layarTextField;
private javax.swing.JButton resetButton;
private javax.swing.JButton samadenganButton;
private javax.swing.JButton tambahButton;
private javax.swing.JButton titikButton;
// End of variables declaration//GEN-END:variables
}
Tidak ada komentar:
Posting Komentar