marcelofoxes

Untitled

May 12th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. // https://www.facebook.com/groups/JavaBr/permalink/13271875172
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. public class Exemplo0505 extends JFrame implements ActionListener {
  8.     private static final long serialVersionUID = 1L;
  9.     JLabel L1, L2, L3;
  10.     JButton B1, B2, B3, B4, B5;
  11.     JTextField T1, T2, T3;
  12.  
  13.     public static void main(String args[]) {
  14.         JFrame Janela = new Exemplo0505();
  15.         Janela.setVisible(true);
  16.         WindowListener x = new WindowAdapter() {
  17.             // você precisa sobrescrever algum método
  18.             // System.exit(0);
  19.         };
  20.  
  21.         Janela.addWindowListener(x);
  22.  
  23.     }
  24.  
  25.     Exemplo0505() {
  26.         setTitle("calculadora");
  27.         setSize(350, 90);
  28.         setLocation(50, 50);
  29.  
  30.         getContentPane().setBackground(new Color(150, 150, 150));
  31.         getContentPane().setLayout(new GridLayout(3, 4));
  32.  
  33.         L1 = new JLabel("num1");
  34.         L1.setForeground(Color.black);
  35.         L2 = new JLabel("num2");
  36.         L2.setForeground(Color.black);
  37.         L2.setFont(new Font("", Font.BOLD, 14));
  38.         L3 = new JLabel("total");
  39.         L3.setFont(new Font("", Font.BOLD, 14));
  40.  
  41.         B1 = new JButton("+");
  42.         B1.addActionListener(this);
  43.         B2 = new JButton("-");
  44.         B2.addActionListener(this);
  45.         B3 = new JButton("*");
  46.         B3.addActionListener(this);
  47.         B4 = new JButton("/");
  48.         B4.addActionListener(this);
  49.         B5 = new JButton("LImPAR");
  50.         B5.addActionListener(this);
  51.         B5.setBackground(Color.black);
  52.         B5.setForeground(Color.white);
  53.  
  54.         T1 = new JTextField();
  55.         T2 = new JTextField();
  56.         T3 = new JTextField();
  57.  
  58.         getContentPane().add(L1);
  59.         getContentPane().add(T1);
  60.         getContentPane().add(B1);
  61.         getContentPane().add(L2);
  62.         getContentPane().add(T2);
  63.         getContentPane().add(B2);
  64.         getContentPane().add(L3);
  65.         getContentPane().add(T3);
  66.         getContentPane().add(B3);
  67.         getContentPane().add(B4);
  68.         getContentPane().add(B5);
  69.     }
  70.  
  71.     public void actionperformed(ActionEvent e) {
  72.         if (e.getSource() == B5) {
  73.             T1.setText("");
  74.             T2.setText("");
  75.             T3.setText("");
  76.             return;
  77.         }
  78.         Float N1 = 0f, N2 = 0f, result = 0f;
  79.         try {
  80.             N1 = Float.parseFloat(T1.getText());
  81.             N2 = Float.parseFloat(T2.getText());
  82.         } catch (NumberFormatException Error) {
  83.             T3.setText("Erro");
  84.             return;
  85.         }
  86.         if (e.getSource() == B1) {
  87.             result = N1 + N2;
  88.         } else if (e.getSource() == B2) {
  89.             result = N1 - N2;
  90.         } else if (e.getSource() == B3) {
  91.             result = N1 * N2;
  92.         } else if (e.getSource() == B4) {
  93.             result = N1 / N2;
  94.         }
  95.         T3.setText("" + result);
  96.     }
  97.  
  98.     @Override
  99.     public void actionPerformed(ActionEvent e) {
  100.         // faltava implementar esse método, mas pq essa classe é um
  101.         // actionlistener?
  102.  
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment