Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calculadora;
- import java.util.Scanner;
- /*
- * @author Alcântara
- */
- public class CalculadoraJFrame extends javax.swing.JFrame {
- double a3;
- float addition,
- subtraction,
- multiplication,
- division,
- square,
- math,
- pot,
- a1,
- a2;
- int count,
- one = 0;
- Scanner ler = new Scanner(System.in);
- public CalculadoraJFrame() {
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jPanelCalculadora = new javax.swing.JPanel();
- B0 = new javax.swing.JButton();
- Result = new javax.swing.JTextField();
- B1 = new javax.swing.JButton();
- B2 = new javax.swing.JButton();
- B3 = new javax.swing.JButton();
- B4 = new javax.swing.JButton();
- B5 = new javax.swing.JButton();
- B6 = new javax.swing.JButton();
- B7 = new javax.swing.JButton();
- B8 = new javax.swing.JButton();
- B9 = new javax.swing.JButton();
- Point = new javax.swing.JButton();
- Plus = new javax.swing.JButton();
- Minus = new javax.swing.JButton();
- Divide = new javax.swing.JButton();
- Multiply = new javax.swing.JButton();
- Clean = new javax.swing.JButton();
- Equal = new javax.swing.JButton();
- Square = new javax.swing.JButton();
- Pot = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- setMaximumSize(new java.awt.Dimension(180, 289));
- setMinimumSize(new java.awt.Dimension(180, 289));
- setPreferredSize(new java.awt.Dimension(180, 289));
- jPanelCalculadora.setMaximumSize(new java.awt.Dimension(286, 289));
- jPanelCalculadora.setMinimumSize(new java.awt.Dimension(286, 289));
- B0.setText("0");
- B0.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- B0ActionPerformed(evt);
- }
- });
- Result.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
- Result.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
- Result.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- ResultActionPerformed(evt);
- }
- });
- B1.setText("1");
- B1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- B1ActionPerformed(evt);
- }
- });
- B2.setText("2");
- B2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- B2ActionPerformed(evt);
- }
- });
- B3.setText("3");
- B3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- B3ActionPerformed(evt);
- }
- });
- B4.setText("4");
- B4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- B4ActionPerformed(evt);
- }
- });
- B5.setText("5");
- B5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- B5ActionPerformed(evt);
- }
- });
- B6.setText("6");
- B6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- B6ActionPerformed(evt);
- }
- });
- B7.setText("7");
- B7.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- B7ActionPerformed(evt);
- }
- });
- B8.setText("8");
- B8.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- B8ActionPerformed(evt);
- }
- });
- B9.setText("9");
- B9.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- B9ActionPerformed(evt);
- }
- });
- Point.setText(".");
- Point.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- PointActionPerformed(evt);
- }
- });
- Plus.setText("+");
- Plus.setPreferredSize(new java.awt.Dimension(30, 15));
- Plus.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- PlusActionPerformed(evt);
- }
- });
- Minus.setText("-");
- Minus.setPreferredSize(new java.awt.Dimension(30, 15));
- Minus.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- MinusActionPerformed(evt);
- }
- });
- Divide.setText("÷");
- Divide.setPreferredSize(new java.awt.Dimension(30, 15));
- Divide.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- DivideActionPerformed(evt);
- }
- });
- Multiply.setText("*");
- Multiply.setPreferredSize(new java.awt.Dimension(30, 15));
- Multiply.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- MultiplyActionPerformed(evt);
- }
- });
- Clean.setBackground(new java.awt.Color(255, 255, 255));
- Clean.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
- Clean.setForeground(new java.awt.Color(0, 153, 51));
- Clean.setText("C");
- Clean.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- CleanActionPerformed(evt);
- }
- });
- Equal.setText("=");
- Equal.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- EqualActionPerformed(evt);
- }
- });
- Square.setText("√");
- Square.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- SquareActionPerformed(evt);
- }
- });
- Pot.setText("x²");
- Pot.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- PotActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanelCalculadoraLayout = new javax.swing.GroupLayout(jPanelCalculadora);
- jPanelCalculadora.setLayout(jPanelCalculadoraLayout);
- jPanelCalculadoraLayout.setHorizontalGroup(
- jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanelCalculadoraLayout.createSequentialGroup()
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanelCalculadoraLayout.createSequentialGroup()
- .addGap(12, 12, 12)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(Plus, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
- .addComponent(Minus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(Divide, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
- .addComponent(Multiply, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanelCalculadoraLayout.createSequentialGroup()
- .addComponent(Pot, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(0, 0, Short.MAX_VALUE))
- .addComponent(Square, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- .addGroup(jPanelCalculadoraLayout.createSequentialGroup()
- .addGap(12, 12, 12)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(B1, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
- .addComponent(B4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(B0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(B7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(B8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(B5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(B2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(Point, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(B6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(B9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(B3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(Clean, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- .addGroup(jPanelCalculadoraLayout.createSequentialGroup()
- .addContainerGap()
- .addComponent(Result))
- .addGroup(jPanelCalculadoraLayout.createSequentialGroup()
- .addContainerGap()
- .addComponent(Equal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- .addGap(112, 112, 112))
- );
- jPanelCalculadoraLayout.setVerticalGroup(
- jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelCalculadoraLayout.createSequentialGroup()
- .addContainerGap()
- .addComponent(Result, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(Plus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(Multiply, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(Square, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(Minus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(Divide, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(Pot))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(B7)
- .addComponent(B8)
- .addComponent(B9))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(B4)
- .addComponent(B5)
- .addComponent(B6))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(B1)
- .addComponent(B2)
- .addComponent(B3))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanelCalculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(B0)
- .addComponent(Point)
- .addComponent(Clean))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(Equal)
- .addGap(18, 18, 18))
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanelCalculadora, javax.swing.GroupLayout.PREFERRED_SIZE, 244, Short.MAX_VALUE)
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanelCalculadora, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- );
- pack();
- }// </editor-fold>
- private void EqualActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- switch (count) {
- case 1: {
- a2 = Float.valueOf(Result.getText());
- addition = a1 + a2;
- Result.setText(String.valueOf(addition));
- count = 0;
- break;
- }
- case 2: {
- a2 = Float.valueOf(Result.getText());
- multiplication = a2 * a1;
- Result.setText(String.valueOf(multiplication));
- count = 0;
- break;
- }
- case 3: {
- a2 = Float.valueOf(Result.getText());
- division = a1 / a2;
- Result.setText(String.valueOf(division));
- count = 0;
- break;
- }
- case 4: {
- a2 = Float.valueOf(Result.getText());
- subtraction = a1 - a2;
- Result.setText(String.valueOf(subtraction));
- count = 0;
- break;
- }
- }
- }
- private void CleanActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText ("");
- }
- private void MultiplyActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- a1 = Float.valueOf(Result.getText());
- Result.setText("");
- count = one + 2;
- }
- private void PlusActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- a1 = Float.valueOf(Result.getText());
- Result.setText("");
- count = one + 1;
- }
- private void B9ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText(Result.getText() + "9");
- }
- private void B8ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText (Result.getText() + "8");
- }
- private void B7ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText (Result.getText() + "7");
- }
- private void B6ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText (Result.getText() + "6");
- }
- private void B5ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText (Result.getText() + "5");
- }
- private void B4ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText (Result.getText() + "4");
- }
- private void B3ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText (Result.getText() + "3");
- }
- private void B2ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText (Result.getText() + "2");
- }
- private void B1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText (Result.getText() + "1");
- }
- private void ResultActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.insets();
- }
- private void DivideActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- a1 = Float.valueOf(Result.getText());
- Result.setText("");
- count = one + 3;
- }
- private void MinusActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- a1 = Float.valueOf(Result.getText());
- Result.setText("");
- count = one + 4;
- }
- private void PointActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText(Result.getText() + ".");
- }
- private void B0ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Result.setText(Result.getText() + "0");
- }
- private void SquareActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- a3 = Float.valueOf(Result.getText());
- square = (float) Math.sqrt(a3);
- Result.setText(String.valueOf(square));
- }
- private void PotActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- a3 = Float.valueOf(Result.getText());
- pot = (float) Math.pow(a3, 2);
- Result.setText(String.valueOf(pot));
- }
- /**
- * @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(CalculadoraJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(CalculadoraJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(CalculadoraJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(CalculadoraJFrame.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 CalculadoraJFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton B0;
- private javax.swing.JButton B1;
- private javax.swing.JButton B2;
- private javax.swing.JButton B3;
- private javax.swing.JButton B4;
- private javax.swing.JButton B5;
- private javax.swing.JButton B6;
- private javax.swing.JButton B7;
- private javax.swing.JButton B8;
- private javax.swing.JButton B9;
- private javax.swing.JButton Clean;
- private javax.swing.JButton Divide;
- private javax.swing.JButton Equal;
- private javax.swing.JButton Minus;
- private javax.swing.JButton Multiply;
- private javax.swing.JButton Plus;
- private javax.swing.JButton Point;
- private javax.swing.JButton Pot;
- private javax.swing.JTextField Result;
- private javax.swing.JButton Square;
- private javax.swing.JPanel jPanelCalculadora;
- // End of variables declaration
- private float pow(float a1, float a21) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- private float pow(float a1, int i) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- private float sqrt(float a1) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement