Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calc;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.InputMismatchException;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class Calculadora extends JFrame implements ActionListener{
- JButton soma, subtrai, multiplica, dividi;
- JTextField caixa1, caixa2, result;
- JLabel lb_num1, lb_num2, lb_calc, lb_resultado;
- double valor1, valor2, resultado;
- public Calculadora(){
- super("calculadora");
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(400, 300);
- setResizable(false);
- setLocationRelativeTo(null); // faz aparecer no meio da tela
- setLayout(null);
- setBackground(Color.DARK_GRAY);
- soma = new JButton("Somar");
- add(soma);
- soma.setBounds(10, 140, 80, 30);
- soma.addActionListener(this);
- soma.setActionCommand("somar");
- subtrai = new JButton("Subtrair");
- add(subtrai);
- subtrai.setBounds(100, 140, 80, 30);
- subtrai.addActionListener(this);
- subtrai.setActionCommand("subtrair");
- multiplica = new JButton("Multiplicar");
- add(multiplica);
- multiplica.setBounds(190, 140, 100, 30);
- multiplica.addActionListener(this);
- multiplica.setActionCommand("multiplicar");
- dividi = new JButton("Dividir");
- add(dividi);
- dividi.setBounds(300, 140, 80, 30);
- dividi.addActionListener(this);
- dividi.setActionCommand("dividir");
- caixa1 = new JTextField();
- add(caixa1);
- caixa1.setBounds(45, 90, 120, 25);
- caixa2 = new JTextField();
- add(caixa2);
- caixa2.setBounds(195, 90, 120, 25);
- result = new JTextField();
- add(result);
- result.setBounds(130, 220, 120, 25);
- result.setEditable(false);
- lb_num1 = new JLabel("Primeiro valor");
- add(lb_num1);
- lb_num1.setFont( new Font("serif", Font.ITALIC, 15));
- lb_num1.setBounds(45, 70, 110, 25);
- lb_num2 = new JLabel("Segundo valor");
- add(lb_num2);
- lb_num2.setFont( new Font("serif", Font.ITALIC, 15));
- lb_num2.setBounds(195, 70, 110, 25);
- lb_calc = new JLabel("Calculadora");
- add(lb_calc);
- lb_calc.setFont( new Font("serif", Font.ITALIC, 33));
- lb_calc.setBounds(120, 20, 220, 25);
- }
- public static void main(String[] args) {
- new Calculadora();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getActionCommand().equals("somar")){
- boolean i = tratar();
- if(i == true){
- resultado = valor1 + valor2;
- result.setText(""+resultado);
- }
- }else if(e.getActionCommand().equals("subtrair")){
- boolean i = tratar();
- if(i == true){
- resultado = valor1 - valor2;
- result.setText(""+resultado);
- }
- }else if(e.getActionCommand().equals("multiplicar")){
- boolean i = tratar();
- if(i == true){
- resultado = valor1 * valor2;
- result.setText(""+resultado);
- }
- }else if(e.getActionCommand().equals("dividir")){
- boolean i = tratar();
- if(i == true){
- resultado = valor1 / valor2;
- result.setText(""+resultado);
- }
- }
- }
- // metodo de tratamento de valores ---------------------------------------------------
- public boolean tratar(){
- // tratamento de campos vazios
- if(caixa1.getText().equals("") || caixa2.getText().equals("")){
- if(caixa1.getText().equals(""))
- caixa1.setBackground(Color.RED);
- if(caixa2.getText().equals(""))
- caixa2.setBackground(Color.RED);
- JOptionPane.showMessageDialog(null, "Campo vazio");
- return false;
- }else{
- // tratamento de valores invalidos
- try{
- valor1 = Double.parseDouble(caixa1.getText());
- valor2 = Double.parseDouble(caixa2.getText());
- }catch(NumberFormatException erro){
- JOptionPane.showMessageDialog(null, "Digite apenas numeros");
- return false;
- }
- }
- // se tudo der certo ;)
- caixa1.setBackground(Color.white);
- caixa2.setBackground(Color.white);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement