1. Membuat sebuah project
2. Membuat sebuah package
3. Membuat sebuah kelas untuk mendesain
Desain :
Source code :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package kalkulatormvc;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import kalkulatormvc.KalkulatorModel;
/**
*
* @author DesDesianny
*/
public class TampilanKalkulator extends javax.swing.JFrame
{
/**
* Creates new form TampilanKalkulator
*/
public TampilanKalkulator()
{
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 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())+"");
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
resultLabel = new javax.swing.JTextField();
btnHapus = new javax.swing.JButton();
btnSamadengan = new javax.swing.JButton();
btnAngka1 = new javax.swing.JButton();
btnAngka2 = new javax.swing.JButton();
btnAngka3 = new javax.swing.JButton();
btnAngka4 = new javax.swing.JButton();
btnAngka5 = new javax.swing.JButton();
btnAngka6 = new javax.swing.JButton();
btnAngka7 = new javax.swing.JButton();
btnAngka8 = new javax.swing.JButton();
btnAngka9 = new javax.swing.JButton();
btnTambah = new javax.swing.JButton();
btnKurang = new javax.swing.JButton();
btnKali = new javax.swing.JButton();
btnAngka0 = new javax.swing.JButton();
btnKoma = new javax.swing.JButton();
btnBagi = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
resultLabel.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
resultLabel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resultLabelActionPerformed(evt);
}
});
btnHapus.setText("C");
btnHapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnHapusActionPerformed(evt);
}
});
btnSamadengan.setText("=");
btnSamadengan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSamadenganActionPerformed(evt);
}
});
btnAngka1.setText("1");
btnAngka1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka1ActionPerformed(evt);
}
});
btnAngka2.setText("2");
btnAngka2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka2ActionPerformed(evt);
}
});
btnAngka3.setText("3");
btnAngka3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka3ActionPerformed(evt);
}
});
btnAngka4.setText("4");
btnAngka4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka4ActionPerformed(evt);
}
});
btnAngka5.setText("5");
btnAngka5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka5ActionPerformed(evt);
}
});
btnAngka6.setText("6");
btnAngka6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka6ActionPerformed(evt);
}
});
btnAngka7.setText("7");
btnAngka7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka7ActionPerformed(evt);
}
});
btnAngka8.setText("8");
btnAngka8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka8ActionPerformed(evt);
}
});
btnAngka9.setText("9");
btnAngka9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka9ActionPerformed(evt);
}
});
btnTambah.setText("+");
btnTambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnTambahActionPerformed(evt);
}
});
btnKurang.setText("-");
btnKurang.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnKurangActionPerformed(evt);
}
});
btnKali.setText("x");
btnKali.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnKaliActionPerformed(evt);
}
});
btnAngka0.setText("0");
btnAngka0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka0ActionPerformed(evt);
}
});
btnKoma.setText(".");
btnKoma.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnKomaActionPerformed(evt);
}
});
btnBagi.setText("/");
btnBagi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBagiActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btnAngka7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
.addComponent(btnAngka1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnAngka4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(btnAngka2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnAngka3, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btnAngka8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
.addComponent(btnAngka5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnAngka6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnAngka9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnKurang, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnTambah, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnKali, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnAngka0, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnKoma, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnBagi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(resultLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnHapus, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnSamadengan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
.addGap(49, 49, 49))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(resultLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnHapus, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addComponent(btnSamadengan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnTambah, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
.addComponent(btnAngka1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnAngka2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnAngka3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnKurang, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnAngka4, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnAngka5, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(btnAngka6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnAngka7, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
.addComponent(btnAngka8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnKali, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnAngka9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnAngka0, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
.addComponent(btnBagi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnKoma, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(41, 41, 41))
);
pack();
}// </editor-fold>
private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {
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 resultLabelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnAngka4ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnAngka4);
}
private void btnAngka5ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnAngka5);
}
private void btnAngka1ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnAngka1);
}
private void btnAngka2ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnAngka2);
}
private void btnAngka3ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnAngka3);
}
private void btnAngka6ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnAngka6);
}
private void btnAngka7ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnAngka7);
}
private void btnAngka8ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnAngka8);
}
private void btnAngka9ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnAngka9);
}
private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {
getOperator(1);
}
private void btnKurangActionPerformed(java.awt.event.ActionEvent evt) {
getOperator(2);
}
private void btnKaliActionPerformed(java.awt.event.ActionEvent evt) {
getOperator(3);
}
private void btnBagiActionPerformed(java.awt.event.ActionEvent evt) {
getOperator(4);
}
private void btnSamadenganActionPerformed(java.awt.event.ActionEvent evt) {
process();
}
private void btnAngka0ActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnAngka0);
}
private void btnKomaActionPerformed(java.awt.event.ActionEvent evt) {
getOperand(btnKoma);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TampilanKalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TampilanKalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TampilanKalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TampilanKalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TampilanKalkulator().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAngka0;
private javax.swing.JButton btnAngka1;
private javax.swing.JButton btnAngka2;
private javax.swing.JButton btnAngka3;
private javax.swing.JButton btnAngka4;
private javax.swing.JButton btnAngka5;
private javax.swing.JButton btnAngka6;
private javax.swing.JButton btnAngka7;
private javax.swing.JButton btnAngka8;
private javax.swing.JButton btnAngka9;
private javax.swing.JButton btnBagi;
private javax.swing.JButton btnHapus;
private javax.swing.JButton btnKali;
private javax.swing.JButton btnKoma;
private javax.swing.JButton btnKurang;
private javax.swing.JButton btnSamadengan;
private javax.swing.JButton btnTambah;
private javax.swing.JTextField resultLabel;
// End of variables declaration
}
4. Membuat kelas untuk rumus
Source code :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package kalkulatormvc;
/**
*
* @author DesDesianny
*/
public class KalkulatorModel
{
int operator=0;
double operand1;
double operand2;
double result;
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;
}
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);
}
}
5. Membuat kelas untuk mengimpor
Source code :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package kalkulatormvc;
/**
*
* @author DesDesianny
*/
import kalkulatormvc.TampilanKalkulator;
public class KalkulatorMVC {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new TampilanKalkulator().setVisible(true);
}
}
Tidak ada komentar:
Posting Komentar