Калькулятор - Java (241759)

Узнай цену своей работы

Формулировка задачи:

Как сделать программно расчёт что бы потом отображалось так
package project1;

import java.awt.Dimension;
 
import java.awt.Rectangle;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
 
public class Frame1 extends JFrame {
    private JButton jButton1 = new JButton();
    private JButton jButton2 = new JButton();
    private JButton jButton3 = new JButton();
    private JButton jButton4 = new JButton();
    private JButton jButton5 = new JButton();
    private JButton jButton6 = new JButton();
    private JButton jButton7 = new JButton();
    private JButton jButton8 = new JButton();
    private JButton jButton9 = new JButton();
    private JButton jButton10 = new JButton();
    private JButton jButton11 = new JButton();
    private JButton jButton12 = new JButton();
    private JButton jButton13 = new JButton();
    private JButton jButton14 = new JButton();
    private JButton jButton16 = new JButton();
    private JButton jButton17 = new JButton();
    private JButton jButton18 = new JButton();
    private JButton jButton15 = new JButton();
    private JButton jButton19 = new JButton();
    private JEditorPane jEditorPane1 = new JEditorPane();
      int firstValue = 0;
      String operation = "+";

    public Frame1() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void jbInit() throws Exception {
        this.getContentPane().setLayout( null );
        this.setSize(new Dimension(253, 283));
        jButton1.setText("С");
        jButton1.setBounds(new Rectangle(100, 35, 100, 45));
        jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton1_actionPerformed(e);
                }
            });
        jButton2.setText("-");
        jButton2.setBounds(new Rectangle(150, 215, 50, 45));
        jButton3.setText("/");
        jButton3.setBounds(new Rectangle(150, 125, 50, 45));
        jButton4.setText("*");
        jButton4.setBounds(new Rectangle(150, 80, 50, 45));
        jButton5.setText("=");
        jButton5.setBounds(new Rectangle(200, 35, 50, 225));
        jButton5.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton5_actionPerformed(e);
                }
            });
        jButton6.setText("+");
        jButton6.setBounds(new Rectangle(150, 170, 50, 45));
        jButton6.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton6_actionPerformed(e);
                }
            });
        jButton7.setText("1");
        jButton7.setBounds(new Rectangle(0, 80, 50, 45));
        jButton7.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton7_actionPerformed(e);
                }
            });
        jButton8.setText("2");
        jButton8.setBounds(new Rectangle(50, 80, 50, 45));
        jButton8.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton8_actionPerformed(e);
                }
            });
        jButton9.setText("3");
        jButton9.setBounds(new Rectangle(100, 80, 50, 45));
        jButton9.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton9_actionPerformed(e);
                }
            });
        jButton10.setText("4");
        jButton10.setBounds(new Rectangle(0, 125, 50, 45));
        jButton10.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton10_actionPerformed(e);
                }
            });
        jButton11.setText("5");
        jButton11.setBounds(new Rectangle(50, 125, 50, 45));
        jButton11.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton11_actionPerformed(e);
                }
            });
        jButton12.setText("6");
        jButton12.setBounds(new Rectangle(100, 125, 50, 45));
        jButton12.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton12_actionPerformed(e);
                }
            });
        jButton13.setText("7");
        jButton13.setBounds(new Rectangle(0, 170, 50, 45));
        jButton13.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton13_actionPerformed(e);
                }
            });
        jButton14.setText("8");
        jButton14.setBounds(new Rectangle(50, 170, 50, 45));
        jButton14.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton14_actionPerformed(e);
                }
            });
        jButton16.setText("0");
        jButton16.setBounds(new Rectangle(0, 215, 50, 45));
        jButton16.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton16_actionPerformed(e);
                }
            });
        jButton17.setText("00");
        jButton17.setBounds(new Rectangle(50, 215, 50, 45));
        jButton17.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton17_actionPerformed(e);
                }
            });
        jButton18.setText("9");
        jButton18.setBounds(new Rectangle(100, 170, 50, 45));
        jButton18.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton18_actionPerformed(e);
                }
            });
        jButton15.setText(",");
        jButton15.setBounds(new Rectangle(100, 215, 50, 45));
        jButton19.setText("<--");
        jButton19.setBounds(new Rectangle(0, 35, 100, 45));
        jButton19.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton19_actionPerformed(e);
                }
            });
        jEditorPane1.setBounds(new Rectangle(0, 0, 250, 35));
        this.getContentPane().add(jButton7, null);
        this.getContentPane().add(jEditorPane1, null);
        this.getContentPane().add(jButton19, null);
        this.getContentPane().add(jButton15, null);
        this.getContentPane().add(jButton18, null);
        this.getContentPane().add(jButton17, null);
        this.getContentPane().add(jButton16, null);
        this.getContentPane().add(jButton14, null);
        this.getContentPane().add(jButton13, null);
        this.getContentPane().add(jButton12, null);
        this.getContentPane().add(jButton11, null);
        this.getContentPane().add(jButton10, null);
        this.getContentPane().add(jButton9, null);
        this.getContentPane().add(jButton8, null);
        this.getContentPane().add(jButton6, null);
        this.getContentPane().add(jButton5, null);
        this.getContentPane().add(jButton4, null);
        this.getContentPane().add(jButton3, null);
        this.getContentPane().add(jButton2, null);
        this.getContentPane().add(jButton1, null);
    }

    private void jButton7_actionPerformed(ActionEvent e) {      
        
      jEditorPane1.setText(jEditorPane1.getText()+"1");
      }
 
    private void jButton8_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"2");
    }
 
    private void jButton9_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"3");
    }
 
    private void jButton10_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"4");
    }
 
    private void jButton11_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"5");
    }
 
    private void jButton12_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"6");
    }
 
    private void jButton13_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"7");
    }
 
    private void jButton14_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"8");
    }
 
    private void jButton18_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"9");
    }
 
    private void jButton16_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"0");
    }
 
    private void jButton17_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"00");
    }
 
    private void jButton1_actionPerformed(ActionEvent e) {
      jEditorPane1.setText("");
        
    }
 
    private void jButton19_actionPerformed(ActionEvent e) {
      String temp = jEditorPane1.getText();
                      jEditorPane1.setText(temp.substring(0,temp.length()-1));
    }
 
    private void jButton6_actionPerformed(ActionEvent e) {
      
      firstValue = Integer.valueOf(jEditorPane1.getText());
      //jEditorPane1.setText(jEditorPane1.getText()+"+");
                    jEditorPane1.setText("");
                     operation = "+";

    }
 
    private void jButton5_actionPerformed(ActionEvent e) {
                        int secondValue = Integer.valueOf(jEditorPane1.getText());
                                       if("+".equals(operation)){
                                           jEditorPane1.setText((firstValue+secondValue)+"");
                       // jEditorPane1.setText(jEditorPane1.getText()+"=");
                      
                                       }
                      }
    }

Решение задачи: «Калькулятор»

textual
Листинг программы
package project1;
 
 
import java.awt.Dimension;
 
import java.awt.Rectangle;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class Frame1 extends JFrame {
    private JButton jButton1 = new JButton();
    private JButton jButton2 = new JButton();
    private JButton jButton3 = new JButton();
    private JButton jButton4 = new JButton();
    private JButton jButton5 = new JButton();
    private JButton jButton6 = new JButton();
    private JButton jButton7 = new JButton();
    private JButton jButton8 = new JButton();
    private JButton jButton9 = new JButton();
    private JButton jButton10 = new JButton();
    private JButton jButton11 = new JButton();
    private JButton jButton12 = new JButton();
    private JButton jButton13 = new JButton();
    private JButton jButton14 = new JButton();
    private JButton jButton16 = new JButton();
    private JButton jButton17 = new JButton();
    private JButton jButton18 = new JButton();
    private JButton jButton15 = new JButton();
    private JButton jButton19 = new JButton();
    private JEditorPane jEditorPane1 = new JEditorPane();
      int firstValue = 0;
      String operation = "+";
    private JLabel jLabel1 = new JLabel();
 
 
    public Frame1() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void jbInit() throws Exception {
        this.getContentPane().setLayout( null );
        this.setSize(new Dimension(253, 283));
        jButton1.setText("С");
        jButton1.setBounds(new Rectangle(100, 35, 100, 45));
        jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton1_actionPerformed(e);
                }
            });
        jButton2.setText("-");
        jButton2.setBounds(new Rectangle(150, 215, 50, 45));
        jButton3.setText("/");
        jButton3.setBounds(new Rectangle(150, 125, 50, 45));
        jButton4.setText("*");
        jButton4.setBounds(new Rectangle(150, 80, 50, 45));
        jButton5.setText("=");
        jButton5.setBounds(new Rectangle(200, 35, 50, 225));
        jButton5.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton5_actionPerformed(e);
                }
            });
        jButton6.setText("+");
        jButton6.setBounds(new Rectangle(150, 170, 50, 45));
        jButton6.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton6_actionPerformed(e);
                }
            });
        jButton7.setText("1");
        jButton7.setBounds(new Rectangle(0, 80, 50, 45));
        jButton7.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton7_actionPerformed(e);
                }
            });
        jButton8.setText("2");
        jButton8.setBounds(new Rectangle(50, 80, 50, 45));
        jButton8.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton8_actionPerformed(e);
                }
            });
        jButton9.setText("3");
        jButton9.setBounds(new Rectangle(100, 80, 50, 45));
        jButton9.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton9_actionPerformed(e);
                }
            });
        jButton10.setText("4");
        jButton10.setBounds(new Rectangle(0, 125, 50, 45));
        jButton10.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton10_actionPerformed(e);
                }
            });
        jButton11.setText("5");
        jButton11.setBounds(new Rectangle(50, 125, 50, 45));
        jButton11.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton11_actionPerformed(e);
                }
            });
        jButton12.setText("6");
        jButton12.setBounds(new Rectangle(100, 125, 50, 45));
        jButton12.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton12_actionPerformed(e);
                }
            });
        jButton13.setText("7");
        jButton13.setBounds(new Rectangle(0, 170, 50, 45));
        jButton13.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton13_actionPerformed(e);
                }
            });
        jButton14.setText("8");
        jButton14.setBounds(new Rectangle(50, 170, 50, 45));
        jButton14.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton14_actionPerformed(e);
                }
            });
        jButton16.setText("0");
        jButton16.setBounds(new Rectangle(0, 215, 50, 45));
        jButton16.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton16_actionPerformed(e);
                }
            });
        jButton17.setText("00");
        jButton17.setBounds(new Rectangle(50, 215, 50, 45));
        jButton17.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton17_actionPerformed(e);
                }
            });
        jButton18.setText("9");
        jButton18.setBounds(new Rectangle(100, 170, 50, 45));
        jButton18.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton18_actionPerformed(e);
                }
            });
        jButton15.setText(",");
        jButton15.setBounds(new Rectangle(100, 215, 50, 45));
        jButton19.setText("<--");
        jButton19.setBounds(new Rectangle(0, 35, 100, 45));
        jButton19.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton19_actionPerformed(e);
                }
            });
        jEditorPane1.setBounds(new Rectangle(0, 0, 250, 35));
        jLabel1.setBounds(new Rectangle(25, 15, 225, 20));
        this.getContentPane().add(jLabel1, null);
        this.getContentPane().add(jButton7, null);
        this.getContentPane().add(jEditorPane1, null);
        this.getContentPane().add(jButton19, null);
        this.getContentPane().add(jButton15, null);
        this.getContentPane().add(jButton18, null);
        this.getContentPane().add(jButton17, null);
        this.getContentPane().add(jButton16, null);
        this.getContentPane().add(jButton14, null);
        this.getContentPane().add(jButton13, null);
        this.getContentPane().add(jButton12, null);
        this.getContentPane().add(jButton11, null);
        this.getContentPane().add(jButton10, null);
        this.getContentPane().add(jButton9, null);
        this.getContentPane().add(jButton8, null);
        this.getContentPane().add(jButton6, null);
        this.getContentPane().add(jButton5, null);
        this.getContentPane().add(jButton4, null);
        this.getContentPane().add(jButton3, null);
        this.getContentPane().add(jButton2, null);
        this.getContentPane().add(jButton1, null);
    }
 
 
      
    private void jButton7_actionPerformed(ActionEvent e) {      
        
      jEditorPane1.setText(jEditorPane1.getText()+"1");
      jLabel1.setText(jLabel1.getText()+"1");
      }
 
    private void jButton8_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"2");
      jLabel1.setText(jLabel1.getText()+"2");
    }
 
    private void jButton9_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"3");
      jLabel1.setText(jLabel1.getText()+"3");
    }
 
    private void jButton10_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"4");
      jLabel1.setText(jLabel1.getText()+"4");
    }
 
    private void jButton11_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"5");
      jLabel1.setText(jLabel1.getText()+"5");
    }
 
    private void jButton12_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"6");
      jLabel1.setText(jLabel1.getText()+"6");
    }
 
    private void jButton13_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"7");
      jLabel1.setText(jLabel1.getText()+"7");
    }
 
    private void jButton14_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"8");
      jLabel1.setText(jLabel1.getText()+"8");
    }
 
    private void jButton18_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"9");
      jLabel1.setText(jLabel1.getText()+"9");
    }
 
    private void jButton16_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"0");
      jLabel1.setText(jLabel1.getText()+"0");
    }
 
    private void jButton17_actionPerformed(ActionEvent e) {
      jEditorPane1.setText(jEditorPane1.getText()+"00");
      jLabel1.setText(jLabel1.getText()+"00");
    }
 
    private void jButton1_actionPerformed(ActionEvent e) {
      jEditorPane1.setText("");
      jLabel1.setText("");
        
    }
 
    private void jButton19_actionPerformed(ActionEvent e) {
      //String temp = jEditorPane1.getText();
        //              jEditorPane1.setText(temp.substring(0,temp.length()-1));
      String temp = jLabel1.getText();
                      jLabel1.setText(temp.substring(0,temp.length()-1));            
    }
 
    private void jButton6_actionPerformed(ActionEvent e) {
      
      firstValue = Integer.valueOf(jEditorPane1.getText());
      //jEditorPane1.setText(jEditorPane1.getText()+"+");
                    jEditorPane1.setText("");
                     operation = "+";
      jLabel1.setText(jLabel1.getText()+"+");
      
    }
 
    private void jButton5_actionPerformed(ActionEvent e) {
                        int secondValue = Integer.valueOf(jEditorPane1.getText());
                                       if("+".equals(operation)){
                                           int tmpRes = firstValue+secondValue;
                                           String tmpStr = jLabel1.getText();
                                          jLabel1.setText(tmpStr + " = " + String.valueOf(tmpRes) );
                                    
                                               
                                       }
                      }
    }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 4.429 из 5