Somar 2 numeros ocm botão somar

6 respostas
E
Eu to fazendo uma aplicação que calcula dois numeros em um frame , e quando clica em calcular ele joga o resultando para um label , porem ta dando um errinho acho que é sintaxe ,alí no : frame.setText. Eu queria que aparecesse o resultado.Alguem pode dar uma mão ?
JButton btncalcular = new JButton("Calcular");
		btncalcular.setBounds(176, 106, 90, 33);
		btncalcular.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        int numero1, numero2, soma = 0;
                        numero1 = Integer.parseInt(txf1.getText());
                        numero2 = Integer.parseInt(txf2.getText());

                        soma = numero1 + numero2;

                        frame.setVisible(true);
                        frame.setText("A Soma é: " + soma);


                    }
                });

6 Respostas

M

Qual sua dúvida em relação a isso?? Qual dificuldade?? O que já tem pronto para que alguem possa te ajudar???

D

u é para ser ajudado vc deve começar a fazer… é o primeiro passo… assim vc vai pouco a pouco aprendendo mais…

seja mais especifico com sua duvida… poste o que ja fez e seja coerente com suas perguntas… pois aqui ninguem ira fazer o seu trabalho… mais sim auxilia-lo para que chegue ao que deseja fazer… fica dica…

V

Troque isso:

frame.setVisible(true); frame.setText("A Soma é: " + soma);

Por:

V

Crie o label antes, e use o setText do label.

package View;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class somar {

	private JFrame frame;
	private JTextField txf1;
	private JTextField txf2;
	private JLabel lblresult;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					somar window = new somar();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public somar() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblnum1 = new JLabel("Numero 1:");
		lblnum1.setBounds(22, 40, 116, 16);
		panel.add(lblnum1);
		
		txf1 = new JTextField();
		txf1.setBounds(22, 69, 116, 22);
		panel.add(txf1);
		txf1.setColumns(10);
		
		JLabel lblnum2 = new JLabel("Numero 2:");
		lblnum2.setBounds(299, 40, 131, 16);
		panel.add(lblnum2);
		
		txf2 = new JTextField();
		txf2.setBounds(299, 69, 116, 22);
		panel.add(txf2);
		txf2.setColumns(10);
		
		JButton btncalcular = new JButton("Calcular");
		btncalcular.setBounds(176, 106, 90, 33);
		panel.add(btncalcular);
		JLabel lblResultado = new JLabel("Resultado:");
		lblResultado.setHorizontalAlignment(SwingConstants.CENTER);
		lblResultado.setBounds(176, 152, 97, 16);
		panel.add(lblResultado);

		lblresult = new JLabel("");
		lblresult.setBounds(186, 181, 195, 33);
		panel.add(lblresult);
		
		btncalcular.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        int numero1, numero2, soma = 0;
                        numero1 = Integer.parseInt(txf1.getText());
                        numero2 = Integer.parseInt(txf2.getText());

                        soma = numero1 + numero2;

                        lblresult.setText("A Soma é: " + soma);
                    }
                });
	}
}

Se você quer alterar o valor de um componente, deve usar esse componente.
Frame é a sua janela, o máximo que vc poderia alterar ali em termos de texto é o título...

No seu código também faltou adicionar o btncalcular ao painel.

E

Valeu , deu certinho intendi o problema !

D

Se você conseguiu solucionar a sua dúvida, edite o primeiro post do tópico e coloque [Resolvido], até para facilitar o pessoal na hora da pesquisa.
Abraços

Criado 27 de junho de 2012
Ultima resposta 30 de jun. de 2012
Respostas 6
Participantes 5