daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class MyPanel extends JPanel {
  6.  
  7.     private JTextField txtVal1, txtVal2, txtResult;
  8.     private JLabel lblVal1, lblVal2, lblResult;
  9.     private JButton btnCalcular, btnLimpar;
  10.  
  11.     public MyPanel() {
  12.         this.setLayout(new GridLayout(4, 2, 5, 5));
  13.  
  14.         lblVal1 = new JLabel(" Valor 1");
  15.         this.add(lblVal1);
  16.         txtVal1 = new JTextField();
  17.         this.add(txtVal1);
  18.  
  19.         lblVal2 = new JLabel(" Valor 2");
  20.         this.add(lblVal2);
  21.         txtVal2 = new JTextField();
  22.         this.add(txtVal2);
  23.  
  24.         btnLimpar = new JButton("Limpar");
  25.         btnLimpar.addActionListener(new ActionListener() {
  26.             @Override
  27.             public void actionPerformed(ActionEvent event) {
  28.                 txtVal1.setText("");
  29.                 txtVal2.setText("");
  30.                 txtResult.setText("");
  31.             }
  32.         });
  33.         this.add(btnLimpar);
  34.  
  35.         btnCalcular = new JButton("Calcular");
  36.         btnCalcular.addActionListener(new ActionListener() {
  37.             @Override
  38.             public void actionPerformed(ActionEvent event) {
  39.                 CalculaMedia();
  40.             }
  41.         });
  42.         this.add(btnCalcular);
  43.  
  44.         lblResult = new JLabel(" Resultado");
  45.         this.add(lblResult);
  46.         txtResult = new JTextField();
  47.         this.add(txtResult);
  48.  
  49.     }
  50.  
  51.     public void CalculaMedia() {
  52.         double val1, val2, result;
  53.         val1 = Double.parseDouble(txtVal1.getText());
  54.         val2 = Double.parseDouble(txtVal2.getText());
  55.         result = (val1 + val2) / 2.0;
  56.         txtResult.setText((new Double(result)).toString());
  57.  
  58.     }
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top