Buenas tardes estoy realizando una pequeña calculadora,Lo tengo todo listo, cada elemento en su sitio INCLUYENDO LOS BOTONES, y asta ahi bien, el problema es la accion que tiene que realizar los botones, que si le pulso en un boton, por ejemplo 1 me tiene que escribir uno, pero no lo hace, en el tema de los botones ¿Me podrian hechar una mano?
ESTE ES EL CODIGO DEL BOTON
btnBoton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
txtResultado.setText("1");
}
});
ESTE ES TODO EL CODIGO DE MI CALCULADORA INCLUYENDO EL DE ARRIBA (NOTA:MI CALCULADORA SOLO SUMA)
package formularios;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class CalculadoraExtedida extends JFrame {
JButton btnSumar = new JButton("+");
JButton btnIgual = new JButton("=");
JTextField txtResultado = new JTextField(5);
JButton btnBoton = null;
public CalculadoraExtedida()
{
//Configuro frame
this.setVisible(true);
this.setBounds(20, 20, 400, 500);
this.setTitle("Calculadora estandar");
this.setLayout(new GridBagLayout());
//Diseñar el formulario
CrearDiseño();
}
private void CrearDiseño()
{
GridBagConstraints restriccionBag = new GridBagConstraints();
//Crear botones
for (int i = 0; i < 10; i++)
{
btnBoton = new JButton(i+"");
btnBoton.setActionCommand(i+"");
btnBoton.setFont(new Font("Arial", Font.BOLD, 25));//Tamaño del contenido
restriccionBag.gridx=i%3;
restriccionBag.weightx=0.1;
restriccionBag.weighty=0.2;
restriccionBag.fill=GridBagConstraints.BOTH;
this.getContentPane().add(btnBoton,restriccionBag);
}
btnSumar = new JButton("+");
btnIgual = new JButton("=");
btnSumar.setFont(new Font("Arial", Font.BOLD, 25));
btnIgual.setFont(new Font("Arial", Font.BOLD, 25));
txtResultado = new JTextField(5);
txtResultado.setEnabled(false);
restriccionBag.gridx=1;
restriccionBag.gridy=3;
restriccionBag.fill=GridBagConstraints.BOTH;
this.getContentPane().add(btnSumar,restriccionBag);
restriccionBag.gridx=2;
restriccionBag.gridy=3;
restriccionBag.fill=GridBagConstraints.BOTH;
this.getContentPane().add(btnIgual,restriccionBag);
restriccionBag.gridx=0;
restriccionBag.gridy=4;
restriccionBag.gridwidth=3;
restriccionBag.fill=GridBagConstraints.BOTH;
this.getContentPane().add(txtResultado,restriccionBag);
btnBoton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
txtResultado.setText("1");
}
});
}
}