Minggu, 04 Mei 2014

Membuat Calculator Menggunakna Java Swing

/*
* DesainKalkulator.java
*
* Created on 24 Mei 2012, 4:07
*/
 
package kalkulator;
 
/**
*
* @author  Witty
*/
public class DesainKalkulator extends javax.swing.JFrame {
double tot, nilai1, nilai2;
String a, b;
int pil;
private Object Int;
 
/** Creates new form DesainKalkulator */
public DesainKalkulator() {
initComponents();
a="";
}
 
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
 
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
 
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Kalkulator Sederhana (Siti Witty Ariyanti)");
setBackground(getBackground());
setResizable(false);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
 
jButton1.setLabel("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 130, -1, -1));
 
jButton2.setLabel("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 130, -1, -1));
 
jButton3.setLabel("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 130, -1, -1));
 
jButton4.setLabel("4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 170, -1, -1));
 
jButton5.setLabel("5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 170, -1, -1));
 
jButton6.setLabel("6");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
getContentPane().add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 170, -1, -1));
 
jButton7.setLabel("7");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
getContentPane().add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 210, -1, -1));
 
jButton8.setLabel("8");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
getContentPane().add(jButton8, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 210, -1, -1));
 
jButton9.setLabel("9");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
getContentPane().add(jButton9, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 210, -1, -1));
 
jButton11.setLabel("0");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
getContentPane().add(jButton11, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 250, -1, -1));
 
jButton12.setLabel("Clear");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
getContentPane().add(jButton12, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 250, 90, -1));
 
jButton13.setLabel("+");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
getContentPane().add(jButton13, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 130, -1, -1));
 
jButton14.setLabel("-");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});
getContentPane().add(jButton14, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 130, -1, -1));
 
jButton15.setLabel("*");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton15ActionPerformed(evt);
}
});
getContentPane().add(jButton15, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 170, -1, -1));
 
jButton16.setLabel("/");
jButton16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton16ActionPerformed(evt);
}
});
getContentPane().add(jButton16, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 170, -1, -1));
 
jButton17.setLabel("=");
jButton17.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton17ActionPerformed(evt);
}
});
getContentPane().add(jButton17, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 210, 100, 60));
 
jTextField1.setEditable(false);
jTextField1.setFont(new java.awt.Font("Comic Sans MS", 1, 24));
jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jTextField1.setText("0");
jTextField1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 204, 204), 3));
jTextField1.setFocusCycleRoot(true);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 60, 360, -1));
jTextField1.getAccessibleContext().setAccessibleName("");
 
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 8));
jLabel1.setText("By : Siti Witty Ariyanti");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 320, -1, -1));
 
jLabel2.setIcon(new javax.swing.ImageIcon("D:\\ITB\\BAHAN KULIAH\\8. JAVA2SE\\netbeans project\\Kalkulator\\src\\kalkulator\\nomor.jpg")); // NOI18N
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 490, 360));
 
pack();
}// </editor-fold>
 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
a="1";
jTextField1.setText(a);
}
 
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
a="2";
jTextField1.setText(a);
}
 
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
a="3";
jTextField1.setText(a);
}
 
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
a="4";
jTextField1.setText(a);
}
 
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
a="5";
jTextField1.setText(a);
}
 
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
a="6";
jTextField1.setText(a);
}
 
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
a="7";
jTextField1.setText(a);
}
 
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
a="8";
jTextField1.setText(a);
}
 
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
a="9";
jTextField1.setText(a);
}
 
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
a="0";
jTextField1.setText(a);
}
 
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("0");
nilai1=0;
nilai2=0;
tot=0;
a="";
}
 
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
nilai1=Double.parseDouble(a);
jTextField1.setText("+");
a="";
pil=1;
}
 
private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
nilai1=Double.parseDouble(a);
jTextField1.setText("-");
a="";
pil=2;
}
 
private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
nilai1=Double.parseDouble(a);
jTextField1.setText("*");
a="";
pil=3;
}
 
private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
nilai1=Double.parseDouble(a);
jTextField1.setText("/");
a="";
pil=4;
}
 
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
 
private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
switch(pil){
case 1:
nilai2=Double.parseDouble(a);
tot=nilai1+nilai2;
a=Double.toString(tot);
jTextField1.setText(a);
break;
case 2:
nilai2=Double.parseDouble(a);
tot=nilai1-nilai2;
a=Double.toString(tot);
jTextField1.setText(a);
break;
case 3:
nilai2=Double.parseDouble(a);
tot=nilai1*nilai2;
a=Double.toString(tot);
jTextField1.setText(a);
break;
case 4:
nilai2=Double.parseDouble(a);
tot=nilai1/nilai2;
a=Double.toString(tot);
jTextField1.setText(a);
break;
}
}
 
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
 

Tidak ada komentar:

Posting Komentar