Sie sind auf Seite 1von 11

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Kalkulator.

java * * Created on May 22, 2012, 11:24:33 AM */ package TugasJavaI; import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.Default; /** * * @author uzhe_kasep/49012039-Ujang Carmana */ public class Kalkulator extends javax.swing.JFrame { String angka; Double Hasil, angka1, angka2; int Operator; /** Creates new form Kalkulator */ public Kalkulator() { initComponents(); angka =""; } /** 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") // <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(); jButton10 = new javax.swing.JButton(); jButton11 = new javax.swing.JButton(); Kali = new javax.swing.JButton(); Bagi = new javax.swing.JButton(); Tambah = new javax.swing.JButton(); Kurang = new javax.swing.JButton(); SamaDengan = new javax.swing.JButton(); Clear = new javax.swing.JButton(); Layar = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Kalkulator by uzhe"); setBackground(new java.awt.Color(102, 204, 255)); jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jButton1.setText("1"); jButton1.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setFont(new java.awt.Font("Tahoma", 1, 11)); jButton2.setText("2"); jButton2.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setFont(new java.awt.Font("Tahoma", 1, 11)); jButton3.setText("3"); jButton3.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setFont(new java.awt.Font("Tahoma", 1, 11)); jButton4.setText("4"); jButton4.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton5.setFont(new java.awt.Font("Tahoma", 1, 11)); jButton5.setText("5"); jButton5.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jButton6.setFont(new java.awt.Font("Tahoma", 1, 11)); jButton6.setText("6"); jButton6.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(evt); } }); jButton7.setFont(new java.awt.Font("Tahoma", 1, 11)); jButton7.setText("7");

jButton7.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton7ActionPerformed(evt); } }); jButton8.setFont(new java.awt.Font("Tahoma", 1, 11)); jButton8.setText("8"); jButton8.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton8ActionPerformed(evt); } }); jButton9.setFont(new java.awt.Font("Tahoma", 1, 11)); jButton9.setText("9"); jButton9.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton9ActionPerformed(evt); } }); jButton10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jButton10.setText("0"); jButton10.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton10ActionPerformed(evt); } }); jButton11.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jButton11.setText("."); jButton11.setMargin(new java.awt.Insets(2, 5, 2, 5)); jButton11.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton11ActionPerformed(evt); } }); jButton11.addPropertyChangeListener(new java.beans.PropertyChangeListene r() { public void propertyChange(java.beans.PropertyChangeEvent evt) { jButton11PropertyChange(evt); } }); Kali.setFont(new java.awt.Font("Tahoma", 1, 11)); Kali.setText("x"); Kali.setMargin(new java.awt.Insets(2, 5, 2, 5)); Kali.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { KaliActionPerformed(evt); } }); Bagi.setFont(new java.awt.Font("Tahoma", 1, 11)); Bagi.setText("/");

Bagi.setMargin(new java.awt.Insets(2, 5, 2, 5)); Bagi.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BagiActionPerformed(evt); } }); Tambah.setFont(new java.awt.Font("Tahoma", 1, 11)); Tambah.setText("+"); Tambah.setMargin(new java.awt.Insets(2, 2, 2, 2)); Tambah.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { TambahActionPerformed(evt); } }); Kurang.setFont(new java.awt.Font("Tahoma", 1, 11)); Kurang.setText("-"); Kurang.setMargin(new java.awt.Insets(2, 5, 2, 5)); Kurang.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { KurangActionPerformed(evt); } }); SamaDengan.setFont(new java.awt.Font("Tahoma", 1, 11)); SamaDengan.setText("="); SamaDengan.setMargin(new java.awt.Insets(2, 10, 2, 10)); SamaDengan.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SamaDenganActionPerformed(evt); } }); Clear.setFont(new java.awt.Font("Tahoma", 1, 11)); Clear.setText("Clear"); Clear.setMargin(new java.awt.Insets(2, 10, 2, 10)); Clear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ClearActionPerformed(evt); } }); Layar.setFont(new java.awt.Font("Tahoma", 1, 12)); Layar.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); Layar.setText("0"); Layar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt. Color(0, 0, 0))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentP ane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(layout.createSequentialGroup() .addGap(22, 22, 22) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.

createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLa yout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton7, javax.swing.GroupLayout. PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.Compone ntPlacement.UNRELATED) .addComponent(jButton8, javax.swing.GroupLayout. PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jButton10, javax.swing.GroupLayout.PRE FERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLa yout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(jButton9, javax.swing.GroupLayout. PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.Compone ntPlacement.UNRELATED) .addComponent(Tambah, javax.swing.GroupLayout.PR EFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.Compone ntPlacement.UNRELATED) .addComponent(Kurang, javax.swing.GroupLayout.PR EFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jButton11, javax.swing.GroupLayout .PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.Compone ntPlacement.UNRELATED) .addComponent(SamaDengan, javax.swing.GroupLayou t.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout. createSequentialGroup() .addComponent(jButton1, javax.swing.GroupLayout.PREFERRE D_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.UNRELATED) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRE D_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.UNRELATED) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRE D_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.UNRELATED) .addComponent(Clear, javax.swing.GroupLayout.PREFERRED_S IZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout. createSequentialGroup() .addComponent(jButton4, javax.swing.GroupLayout.PREFERRE D_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.UNRELATED) .addComponent(jButton5, javax.swing.GroupLayout.PREFERRE D_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.UNRELATED)

.addComponent(jButton6, javax.swing.GroupLayout.PREFERRE D_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.UNRELATED) .addComponent(Kali, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.UNRELATED) .addComponent(Bagi, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(Layar, javax.swing.GroupLayout.Alignment.LEADI NG, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)) .addGap(23, 23, 23)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(layout.createSequentialGroup() .addGap(28, 28, 28) .addComponent(Layar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.BASELINE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Clear, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.BASELINE) .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Kali, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Bagi, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE LATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.BASELINE) .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SI ZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Tambah, javax.swing.GroupLayout.PREFERRED_SIZE , 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Kurang, javax.swing.GroupLayout.PREFERRED_SIZE , 45, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE

LATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.BASELINE) .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_S IZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_S IZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(SamaDengan, javax.swing.GroupLayout.PREFERRED_ SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(42, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka += "4"; Layar.setText(angka); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka += "1"; Layar.setText(angka); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka += "2"; Layar.setText(angka); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka += "3"; Layar.setText(angka); } private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka += "5"; Layar.setText(angka); } private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka += "6"; Layar.setText(angka); } private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:

angka += "7"; Layar.setText(angka); } private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka += "8"; Layar.setText(angka); } private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka += "9"; Layar.setText(angka); } private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka += "0"; Layar.setText(angka); } private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka += "."; Layar.setText(angka); } private void ClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka=""; Layar.setText("0"); } private void KaliActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka1 = Double.parseDouble(angka); Layar.setText("x"); angka = ""; Operator = 1; } private void BagiActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka1 = Double.parseDouble(angka); Layar.setText("/"); angka = ""; Operator = 2; } private void TambahActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here: angka1 = Double.parseDouble(angka); Layar.setText("+"); angka = ""; Operator = 3; } private void KurangActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: angka1 = Double.parseDouble(angka); Layar.setText("-"); angka = ""; Operator = 4; } private void SamaDenganActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: switch (Operator){ case 1: angka2 = Double.parseDouble(angka); Hasil = angka1 * angka2; angka=Double.toString(Hasil); Layar.setText(angka); break; case 2: angka2 = Double.parseDouble(angka); Hasil = angka1 / angka2; angka=Double.toString(Hasil); Layar.setText(angka); break; case 3: angka2 = Double.parseDouble(angka); Hasil = angka1 + angka2; angka=Double.toString(Hasil); Layar.setText(angka); break; case 4: angka2 = Double.parseDouble(angka); Hasil = angka1 - angka2; angka=Double.toString(Hasil); Layar.setText(angka); break; default: break; } } private void jButton11PropertyChange(java.beans.PropertyChangeEvent evt) { // TODO add your handling code here: } /** * @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 cod e (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the d efault look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lo okandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIMana ger.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(j ava.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(j ava.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(j ava.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(j ava.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Kalkulator().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton Bagi; private javax.swing.JButton Clear; private javax.swing.JButton Kali; private javax.swing.JButton Kurang; private javax.swing.JLabel Layar; private javax.swing.JButton SamaDengan; private javax.swing.JButton Tambah; private javax.swing.JButton jButton1; private javax.swing.JButton jButton10; private javax.swing.JButton jButton11; 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;

// End of variables declaration }

Das könnte Ihnen auch gefallen