Ajudinha :oops:

1 resposta
O

Tudo be pessoal
eu to usando o J2SDK 1.4.2 e gostaria de saber como fazer uma telinha(formulário) com dois campos e um botão e quando eu clicasse no botão ele somasse os dois campos e mostrasse no texto do botão mesmo. Obrigado e espero q me ajudem !

1 Resposta

A

import java.awt.;
import java.awt.event.
;

import javax.swing.*;

/*

  • Created on 29/06/2003
  • To change the template for this generated file go to
  • Window>Preferences>Java>Code Generation>Code and Comments
    */

/**

  • @author André Topázio

  • To change the template for this generated type comment go to

  • Window>Preferences>Java>Code Generation>Code and Comments
    
    */
    
    public class Aplicacao extends JFrame implements ActionListener {
    
    private JTextField texto1;
    
    private JTextField texto2;
    
    private JButton butao;
    
    private JPanel painel;
    
    public Aplicacao(){
    
    texto1=new JTextField();
    
    texto2=new JTextField();
    
    butao=new JButton(Somar);
    
    painel=new JPanel();
    
    painel.setLayout(new GridLayout(3,1));
    
    painel.add(texto1);
    
    painel.add(texto2);
    
    painel.add(butao);
    
    butao.addActionListener(this);
     
     this.getContentPane().add(painel);
     
     this.setSize(200,200);
     this.show();
    

    }

    public void somar(){
    
    int x=Integer.parseInt(texto1.getText());
    
    int y=Integer.parseInt(texto2.getText());
    
    int result=x+y;
    
    butao.setText(""+result);
    
    }
    
    public void actionPerformed(ActionEvent e){
    
    if(e.getSource()==butao){
    
    this.somar();
    
    }
    
    }
    
    public static void main(String[] args) {
    
    new Aplicacao();
    
    }
    
    }
    

Seja Bem-vindo ao mundo Java… :slight_smile:

Criado 28 de junho de 2003
Ultima resposta 29 de jun. de 2003
Respostas 1
Participantes 2