Fazer um resultado aparecer no JLabel

1 resposta
E

Eu fiz uma aplicação que tem dois campos e um botao de soma quando clica nele , joga o resultado para um teceiro campo , ai esta o problema nao consigo fazer o resultado aparecer no terceiro label. Alguem pode me ajudar ai ?

Trecho do problema:

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);
                        
                        
                        //AQUI ESTA O PROBLEMA 
                        frame.setText("A Soma é: " + soma);


                    }
                });
Código inteiro:
package View;

import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class somar {

	private JFrame frame;
	private JTextField txf1;
	private JTextField txf2;
	


	/**
	 * 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);
		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);
                        
                        
                        //AQUI ESTA O PROBLEMA 
                        frame.setText("A Soma é: " + soma);


                    }
                });
	

		JLabel lblResultado = new JLabel("Resultado:");
		lblResultado.setHorizontalAlignment(SwingConstants.CENTER);
		lblResultado.setBounds(176, 152, 97, 16);
		panel.add(lblResultado);
		
		JLabel lblresult = new JLabel("");
		lblresult.setBounds(186, 181, 195, 33);
		panel.add(lblresult);
	}
}

1 Resposta

ViniGodoy

Por favor, não duplique tópicos. Se quer complementar informações, volte ao seu tópico anterior e faça isso por lá.
http://www.guj.com.br/java/276626-somar-2-numeros-ocm-botao-somar

Esse tópico será trancado.

Criado 27 de junho de 2012
Ultima resposta 28 de jun. de 2012
Respostas 1
Participantes 2