lestat1666
Membro
Eu estou a ter um problema que penso que se seja com o J2SE pois acho que isto está correcto
Penso que o problema seja quando faço b1.addActionListener(this); e depois quando faço if(e.equals(b1)). Se optar por algo do genero b1.addActionListerner(new ActionListener(){ public void actionPerformed(ActionListener e.............................. } acabo por ter ainda mais erros. Alguem me pode ajudar? o codigo é o seguinte.
Penso que o problema seja quando faço b1.addActionListener(this); e depois quando faço if(e.equals(b1)). Se optar por algo do genero b1.addActionListerner(new ActionListener(){ public void actionPerformed(ActionListener e.............................. } acabo por ter ainda mais erros. Alguem me pode ajudar? o codigo é o seguinte.
Código:
package JogoDoGalo;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
public class JogoDoGalo implements ActionListener {
private JLabel label;
private JFrame frame;
private JPanel panel;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;
private JButton b7;
private JButton b8;
private JButton b9;
private static int jogador;
private int[][] round;
public static void main(String[] args) {
JogoDoGalo gam3 = new JogoDoGalo();
}
public JogoDoGalo() {
label = new JLabel();
frame = new JFrame("Jogo do galo");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
panel = new JPanel();
panel.setLayout(new GridLayout(3, 3));
panel.add(b1 = new JButton());
panel.add(b2 = new JButton());
panel.add(b3 = new JButton());
panel.add(b4 = new JButton());
panel.add(b5 = new JButton());
panel.add(b6 = new JButton());
panel.add(b7 = new JButton());
panel.add(b8 = new JButton());
panel.add(b9 = new JButton());
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
frame.setContentPane(panel);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.equals(b1)) {
if (jogador % 2 == 0) {
b1.setBackground(Color.BLACK);
b1.setForeground(Color.white);
b1.setText("X");
b1.setEnabled(false);
round[0][0] = 1;
System.round(round, jogador);
jogador++;
} else {
b1.setBackground(Color.WHITE);
b1.setForeground(Color.black);
b1.setText("0");
b1.setEnabled(false);
round[0][0] = 2;
System.round(round, jogador);
jogador++;
}
}
if(e.equals(b2))
{
if (jogador % 2 == 0) {
b2.setBackground(Color.BLACK);
b2.setForeground(Color.white);
b2.setText("X");
b2.setEnabled(false);
round[0][1] = 1;
System.round(round, jogador);
jogador++;
} else {
b2.setBackground(Color.WHITE);
b2.setForeground(Color.black);
b2.setText("0");
b2.setEnabled(false);
round[0][1] = 2;
System.round(round, jogador);
jogador++;
}
}
if(e.equals(b3))
{
if (jogador % 2 == 0) {
b3.setBackground(Color.BLACK);
b3.setForeground(Color.white);
b3.setText("X");
b3.setEnabled(false);
round[0][2] = 1;
System.round(round, jogador);
jogador++;
} else {
b3.setBackground(Color.WHITE);
b3.setForeground(Color.black);
b3.setText("0");
b3.setEnabled(false);
round[0][2] = 2;
System.round(round, jogador);
jogador++;
}
}
if(e.equals(b4))
{
if (jogador % 2 == 0) {
b4.setBackground(Color.BLACK);
b4.setForeground(Color.white);
b4.setText("X");
b4.setEnabled(false);
round[1][0] = 1;
System.round(round, jogador);
jogador++;
} else {
b4.setBackground(Color.WHITE);
b4.setForeground(Color.black);
b4.setText("0");
b4.setEnabled(false);
round[1][0] = 2;
System.round(round, jogador);
jogador++;
}
}
if(e.equals(b5))
{
if (jogador % 2 == 0) {
b5.setBackground(Color.BLACK);
b5.setForeground(Color.white);
b5.setText("X");
b5.setEnabled(false);
round[1][1] = 1;
System.round(round, jogador);
jogador++;
} else {
b5.setBackground(Color.WHITE);
b5.setForeground(Color.black);
b5.setText("0");
b5.setEnabled(false);
round[1][1] = 2;
System.round(round, jogador);
jogador++;
}
}
if(e.equals(b6))
{
if (jogador % 2 == 0) {
b6.setBackground(Color.BLACK);
b6.setForeground(Color.white);
b6.setText("X");
b6.setEnabled(false);
round[1][2] = 1;
System.round(round, jogador);
jogador++;
} else {
b6.setBackground(Color.WHITE);
b6.setForeground(Color.black);
b6.setText("0");
b6.setEnabled(false);
round[1][2] = 2;
System.round(round, jogador);
jogador++;
}
}
if(e.equals(b7))
{
if (jogador % 2 == 0) {
b7.setBackground(Color.BLACK);
b7.setForeground(Color.white);
b7.setText("X");
b7.setEnabled(false);
round[2][0] = 1;
System.round(round, jogador);
jogador++;
} else {
b7.setBackground(Color.WHITE);
b7.setForeground(Color.black);
b7.setText("0");
b7.setEnabled(false);
round[2][0] = 2;
System.round(round, jogador);
jogador++;
}
}
if(e.equals(b8))
{
if (jogador % 2 == 0) {
b8.setBackground(Color.BLACK);
b8.setForeground(Color.white);
b8.setText("X");
b8.setEnabled(false);
round[2][1] = 1;
System.round(round, jogador);
jogador++;
} else {
b8.setBackground(Color.WHITE);
b8.setForeground(Color.black);
b8.setText("0");
b8.setEnabled(false);
round[2][1] = 2;
System.round(round, jogador);
jogador++;
}
}
if(e.equals(b9))
{
if (jogador % 2 == 0) {
b9.setBackground(Color.BLACK);
b9.setForeground(Color.white);
b9.setText("X");
b9.setEnabled(false);
round[2][2] = 1;
System.round(round, jogador);
jogador++;
} else {
b9.setBackground(Color.WHITE);
b9.setForeground(Color.black);
b9.setText("0");
b9.setEnabled(false);
round[2][2] = 2;
System.round(round, jogador);
jogador++;
}
}
if (jogador == 9)
JOptionPane.showMessageDialog(panel, "EMPATE");
}