Help na calculadora [Resolvido]

2 respostas
jkmilab

Queria uma ajuda nesse programinha, é uma calculadora com botão de rádio que eu não tô conseguindo rodar, a parte do erro esta em comentário, se alguém puder me ajudar, vlws

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Radio2 extends JFrame implements ItemListener {
  
   JLabel L1,L2,L3;
   JRadioButton B1, B2, B3, B4, B5;
   JTextField T1, T2, T3;
   ButtonGroup botaoradio;
   JPanel P2;
   public static void main (String args[]) {

JFrame janela = new Radio2();
janela.show();
WindowListener x = new WindowAdapter()
     { public void windowClosing (WindowEvent fecha)
             {System.exit(0);}
      };
 janela.addWindowListener(x);
      
   Radio2();{
     
     getContentPane().setBackground (new Color (0,0,0));
     getContentPane().setLayout (new GridLayout(3,4));
     L1 = new JLabel("numero 1");
     L1.setForeground(Color.green);
     L1.setFont(new Font(" ",Font.BOLD,14));
     L2 = new JLabel("numero 2");
     L2.setForeground(Color.blue);
     L2.setFont(new Font(" ",Font.BOLD,14));
     L3 = new JLabel("resultado");
     L3.setForeground(Color.yellow);
     L3.setFont(new Font(" ",Font.BOLD,14));
     
     B1 = new JRadioButton("+"); 
     B1.addItemListener(this);
     
     B2 = new JRadioButton("-"); 
     B2.addItemListener(this);
     
     B3 = new JRadioButton("*"); 
     B3.addItemListener(this);
     
     B4 = new JRadioButton("/"); 
     B4.addItemListener(this);
     
     B5 = new JRadioButton("limpar"); 
     B5.addItemListener(this);
     
     B1.setMnemonic(KeyEvent.VK_1);
     B2.setMnemonic(KeyEvent.VK_2);
     B3.setMnemonic(KeyEvent.VK_3);
     B4.setMnemonic(KeyEvent.VK_4);
     B5.setMnemonic(KeyEvent.VK_5);
     
     botaoradio = new ButtonGroup();
     botaoradio.add(B1);
     botaoradio.add(B2);
     botaoradio.add(B3);
     botaoradio.add(B4);
     botaoradio.add(B5);


P2.setLayout(new GridLayout (2,3));
P2.setBackground (new Color (200,100,100));

P2.add(B1);
P2.add(B2);
P2.add(B3);
P2.add(B4);
P2.add(B5);

getContentPane().add(P2);

     T1 = new JTextField();
     T1.setBackground(Color.black);
     T1.setForeground(Color.white);
     T2 = new JTextField();
     T2.setBackground(Color.black);
     T2.setForeground(Color.white);
     T3 = new JTextField();
     T3.setBackground(Color.black);
     T3.setForeground(Color.white);
     T3.setEditable(false);
     getContentPane().add(L1);
     getContentPane().add(T1);
     getContentPane().add(B1);
     getContentPane().add(B2);
     getContentPane().add(L2);
     getContentPane().add(T2);
     getContentPane().add(B3);
     getContentPane().add(B4);
     getContentPane().add(L3);
     getContentPane().add(T3);
     getContentPane().add(B5);

   }
   
  //Essa parte aqui esta dando erro "illegal start of expression"   
  //public void itemStateChanged(ItemEvent e){ 
             
        if (e.getSource()==B5)
    {
      T1.setText(" ");
      T2.setText(" ");
      T3.setText(" ");
      return;
     }

   float n1=0, n2=0, resultado=0;
   
    try
    { 
    n1 = Float.parseFloat(T1.getText());
    n2 = Float.parseFloat(T2.getText());
    }
   catch (NumberFormatException erro)
    { T3.setText("erro"); return;}
    
    if (e.getSource()==B1)
    { 
    resultado = n1 + n2;
    }
  
    if (e.getSource()==B2)
    { 
    resultado = n1 - n2;
    }

    if (e.getSource()==B3)
    { 
    resultado = n1 * n2;
    }

    if (e.getSource()==B4)
    { 
    resultado = n1 / n2;
    }
   
    T3.setText(" "+resultado);

    }
   
}

2 Respostas

Petronio_Braga

Segue seu código ajeitado. Agora, é bom você rever alguns conceitos. Você colocou o construtor dentro do método main. E também colocou o método itemStateChanged(ItemEvent e) dentro do método main. Dê uma olhada nisso.

package src;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Calculadora extends JFrame implements ItemListener {

	private static final long serialVersionUID = 151465088124477736L;

	private JLabel lbl1, lbl2, lbl3;

	private JRadioButton rb1, rb2, rb3, rb4, rb5;

	private JTextField txt1, txt2, txt3;

	private ButtonGroup botaoRadio;

	private JPanel panel;

	public Calculadora() {

		getContentPane().setBackground(new Color(0, 0, 0));
		getContentPane().setLayout(new GridLayout(3, 4));
		lbl1 = new JLabel("numero 1");
		lbl1.setForeground(Color.green);
		lbl1.setFont(new Font(" ", Font.BOLD, 14));
		lbl2 = new JLabel("numero 2");
		lbl2.setForeground(Color.blue);
		lbl2.setFont(new Font(" ", Font.BOLD, 14));
		lbl3 = new JLabel("resultado");
		lbl3.setForeground(Color.yellow);
		lbl3.setFont(new Font(" ", Font.BOLD, 14));

		rb1 = new JRadioButton("+");
		rb1.addItemListener(this);

		rb2 = new JRadioButton("-");
		rb2.addItemListener(this);

		rb3 = new JRadioButton("*");
		rb3.addItemListener(this);

		rb4 = new JRadioButton("/");
		rb4.addItemListener(this);

		rb5 = new JRadioButton("limpar");
		rb5.addItemListener(this);

		rb1.setMnemonic(KeyEvent.VK_1);
		rb2.setMnemonic(KeyEvent.VK_2);
		rb3.setMnemonic(KeyEvent.VK_3);
		rb4.setMnemonic(KeyEvent.VK_4);
		rb5.setMnemonic(KeyEvent.VK_5);

		botaoRadio = new ButtonGroup();
		botaoRadio.add(rb1);
		botaoRadio.add(rb2);
		botaoRadio.add(rb3);
		botaoRadio.add(rb4);
		botaoRadio.add(rb5);

		panel = new JPanel();
		panel.setLayout(new GridLayout(2, 3));
		panel.setBackground(new Color(200, 100, 100));

		panel.add(rb1);
		panel.add(rb2);
		panel.add(rb3);
		panel.add(rb4);
		panel.add(rb5);

		getContentPane().add(panel);

		txt1 = new JTextField();
		txt1.setBackground(Color.black);
		txt1.setForeground(Color.white);
		txt2 = new JTextField();
		txt2.setBackground(Color.black);
		txt2.setForeground(Color.white);
		txt3 = new JTextField();
		txt3.setBackground(Color.black);
		txt3.setForeground(Color.white);
		txt3.setEditable(false);
		getContentPane().add(lbl1);
		getContentPane().add(txt1);
		getContentPane().add(rb1);
		getContentPane().add(rb2);
		getContentPane().add(lbl2);
		getContentPane().add(txt2);
		getContentPane().add(rb3);
		getContentPane().add(rb4);
		getContentPane().add(lbl3);
		getContentPane().add(txt3);
		getContentPane().add(rb5);

	}

	public static void main(String args[]) {

		JFrame janela = new Calculadora();
		janela.setVisible(true);
		janela.setSize(400, 400);
		janela.setTitle("Calculadora");
		WindowListener x = new WindowAdapter() {
			public void windowClosing(WindowEvent fecha) {
				System.exit(0);
			}
		};
		janela.addWindowListener(x);
	}

	public void itemStateChanged(ItemEvent e) {
		if (e.getSource() == rb5) {
			txt1.setText(" ");
			txt2.setText(" ");
			txt3.setText(" ");
			return;
		}

		float n1 = 0, n2 = 0, resultado = 0;

		try {
			n1 = Float.parseFloat(txt1.getText());
			n2 = Float.parseFloat(txt2.getText());
		} catch (NumberFormatException erro) {
			txt3.setText("erro");
			return;
		}

		if (e.getSource() == rb1) {
			resultado = n1 + n2;
		}

		if (e.getSource() == rb2) {
			resultado = n1 - n2;
		}

		if (e.getSource() == rb3) {
			resultado = n1 * n2;
		}

		if (e.getSource() == rb4) {
			resultado = n1 / n2;
		}

		txt3.setText(" " + resultado);

	}

}
jkmilab

Eu ainda sou iniciante, não conheço as regrinhas de java faço os programas baseados em outros programas…

vlw mto pela ajuda, está perfeito, mto obrigada msm

Criado 10 de junho de 2007
Ultima resposta 11 de jun. de 2007
Respostas 2
Participantes 2