Вы находитесь на странице: 1из 10

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

java * * Created on Jan 1, 2003, 1:05:56 AM */ /** * * @author Administrator */ public class Calculator1 extends javax.swing.JFrame { /** Creates new form Calculator1 */ //variables double plusminus; double firstDouble; double secondDouble; double totalDouble; double decimalDouble; //to check for button clicks int plusClick; int minusClick; int multiplyClick; int divideClick; int decimalClick; public Calculator1() { 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") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:i nitComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); display = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); plus = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); jButton7 = new javax.swing.JButton(); minus = new javax.swing.JButton(); clear = new javax.swing.JButton(); jButton10 = new javax.swing.JButton(); multiply = new javax.swing.JButton(); jButton12 = new javax.swing.JButton();

jButton13 = new javax.swing.JButton(); jButton14 = new javax.swing.JButton(); decimal = new javax.swing.JButton(); divide = new javax.swing.JButton(); posneg = new javax.swing.JButton(); equals = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swi ng.border.EtchedBorder.RAISED)); display.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N display.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swi ng.border.EtchedBorder.RAISED)); jButton1.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N jButton1.setText("1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N jButton2.setText("3"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N jButton3.setText("4"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); plus.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N plus.setText("+"); plus.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { plusActionPerformed(evt); } }); jButton5.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N jButton5.setText("2"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jButton6.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N jButton6.setText("5"); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton6ActionPerformed(evt); } }); jButton7.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N jButton7.setText("6"); jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton7ActionPerformed(evt); } }); minus.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N minus.setText("-"); minus.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { minusActionPerformed(evt); } }); clear.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N clear.setText("c"); clear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clearActionPerformed(evt); } }); jButton10.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N jButton10.setText("0"); jButton10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton10ActionPerformed(evt); } }); multiply.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N multiply.setText("*"); multiply.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { multiplyActionPerformed(evt); } }); jButton12.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N jButton12.setText("8"); jButton12.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton12ActionPerformed(evt); } }); jButton13.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N jButton13.setText("9"); jButton13.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton13ActionPerformed(evt); } });

jButton14.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N jButton14.setText("7"); jButton14.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton14ActionPerformed(evt); } }); decimal.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N decimal.setText("."); decimal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { decimalActionPerformed(evt); } }); divide.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N divide.setText("/"); divide.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { divideActionPerformed(evt); } }); posneg.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N posneg.setText("+/-"); posneg.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { posnegActionPerformed(evt); } }); equals.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N equals.setText("="); equals.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { equalsActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPan el1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.LEADING) .addComponent(display, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPa nel1Layout.createSequentialGroup() .addComponent(jButton3, javax.swing.GroupLayout.PREF ERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPl acement.RELATED)

.addComponent(jButton6, javax.swing.GroupLayout.PREF ERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPl acement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton7, javax.swing.GroupLayout.PREF ERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPl acement.RELATED) .addComponent(minus, javax.swing.GroupLayout.PREFERR ED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPa nel1Layout.createSequentialGroup() .addComponent(jButton1, javax.swing.GroupLayout.PREF ERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPl acement.RELATED) .addComponent(jButton5, javax.swing.GroupLayout.PREF ERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPl acement.RELATED) .addComponent(jButton2, javax.swing.GroupLayout.PREF ERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPl acement.RELATED) .addComponent(plus, javax.swing.GroupLayout.PREFERRE D_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jButton14, javax.swing.GroupLayout.PREFERR ED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED) .addComponent(jButton12, javax.swing.GroupLayout.PREFERR ED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED) .addComponent(jButton13, javax.swing.GroupLayout.PREFERR ED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED) .addComponent(multiply, javax.swing.GroupLayout.PREFERRE D_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jButton10, javax.swing.GroupLayout .PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.Compone ntPlacement.RELATED) .addComponent(clear, javax.swing.GroupLayout.PRE FERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(10, 10, 10) .addComponent(posneg, javax.swing.GroupLayout.PR EFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem ent.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup()

.addComponent(decimal, javax.swing.GroupLayout.P REFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.Compone ntPlacement.RELATED) .addComponent(divide, javax.swing.GroupLayout.PR EFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(10, 10, 10) .addComponent(equals, javax.swing.GroupLayout.PR EFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 4 5, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE LATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.BASELINE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SI ZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SI ZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SI ZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA TED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.BASELINE) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SI ZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SI ZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SI ZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA TED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.BASELINE) .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_S IZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_S IZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_S IZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SI ZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA TED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.BASELINE) .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_S

IZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZ E, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE , 43, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA TED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLay out.Alignment.LEADING) .addComponent(posneg, javax.swing.GroupLayout.PREFERRED_SIZE , 39, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(equals, javax.swing.GroupLayout.PREFERRED_SIZE , 39, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX _VALUE)) ); 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() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, jav ax.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.DEFAULT_SIZE, jav ax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton1ActionPerformed display.setText(display.getText()+jButton1.getText()); }//GEN-LAST:event_jButton1ActionPerformed private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton5ActionPerformed display.setText(display.getText()+jButton5.getText()); // TODO add your h andling code here: }//GEN-LAST:event_jButton5ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton2ActionPerformed display.setText(display.getText()+jButton2.getText()); // TODO add your ha ndling code here: }//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton3ActionPerformed display.setText(display.getText()+jButton3.getText()); // TODO add your h andling code here: }//GEN-LAST:event_jButton3ActionPerformed private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton6ActionPerformed display.setText(display.getText()+jButton6.getText()); // TODO add your h andling code here: }//GEN-LAST:event_jButton6ActionPerformed private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton7ActionPerformed display.setText(display.getText()+jButton7.getText()); // TODO add your h andling code here: }//GEN-LAST:event_jButton7ActionPerformed private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN -FIRST:event_jButton14ActionPerformed display.setText(display.getText()+jButton14.getText()); // TODO add your handling code here: }//GEN-LAST:event_jButton14ActionPerformed private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN -FIRST:event_jButton12ActionPerformed display.setText(display.getText()+jButton12.getText()); // TODO add your handling code here: }//GEN-LAST:event_jButton12ActionPerformed private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN -FIRST:event_jButton13ActionPerformed display.setText(display.getText()+jButton13.getText()); // TODO add your handling code here: }//GEN-LAST:event_jButton13ActionPerformed private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN -FIRST:event_jButton10ActionPerformed display.setText(display.getText()+jButton10.getText()); // TODO add your handling code here: }//GEN-LAST:event_jButton10ActionPerformed private void clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR ST:event_clearActionPerformed display.setText(""); // TODO add your handling code here: }//GEN-LAST:event_clearActionPerformed private void decimalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-F IRST:event_decimalActionPerformed display.setText(display.getText()+decimal.getText()); //decimalClick=1;// TODO add your handling code here: }//GEN-LAST:event_decimalActionPerformed private void plusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS T:event_plusActionPerformed firstDouble=(Double.parseDouble(String.valueOf(display.getText()))); display.setText(""); plusClick=1; // TODO add your handling code here:

}//GEN-LAST:event_plusActionPerformed private void minusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR ST:event_minusActionPerformed firstDouble=(Double.parseDouble(String.valueOf(display.getText()))); display.setText(""); minusClick=1; // TODO add your handling code here: }//GEN-LAST:event_minusActionPerformed private void multiplyActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_multiplyActionPerformed firstDouble=(Double.parseDouble(String.valueOf(display.getText()))); display.setText(""); multiplyClick=1; // TODO add your handling code here: }//GEN-LAST:event_multiplyActionPerformed private void divideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI RST:event_divideActionPerformed firstDouble=(Double.parseDouble(String.valueOf(display.getText()))); display.setText(""); divideClick=1; // TODO add your handling code here: }//GEN-LAST:event_divideActionPerformed private void equalsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI RST:event_equalsActionPerformed secondDouble=(Double.parseDouble(String.valueOf(display.getText()))); if(plusClick>0) { totalDouble=firstDouble+secondDouble; display.setText((String.valueOf(totalDouble))); firstDouble=0; secondDouble=0; plusClick=0; } else if(minusClick>0) { totalDouble=firstDouble+secondDouble; display.setText((String.valueOf(totalDouble))); firstDouble=0; secondDouble=0; minusClick=0; } else if(multiplyClick>0) { totalDouble=firstDouble+secondDouble; display.setText((String.valueOf(totalDouble))); firstDouble=0; secondDouble=0; multiplyClick=0; } else if(divideClick>0) { totalDouble=firstDouble+secondDouble; display.setText((String.valueOf(totalDouble))); firstDouble=0; secondDouble=0; divideClick=0; } // TODO add your handling code here: }//GEN-LAST:event_equalsActionPerformed

private void posnegActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI RST:event_posnegActionPerformed plusminus=Double.parseDouble(String.valueOf(display.getText())); plusminus=plusminus*(-1); display.setText(String.valueOf(plusminus)); // TODO add your handling code here: }//GEN-LAST:event_posnegActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Calculator1().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton clear; private javax.swing.JButton decimal; private javax.swing.JTextField display; private javax.swing.JButton divide; private javax.swing.JButton equals; private javax.swing.JButton jButton1; private javax.swing.JButton jButton10; private javax.swing.JButton jButton12; private javax.swing.JButton jButton13; private javax.swing.JButton jButton14; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton5; private javax.swing.JButton jButton6; private javax.swing.JButton jButton7; private javax.swing.JPanel jPanel1; private javax.swing.JButton minus; private javax.swing.JButton multiply; private javax.swing.JButton plus; private javax.swing.JButton posneg; // End of variables declaration//GEN-END:variables }

Вам также может понравиться