Advertisement
Guest User

JavaCalculator

a guest
Sep 16th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.82 KB | None | 0 0
  1. public class Calc extends javax.swing.JFrame {
  2.  
  3.     private int op1 = 1;
  4.     private int op2 = 1;
  5.     private double num1;
  6.     private double num2;
  7.     public Calc() {
  8.         initComponents();
  9.     }
  10.  
  11.     /**
  12.      * This method is called from within the constructor to initialize the form.
  13.      * WARNING: Do NOT modify this code. The content of this method is always
  14.      * regenerated by the Form Editor.
  15.      */
  16.     @SuppressWarnings("unchecked")
  17.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  18.     private void initComponents() {
  19.  
  20.         buttonGroup1 = new javax.swing.ButtonGroup();
  21.         buttonGroup2 = new javax.swing.ButtonGroup();
  22.         jLabel1 = new javax.swing.JLabel();
  23.         jLabel2 = new javax.swing.JLabel();
  24.         jLabel3 = new javax.swing.JLabel();
  25.         jLabel4 = new javax.swing.JLabel();
  26.         jLabel5 = new javax.swing.JLabel();
  27.         jRadioButton1 = new javax.swing.JRadioButton();
  28.         jRadioButton2 = new javax.swing.JRadioButton();
  29.         jRadioButton3 = new javax.swing.JRadioButton();
  30.         jRadioButton4 = new javax.swing.JRadioButton();
  31.         jRadioButton5 = new javax.swing.JRadioButton();
  32.         jRadioButton6 = new javax.swing.JRadioButton();
  33.         jButton1 = new javax.swing.JButton();
  34.         textField1 = new java.awt.TextField();
  35.         textField2 = new java.awt.TextField();
  36.  
  37.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  38.  
  39.         jLabel1.setText("Digite o valor da temperatura, sua unidade, e a unidade da temperatura convertida ");
  40.  
  41.         jLabel2.setText("Temperatura a");
  42.  
  43.         jLabel3.setText("Temperatura");
  44.  
  45.         jLabel4.setText("ser convertida");
  46.  
  47.         jLabel5.setText("convertida");
  48.  
  49.         buttonGroup1.add(jRadioButton1);
  50.         jRadioButton1.setSelected(true);
  51.         jRadioButton1.setText("Celcius");
  52.         jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
  53.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  54.                 jRadioButton1ActionPerformed(evt);
  55.             }
  56.         });
  57.  
  58.         buttonGroup1.add(jRadioButton2);
  59.         jRadioButton2.setText("Fahrenhiet");
  60.         jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
  61.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  62.                 jRadioButton2ActionPerformed(evt);
  63.             }
  64.         });
  65.  
  66.         buttonGroup1.add(jRadioButton3);
  67.         jRadioButton3.setText("Kelvin");
  68.         jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
  69.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  70.                 jRadioButton3ActionPerformed(evt);
  71.             }
  72.         });
  73.  
  74.         buttonGroup2.add(jRadioButton4);
  75.         jRadioButton4.setSelected(true);
  76.         jRadioButton4.setText("Celcius");
  77.         jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
  78.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  79.                 jRadioButton4ActionPerformed(evt);
  80.             }
  81.         });
  82.  
  83.         buttonGroup2.add(jRadioButton5);
  84.         jRadioButton5.setText("Fahrenheit");
  85.         jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
  86.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  87.                 jRadioButton5ActionPerformed(evt);
  88.             }
  89.         });
  90.  
  91.         buttonGroup2.add(jRadioButton6);
  92.         jRadioButton6.setText("Kelvin");
  93.         jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
  94.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  95.                 jRadioButton6ActionPerformed(evt);
  96.             }
  97.         });
  98.  
  99.         jButton1.setText("Calcular");
  100.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  101.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  102.                 jButton1ActionPerformed(evt);
  103.             }
  104.         });
  105.  
  106.         textField1.addActionListener(new java.awt.event.ActionListener() {
  107.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  108.                 textField1ActionPerformed(evt);
  109.             }
  110.         });
  111.  
  112.         textField2.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
  113.         textField2.setEditable(false);
  114.         textField2.addActionListener(new java.awt.event.ActionListener() {
  115.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  116.                 textField2ActionPerformed(evt);
  117.             }
  118.         });
  119.  
  120.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  121.         getContentPane().setLayout(layout);
  122.         layout.setHorizontalGroup(
  123.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  124.             .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  125.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  126.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  127.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  128.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  129.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  130.                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  131.                                 .addComponent(jLabel4)
  132.                                 .addComponent(jLabel2))
  133.                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  134.                                 .addComponent(jLabel5)
  135.                                 .addComponent(jLabel3)))
  136.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  137.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  138.                             .addComponent(textField1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
  139.                             .addComponent(textField2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  140.                         .addGap(137, 137, 137)
  141.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  142.                             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  143.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  144.                                     .addComponent(jRadioButton3)
  145.                                     .addComponent(jRadioButton2)
  146.                                     .addComponent(jRadioButton1))
  147.                                 .addGap(33, 33, 33))
  148.                             .addGroup(layout.createSequentialGroup()
  149.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  150.                                     .addComponent(jRadioButton6)
  151.                                     .addComponent(jRadioButton5)
  152.                                     .addComponent(jRadioButton4))
  153.                                 .addContainerGap())))
  154.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  155.                         .addComponent(jButton1)
  156.                         .addGap(47, 47, 47))))
  157.         );
  158.         layout.setVerticalGroup(
  159.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  160.             .addGroup(layout.createSequentialGroup()
  161.                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  162.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  163.                     .addGroup(layout.createSequentialGroup()
  164.                         .addGap(61, 61, 61)
  165.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  166.                             .addGroup(layout.createSequentialGroup()
  167.                                 .addComponent(jRadioButton1)
  168.                                 .addGap(8, 8, 8)
  169.                                 .addComponent(jRadioButton2))
  170.                             .addGroup(layout.createSequentialGroup()
  171.                                 .addComponent(jLabel2)
  172.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  173.                                 .addComponent(jLabel4))))
  174.                     .addGroup(layout.createSequentialGroup()
  175.                         .addGap(59, 59, 59)
  176.                         .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
  177.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  178.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  179.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  180.                         .addComponent(jLabel3)
  181.                         .addGap(12, 12, 12)
  182.                         .addComponent(jLabel5)
  183.                         .addGap(61, 61, 61))
  184.                     .addGroup(layout.createSequentialGroup()
  185.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  186.                         .addComponent(jRadioButton3)
  187.                         .addGap(34, 34, 34)
  188.                         .addComponent(jButton1)
  189.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
  190.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  191.                             .addGroup(layout.createSequentialGroup()
  192.                                 .addComponent(jRadioButton4)
  193.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
  194.                             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  195.                                 .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
  196.                                 .addGap(2, 2, 2)))
  197.                         .addComponent(jRadioButton5)
  198.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  199.                         .addComponent(jRadioButton6)
  200.                         .addGap(20, 20, 20))))
  201.         );
  202.  
  203.         pack();
  204.     }// </editor-fold>                        
  205.  
  206.     private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
  207.         op1 = 1;
  208.     }                                            
  209.  
  210.     private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                              
  211.         op1 = 2;
  212.     }                                            
  213.  
  214.     private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                              
  215.         op2 = 1;
  216.     }                                            
  217.  
  218.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  219.         calcula();
  220.     }                                        
  221.  
  222.     private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                              
  223.         op2 = 2;
  224.     }                                            
  225.  
  226.     private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                              
  227.         op2 = 3;
  228.     }                                            
  229.  
  230.     private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                              
  231.         op1 = 3;
  232.     }                                            
  233.  
  234.     private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  235.         num1 = Double.parseDouble(textField1.getText());
  236.     }                                          
  237.  
  238.     private void textField2ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  239.         // TODO add your handling code here:
  240.     }                                          
  241.  
  242.     private void calcula(){
  243.         if (op1 == 1 && op2 == 1){
  244.           num2 = num1;
  245.           textField2.setText(""+num2);
  246.         }
  247.         else if (op1 == 1 && op2 == 2){
  248.           num2 = (((num1*9)/5) + 32);
  249.           textField2.setText(""+num2);  
  250.         }
  251.         else if (op1 == 1 && op2 == 3){
  252.           num2 = 273.15 + num1;
  253.           textField2.setText(""+num2);
  254.         }
  255.         else if (op1 == 2 && op2 == 1){
  256.           num2 = (((num1 - 32) * 5)/9);
  257.           textField2.setText(""+num2);
  258.         }
  259.         else if (op1 == 2 && op2 == 2){
  260.           num2 = num1;
  261.           textField2.setText(""+num2);  
  262.         }
  263.         else if (op1 == 2 && op2 == 3){
  264.           num2 = (((num1 - 32) * 5)/9 + 273.15);
  265.           textField2.setText(""+num2);  
  266.         }
  267.         else if (op1 == 3 && op2 == 1){
  268.           num2 = -273.15 + num1;
  269.           textField2.setText(""+num2);  
  270.         }
  271.         else if (op1 == 3 && op2 == 2){
  272.             num2 = (((num1 - 273.15) * 9)/5 + 32);
  273.           textField2.setText(""+num2);  
  274.         }
  275.         else if (op1 == 3 && op2 == 3){
  276.           num2 = num1;
  277.           textField2.setText(""+num2);  
  278.         }
  279.     }
  280.     public static void main(String args[]) {
  281.         java.awt.EventQueue.invokeLater(new Runnable() {
  282.             public void run() {
  283.                 new Calc().setVisible(true);
  284.             }
  285.         });
  286.     }
  287.  
  288.     // Variables declaration - do not modify                    
  289.     private javax.swing.ButtonGroup buttonGroup1;
  290.     private javax.swing.ButtonGroup buttonGroup2;
  291.     private javax.swing.JButton jButton1;
  292.     private javax.swing.JLabel jLabel1;
  293.     private javax.swing.JLabel jLabel2;
  294.     private javax.swing.JLabel jLabel3;
  295.     private javax.swing.JLabel jLabel4;
  296.     private javax.swing.JLabel jLabel5;
  297.     private javax.swing.JRadioButton jRadioButton1;
  298.     private javax.swing.JRadioButton jRadioButton2;
  299.     private javax.swing.JRadioButton jRadioButton3;
  300.     private javax.swing.JRadioButton jRadioButton4;
  301.     private javax.swing.JRadioButton jRadioButton5;
  302.     private javax.swing.JRadioButton jRadioButton6;
  303.     private java.awt.TextField textField1;
  304.     private java.awt.TextField textField2;
  305.     // End of variables declaration                  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement