10. TALLER DE PROGRAMACIÓN
EL TRIQUI
1. Código Fuente en NetBeans
package paqTriki;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
/**
*
* @author
*/
public class JTriki extends javax.swing.JFrame {
JButton[][] botones=new JButton[3][3];
String letra="x";
Integer x=0, y=0;
/**
* Creates new form JTriki
*/
public JTriki() {
initComponents();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
botones[i][j]=new JButton("");
botones[i][j].setBounds((60*i)+10,(60*j)+10,50,50);
this.add(botones[i][j]);
botones[i][j].setVisible(true);
botones[i][j].addActionListener(es);
}}
}
ActionListener es=new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(botones[i][j].equals(e.getSource())){
if(botones[i][j].getText().equals("")){
botones[i][j].setText(letra);
x=i;
y=j;
}
}}
}
if(letra.equals("x")){
letra="o";
}
else{
letra="x";
}
if((botones[x][y].getText().equals(botones[x+1][y+1].getText()))&&
(botones[x][y].getText().equals(botones[x+1][y+1].getText()))){
JOptionPane.showMessageDialog(null, " Triqui");
}
if((botones[x][y].getText().equals(botones[x-1][y].getText()))&&
(botones[x][y].getText().equals(botones[x-2][y].getText()))){
JOptionPane.showMessageDialog(null, " Triqui");
}
if((botones[x][y].getText().equals(botones[x-1][y].getText()))&&
(botones[x][y].getText().equals(botones[x+1][y].getText()))){
JOptionPane.showMessageDialog(null, " Triqui");
}
if((botones[x][y].getText().equals(botones[x][y+1].getText()))&&
(botones[x][y].getText().equals(botones[x][y+2].getText()))){
JOptionPane.showMessageDialog(null, " Triqui");
}
if((botones[x][y].getText().equals(botones[x-1][y-1].getText()))&&
(botones[x][y].getText().equals(botones[x+1][y+1].getText()))){
JOptionPane.showMessageDialog(null, " Triqui");
}
}};
2. Comprimido del programa
No hay comentarios:
Publicar un comentario