1
Aprender a programar desde cero / Java clase extends JFrame implements ActionListener con JButton y eventos
« en: 13 de Abril 2016, 01:05 »
Tengo el siguiente código pero no me ejecita ¿cual puede ser el error?
Código: [Seleccionar]
package guii;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class GUII extends JFrame implements ActionListener {
int a,sum=0,cont=5,sum2=0,sum3=0,sum4=0,sum5=0,sum6=0,sum7=0,sum8,sum9;
Container ctn;
JPanel panel1, panel2;
JButton [][] boton;
JButton botones1[];
JButton botones2[];
private Object evt;
public void GUII(){
ctn = getContentPane();
boton=new JButton [6][5];
for (int i = 0; i <=6; i++) {
for (int j = 0; j <=5; j++) {
boton[i][j] = new JButton();
panel1.add(boton[i][j]);
boton[i][j].addActionListener(this);
this.add(panel1);
panel1.add(boton[0][0]);
panel1.add(boton[0][1]);
panel1.add(boton[0][2]);
panel1.add(boton[0][3]);
panel1.add(boton[0][4]);
boton[0][0].setText("generar");
boton[0][1].setText("todo");
boton[0][2].setText("diagonal");
boton[0][3].setText("bordes");
boton[0][4].setText("terminar");
}
}
panel1.add(boton[1][4]);
for (int i = 5; i <= 25; i++) {
}
panel2=new JPanel(new GridLayout(1,2));
botones1[30].setText("ver resultado");
botones2[31].setText("borar resultado");
panel2.add(botones1[30]);
panel2.add(botones2[31]);
ctn.setLayout(new BorderLayout());
ctn.add(panel1,BorderLayout.CENTER);
ctn.add(panel2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == boton[0][0]) {
cont=5;
sum=0;
for (int i = 0; i <= 4; i++) {
for (int j = 0; j <= 4; j++) {
a=1;
sum=sum+a;
cont=cont+1;
if (i==j) {
sum2=sum2+a;
}
if (i+j==4) {
sum3=sum3+a;
}
if (i==4){
sum4=sum4+a;
}
if (i==3){
sum5=sum5+a;
}
if (j==3){
sum6=sum6+a;
}
if (j==3){
sum7=sum7+a;
}
}
}
}
if (e.getSource()== boton[0][1]) {
JOptionPane.showMessageDialog(null,"la suma es :"+sum);
}
if (e.getSource()== boton[0][2]) {
sum8=sum2+sum3;
JOptionPane.showMessageDialog(null,"la suma en diagonal es :"+sum8);
}
if (e.getSource () == boton[0][3]){
sum9=sum4+sum5+sum6+sum7;
JOptionPane.showMessageDialog(null,"La Suma de los bordes es:"+sum9);
}
if (e.getSource () == boton[0][4]){
GUII.main (null);
}
}
public static void main(String arg[]){
GUII miVentanita = new GUII();
miVentanita.setSize(450,450);
miVentanita.setVisible(true);
}
}