Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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.
- */
- /**
- *
- * @author lenovo
- */
- public class calculatordesign extends javax.swing.JFrame {
- double firstnum;
- double secondnum;
- double result=0;
- String operate="";
- String answer="";
- String takein="";
- int offon=0;
- public calculatordesign() {
- 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">
- private void initComponents() {
- jButton1 = new javax.swing.JButton();
- key1 = new javax.swing.JButton();
- key2 = new javax.swing.JButton();
- key3 = new javax.swing.JButton();
- key4 = new javax.swing.JButton();
- key5 = new javax.swing.JButton();
- key6 = new javax.swing.JButton();
- key7 = new javax.swing.JButton();
- key8 = new javax.swing.JButton();
- key9 = new javax.swing.JButton();
- key0 = new javax.swing.JButton();
- plussign = new javax.swing.JButton();
- minussign = new javax.swing.JButton();
- multisign = new javax.swing.JButton();
- divsign = new javax.swing.JButton();
- modsign = new javax.swing.JButton();
- txtdisplay = new javax.swing.JTextField();
- equalsign = new javax.swing.JButton();
- dotsign = new javax.swing.JButton();
- clearsign = new javax.swing.JButton();
- onbutton = new javax.swing.JButton();
- offbutton = new javax.swing.JButton();
- jButton1.setText("jButton1");
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- key1.setText("1");
- key1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- key1ActionPerformed(evt);
- }
- });
- key2.setText("2");
- key2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- key2ActionPerformed(evt);
- }
- });
- key3.setText("3");
- key3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- key3ActionPerformed(evt);
- }
- });
- key4.setText("4");
- key4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- key4ActionPerformed(evt);
- }
- });
- key5.setText("5");
- key5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- key5ActionPerformed(evt);
- }
- });
- key6.setText("6");
- key6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- key6ActionPerformed(evt);
- }
- });
- key7.setText("7");
- key7.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- key7ActionPerformed(evt);
- }
- });
- key8.setText("8");
- key8.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- key8ActionPerformed(evt);
- }
- });
- key9.setText("9");
- key9.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- key9ActionPerformed(evt);
- }
- });
- key0.setText("0");
- key0.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- key0ActionPerformed(evt);
- }
- });
- plussign.setText("+");
- plussign.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- plussignActionPerformed(evt);
- }
- });
- minussign.setText("-");
- minussign.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- minussignActionPerformed(evt);
- }
- });
- multisign.setText("*");
- multisign.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- multisignActionPerformed(evt);
- }
- });
- divsign.setText("/");
- divsign.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- divsignActionPerformed(evt);
- }
- });
- modsign.setText("%");
- modsign.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- modsignActionPerformed(evt);
- }
- });
- txtdisplay.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- txtdisplayActionPerformed(evt);
- }
- });
- equalsign.setText("=");
- equalsign.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- equalsignActionPerformed(evt);
- }
- });
- dotsign.setText(".");
- dotsign.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- dotsignActionPerformed(evt);
- }
- });
- clearsign.setText("C");
- clearsign.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- clearsignActionPerformed(evt);
- }
- });
- onbutton.setText("ON");
- onbutton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- onbuttonActionPerformed(evt);
- }
- });
- offbutton.setText("OFF");
- offbutton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- offbuttonActionPerformed(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()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(24, 24, 24)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(key7)
- .addComponent(key1)
- .addComponent(key4)
- .addComponent(key0))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(key8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(dotsign, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(key9)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(multisign)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(offbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(equalsign)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(divsign)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(modsign))))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(key5)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(key6)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(minussign))
- .addGroup(layout.createSequentialGroup()
- .addComponent(key2)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(key3)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(plussign)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(onbutton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(clearsign, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(txtdisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(13, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(21, 21, 21)
- .addComponent(txtdisplay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(39, 39, 39)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(key1)
- .addComponent(key2)
- .addComponent(key3)
- .addComponent(plussign)
- .addComponent(clearsign))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(key6)
- .addComponent(minussign)
- .addComponent(onbutton))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(offbutton))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(key5)
- .addComponent(key4))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(key7)
- .addComponent(key8)
- .addComponent(key9)
- .addComponent(multisign))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(key0)
- .addComponent(equalsign)
- .addComponent(divsign)
- .addComponent(dotsign)
- .addComponent(modsign))))
- .addContainerGap(30, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void txtdisplayActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void key8ActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + key8.getText();
- txtdisplay.setText(takein);}
- }
- private void key1ActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + key1.getText();
- txtdisplay.setText(takein);}
- }
- private void key2ActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + key2.getText();
- txtdisplay.setText(takein);}
- }
- private void key3ActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + key3.getText();
- txtdisplay.setText(takein);}
- }
- private void key4ActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + key4.getText();
- txtdisplay.setText(takein);}
- }
- private void key5ActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + key5.getText();
- txtdisplay.setText(takein);}
- }
- private void key6ActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + key6.getText();
- txtdisplay.setText(takein);}
- }
- private void key7ActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + key7.getText();
- txtdisplay.setText(takein);}
- }
- private void key9ActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + key9.getText();
- txtdisplay.setText(takein);}
- }
- private void key0ActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + key0.getText();
- txtdisplay.setText(takein);}
- }
- private void dotsignActionPerformed(java.awt.event.ActionEvent evt) {
- if(offon==1){
- takein = txtdisplay.getText() + dotsign.getText();
- txtdisplay.setText(takein);}
- }
- private void clearsignActionPerformed(java.awt.event.ActionEvent evt) {
- txtdisplay.setText("");
- firstnum = 0;
- secondnum = 0;
- }
- private void plussignActionPerformed(java.awt.event.ActionEvent evt) {
- firstnum = Double.parseDouble(txtdisplay.getText());
- txtdisplay.setText("");
- operate = "+";
- }
- private void minussignActionPerformed(java.awt.event.ActionEvent evt) {
- firstnum = Double.parseDouble(txtdisplay.getText());
- txtdisplay.setText("");
- operate = "-";
- }
- private void multisignActionPerformed(java.awt.event.ActionEvent evt) {
- firstnum = Double.parseDouble(txtdisplay.getText());
- txtdisplay.setText("");
- operate = "*";
- }
- private void divsignActionPerformed(java.awt.event.ActionEvent evt) {
- firstnum = Double.parseDouble(txtdisplay.getText());
- txtdisplay.setText("");
- operate = "/";
- }
- private void modsignActionPerformed(java.awt.event.ActionEvent evt) {
- firstnum = Double.parseDouble(txtdisplay.getText());
- txtdisplay.setText("");
- operate = "%";
- }
- private void equalsignActionPerformed(java.awt.event.ActionEvent evt) {
- secondnum = Double.parseDouble(txtdisplay.getText());
- if(operate == "+"){
- result = firstnum + secondnum;
- answer = String.format("%.0f", result);
- txtdisplay.setText(answer);
- answer="";
- takein="";
- result=0;
- }
- else if(operate == "-"){
- result = firstnum - secondnum;
- answer = String.format("%.0f", result);
- txtdisplay.setText(answer);
- answer="";
- takein="";
- result=0;
- }
- else if(operate == "*"){
- result = firstnum * secondnum;
- answer = String.format("%.0f", result);
- txtdisplay.setText(answer);
- answer="";
- takein="";
- result=0;
- }
- else if(operate == "/"){
- result = firstnum / secondnum;
- answer = String.format("%.0f", result);
- txtdisplay.setText(answer);
- answer="";
- takein="";
- result=0;
- }
- else if(operate == "%"){
- result = firstnum % secondnum;
- answer = String.format("%.0f", result);
- txtdisplay.setText(answer);
- answer="";
- takein="";
- result=0;
- }
- }
- private void onbuttonActionPerformed(java.awt.event.ActionEvent evt) {
- txtdisplay.setText("0");
- offon=1;
- takein="";
- }
- private void offbuttonActionPerformed(java.awt.event.ActionEvent evt) {
- txtdisplay.setText("");
- takein="";
- offon=0;
- }
- /**
- * @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(calculatordesign.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(calculatordesign.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(calculatordesign.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(calculatordesign.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 calculatordesign().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton clearsign;
- private javax.swing.JButton divsign;
- private javax.swing.JButton dotsign;
- private javax.swing.JButton equalsign;
- private javax.swing.JButton jButton1;
- private javax.swing.JButton key0;
- private javax.swing.JButton key1;
- private javax.swing.JButton key2;
- private javax.swing.JButton key3;
- private javax.swing.JButton key4;
- private javax.swing.JButton key5;
- private javax.swing.JButton key6;
- private javax.swing.JButton key7;
- private javax.swing.JButton key8;
- private javax.swing.JButton key9;
- private javax.swing.JButton minussign;
- private javax.swing.JButton modsign;
- private javax.swing.JButton multisign;
- private javax.swing.JButton offbutton;
- private javax.swing.JButton onbutton;
- private javax.swing.JButton plussign;
- private javax.swing.JTextField txtdisplay;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement