Aew galera aqui estou eu de novo :razz: \o/ Bem, hoje eu estava aqui tentando fazer uma calculadorazinha e talz com as 4 operações básicas, cosegui e talz, mas ai eu num me convenci e coloquei uns botões :lol: mas eu não sei como colocar uma ação no botão, tipo ex, quando eu clicar no botão 1 da minha calculadora aparecer no campo de texto?!? tipo eu consegui de uma forma meio bizarra, mas so aparece uma vez, não aparece mais de uma cs me entenderam? :???: bem… ta aki o código dele =~~ me ajudem :lol:
==========================================
package Arquivo;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Janela extends JFrame implements ActionListener {
JButton botao = new JButton("+");
JButton botao2 = new JButton ("-");
JButton botao3 = new JButton ("/");
JButton botao4 = new JButton ("*");
JButton botnum = new JButton (“0”);
JButton botnum1 = new JButton (“1”);
JButton botnum2 = new JButton (“2”);
JButton botnum3 = new JButton (“3”);
JButton botnum4 = new JButton (“4”);
JTextField valor1 = new JTextField(10);
JTextField valor2 = new JTextField(10);
JTextField result = new JTextField(10);
JPanel painel = new JPanel();
Janela(String titulo){
super(titulo);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
botao.addActionListener(this);
botao2.addActionListener(this);
botao3.addActionListener(this);
botao4.addActionListener(this);
botnum.addActionListener(this);
botnum1.addActionListener(this);
botnum2.addActionListener(this);
botnum3.addActionListener(this);
botnum4.addActionListener(this);
painel.add(valor1);
painel.add(valor2);
painel.add(botao);
painel.add(botao2);
painel.add(botao3);
painel.add(botao4);
painel.add(botnum);
painel.add(botnum1);
painel.add(botnum2);
painel.add(botnum3);
painel.add(botnum4);
painel.add(result);
setContentPane(painel);
setSize(270,300);
setVisible(true);
}
public static void main(String args[]){
Janela in = new Janela(“Minha primeira CALCULADORA”);
}
public void somar(){
int n1,n2,soma;
n1 = Integer.parseInt(valor1.getText());
n2 = Integer.parseInt(valor2.getText());
soma = n1 + n2;
result.setText(""+soma);
}
public void subtrair(){
int n1,n2,sub;
n1 = Integer.parseInt(valor1.getText());
n2 = Integer.parseInt(valor1.getText());
sub = n1 - n2;
result.setText(""+sub);
}
public void dividir(){
float n1,n2,div;
n1 = Integer.parseInt(valor1.getText());
n2 = Integer.parseInt(valor2.getText());
div = n1/n2;
result.setText(""+div);
}
public void actionPerformed(ActionEvent evt){
if(evt.getSource()== botao){
somar();
}
if(evt.getSource()== botao2){
subtrair();
}
if(evt.getSource()== botao3){
dividir();
}
if(evt.getSource()== botnum){
valor1.setText("0");
}
if(evt.getSource()== botnum1){
valor2.setText("1");
}
}
}