Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class ProvaRecuperacao extends JFrame implements ActionListener{ //NUMBERFORMATEXCEPTION
- JLabel titulo = new JLabel("CALCULADORA");
- JLabel texto1 = new JLabel("Primeiro valor:");
- JLabel texto2 = new JLabel("Ssegundo valor:");
- JTextField caixa1 = new JTextField();
- JTextField caixa2 = new JTextField();
- JButton soma = new JButton("+");
- JButton subtra = new JButton("-");
- JButton multi = new JButton("*");
- JButton divisao = new JButton("/");
- public ProvaRecuperacao(){
- setSize(300,250);
- setLocation(500,200);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- getContentPane().setLayout(null);
- getContentPane().setBackground(Color.LIGHT_GRAY);
- setResizable(false);
- titulo.setBounds(110, 10, 100, 30);
- getContentPane().add(titulo);
- texto1.setBounds(40,65,100,20);
- getContentPane().add(texto1);
- texto2.setBounds(40,105,100,20);
- getContentPane().add(texto2);
- caixa1.setBounds(130,66,100,20);
- getContentPane().add(caixa1);
- caixa2.setBounds(135,106,100,20);
- getContentPane().add(caixa2);
- soma.setBounds(40,160,45,20);
- getContentPane().add(soma);
- soma.addActionListener(this);
- subtra.setBounds(100,160,45,20);
- getContentPane().add(subtra);
- subtra.addActionListener(this);
- multi.setBounds(160,160,45,20);
- getContentPane().add(multi);
- multi.addActionListener(this);
- divisao.setBounds(220,160,45,20);
- getContentPane().add(divisao);
- divisao.addActionListener(this);
- }
- public static void main(String[] args) {
- ProvaRecuperacao janela = new ProvaRecuperacao();
- janela.setVisible(true);
- }
- public void actionPerformed(ActionEvent acao) {
- /* if(caixa1.getText().matches("//D*.*")) {
- JOptionPane.showMessageDialog(null, "Digite numeros na caixa 1.");
- }
- if(caixa2.getText().matches("//D*.*")) {
- JOptionPane.showMessageDialog(null, "Digite numeros na caixa 2.");
- //return;
- } */
- if(acao.getSource()==soma){
- try{
- if((caixa1.getText().equals(""))&&((caixa2.getText().equals("")))){
- JOptionPane.showMessageDialog(null, "Caixas vazias.");
- return;
- }
- if(caixa1.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Caixa 1 vazia.");
- }
- if(caixa2.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Caixa 2 vazia.");
- // return;
- }
- Double num1 = Double.parseDouble(caixa1.getText());
- Double num2 = Double.parseDouble(caixa2.getText());
- Double resul = num1 + num2;
- JOptionPane.showMessageDialog(null, resul);
- }
- catch(NumberFormatException e) {
- }
- }
- if(acao.getSource()==subtra){
- try{
- if((caixa1.getText().equals(""))&&((caixa2.getText().equals("")))){
- JOptionPane.showMessageDialog(null, "Caixas vazias.");
- return;
- }
- if(caixa1.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Caixa 1 vazia.");
- }
- if(caixa2.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Caixa 2 vazia.");
- return;
- }
- Double num1 = Double.parseDouble(caixa1.getText());
- Double num2 = Double.parseDouble(caixa2.getText());
- Double resul = num1 - num2;
- JOptionPane.showMessageDialog(null, resul);
- }
- catch(NumberFormatException e) {
- }
- }
- if(acao.getSource()==multi){
- try{
- if((caixa1.getText().equals(""))&&((caixa2.getText().equals("")))){
- JOptionPane.showMessageDialog(null, "Caixas vazias.");
- return;
- }
- if(caixa1.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Caixa 1 vazia.");
- }
- if(caixa2.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Caixa 2 vazia.");
- return;
- }
- Double num1 = Double.parseDouble(caixa1.getText());
- Double num2 = Double.parseDouble(caixa2.getText());
- Double resul = num1 * num2;
- JOptionPane.showMessageDialog(null, resul);
- }
- catch(NumberFormatException e) {
- }
- }
- if(acao.getSource()==divisao){
- try{
- if((caixa1.getText().equals(""))&&((caixa2.getText().equals("")))){
- JOptionPane.showMessageDialog(null, "Caixas vazias.");
- return;
- }
- if(caixa1.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Caixa 1 vazia.");
- }
- if(caixa2.getText().equals("")){
- JOptionPane.showMessageDialog(null, "Caixa 2 vazia.");
- return;
- }
- Double num1 = Double.parseDouble(caixa1.getText());
- Double num2 = Double.parseDouble(caixa2.getText());
- Double resul = num1 / num2;
- JOptionPane.showMessageDialog(null, resul);
- }
- catch(NumberFormatException e) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement