-2

I'm making a tic tac toe game in Java using Eclipse.I have the source code all done but for some reason, it isn't working, and the menu are not appearing.Eclipse doesn’t show any error.Can someone help me out ?

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;

public class Jogo extends JFrame {
    private static final long serialVersionUID = 1L;
    private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
    int qtde;
    int jogador;
    int mat[][] = new int [3][3];
    JButton b[] = new JButton[9];
    String ganhador;
    String jogador1;
    String jogador2;

    public Jogo() {
        setTitle("Jogo da Velha");
        setBounds(190,100,300,400);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setBackground(new Color(197,197,197));
        setLayout(null);

        b1 = new JButton();
        b1.setBounds(25,50,60,70);
        this.add(b1);

        b2 = new JButton();
        b2.setBounds(115,50,60,70);
        this.add(b2);

        b3 = new JButton();
        b3.setBounds(205,50,60,70);
        this.add(b3);

        b4 = new JButton();
        b4.setBounds(25,140,60,70);
        this.add(b4);

        b5 = new JButton();
        b5.setBounds(115,140,60,70);
        this.add(b5);

        b6 = new JButton();
        b6.setBounds(205,140,60,70);
        this.add(b6);

        b7 = new JButton();
        b7.setBounds(25,230,60,70);
        this.add(b7);

        b8 = new JButton();
        b8.setBounds(115,230,60,70);
        this.add(b8);

        b9 = new JButton();
        b9.setBounds(205,230,60,70);
        this.add(b9);
        qtde = 1;
        jogador = 1;
        b[0] = b1;
        b[1] = b2;
        b[2] = b3;
        b[3] = b4;
        b[4] = b5;
        b[5] = b6;
        b[6] = b7;
        b[7] = b8;
        b[8] = b9;


    }
    public void jogada(JButton b,int x, int y){
        b.setEnabled(false);
        if (jogador == 1){
            mat [x][y] = 1;
            b.setText("X");
            jogador = 2;
            ganhador = jogador1;
            checarjogada(1);
        } else {
            mat [x][y] = 2;
            b.setText("O");
            jogador =1;
            ganhador = jogador2;
            checarjogada(2);
        }
        qtde++;
    }
    public void checarjogada(int x){
        if (vitoria(x)== true){
            JOptionPane.showMessageDialog(null,"Jogador:"+ganhador+""+"Venceu", "Vitória!",JOptionPane.INFORMATION_MESSAGE);
            fimdojogo();
        }
    }
    public boolean vitoria (int x){
        for (int i = 0; i < mat.length;i++){
        if(mat[i][0] == x && mat [i][1] == x && mat [i][2] == x){
            return true;
        }
        if (mat[0][i]== x && mat [1][i] == x && mat [2][i] == x){
            return true;
        }
    }
        if(mat[0][0] == x && mat [1][1] == x && mat[2][2] == x){
            return true;
        }
        if(mat[0][2] == x && mat [1][1] == x && mat [2][0] == x){
            return true;
        }
        return false;
    }
    public void fimdojogo(){
        for(int i =0; i<9;i++){
            b[i].setEnabled(false);
        }
    }
    public void limpar(){
        for(int i=0;i < 9; i++){
            b[i].setEnabled(true);
            b[i].setText("");
        }
        for(int x=0; x < 3; x++){
            for(int y=0;y < 3;y++){
                mat[x][y] = 0;
            }
        }
        jogador = 1;
        jogador1 = "";
        jogador2 = "";
        ganhador = "";

            jMenuBar1 = new JMenuBar();
            jMenu1 = new JMenu();
            jMenuItem1 = new JMenuItem();
            jMenu2 = new JMenu();

        b1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b1ActionPerformed(evt);
            }
        });

        b2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b2ActionPerformed(evt);
            }
        });

        b3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b3ActionPerformed(evt);
            }
        });

        b4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b4ActionPerformed(evt);
            }
        });

        b5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b5ActionPerformed(evt);
            }
        });

        b6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b6ActionPerformed(evt);
            }
        });

        b7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b7ActionPerformed(evt);
            }
        });

        b8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b8ActionPerformed(evt);
            }
        });

        b9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b9ActionPerformed(evt);
            }
        });

        jMenu1.setMnemonic('N');
        jMenu1.setText("Opções");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F8, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setMnemonic('N');
        jMenuItem1.setText("Novo Jogo");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Sair");
        jMenu2.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
            public void menuDeselected(javax.swing.event.MenuEvent evt) {
            }
            public void menuSelected(javax.swing.event.MenuEvent evt) {
                jMenu2MenuSelected(evt);
            }
        });
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);


    }
    private void jMenuItem1ActionPerformed(ActionEvent evt) {                                           
    }                                          

    private void b1ActionPerformed(ActionEvent evt) {                                     
      jogada(b1,0,0);
    }                                    

    private void b2ActionPerformed(ActionEvent evt) {                                     
      jogada(b2,0,1);
    }                                    

    private void b3ActionPerformed(ActionEvent evt) {                                     
       jogada(b3,0,2);
    }                                    

    private void b4ActionPerformed(ActionEvent evt) {                                     
        jogada(b4,1,0);
    }                                    

    private void b5ActionPerformed(ActionEvent evt) {                                     
        jogada(b5,1,1);
    }                                    

    private void b6ActionPerformed(ActionEvent evt) {                                     
        jogada(b6,1,2);
    }                                    

    private void b7ActionPerformed(ActionEvent evt) {                                     
        jogada(b7,2,0);
    }                                    

    private void b8ActionPerformed(ActionEvent evt) {                                     
        jogada(b8,2,1);
    }                                    

    private void b9ActionPerformed(ActionEvent evt) {                                     
        jogada(b9,2,2);
    }                                    

    private void jMenu2MenuSelected(MenuEvent evt) {                                    
        System.exit(0);
    }                                   

    private void jMenu1ActionPerformed(ActionEvent evt) {                                       
        limpar();
        jogador1 = JOptionPane.showInputDialog("Digite o nome do Jogador 1");
        jogador2 = JOptionPane.showInputDialog("Digite o nome do Jogador 2");
    }                                      

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Jogo().setVisible(true);
            }
        });
    }                   
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;                 
}
ChanderG
  • 7
  • 3
Gabriel Ozzy
  • 73
  • 1
  • 7
  • 1
    In your first section, you assign each element of your array to b1, b2, b3 and so on before they are defined (so they begin as null, and you have just copied null into each element of your array). Then, you define them as new jbuttons -- but this doesn't affect the array in java. You need to do it the other way around. Define them as new jbuttons, and THEN add them to your array. – scott_fakename Jun 01 '14 at 03:59
  • i did it but still the same... code edited! – Gabriel Ozzy Jun 01 '14 at 04:58
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jun 01 '14 at 05:44

2 Answers2

2

It sounds like you are asking about why the menu bar isn't showing up? Here's a simplified version of your code:

public class Jogo extends JFrame {

public Jogo() {
    ...
    //Adding buttons
}

private void jMenu1ActionPerformed(ActionEvent evt) {

    limpar();
    ...
}

public void limpar(){
    ...
    jMenuBar1 = new JMenuBar();
    ...
    setJMenuBar(jMenuBar1);
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Jogo().setVisible(true);
        }
    });
}

It's not showing up because you don't aren't creating the menu bar ever. You are creating the menu only when the limpar() method is called, but you only call this method when jMenu1 has an action performed. So it's never going to happen because you can't perform an action on a menu that hasn't been created yet! You need to create the JMenuBar in the constructor of your class instead.

tobii
  • 517
  • 3
  • 13
  • @user3632011 No problem! Please consider accepting an answer if it solved the issue. It's a nice "thank you" to people, and both you and the other person get reputation points. :) – tobii Aug 27 '14 at 21:47
0

Why dont you use a loop to create your buttons? Here is mine:

    import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.*;


public class TicTacToe extends JFrame {
    JPanel p = new JPanel();
    XOButton button[] = new XOButton[9];
    JButton clear = new JButton("CLEAR!");


    public static void main(String[] args) {
        new TicTacToe();
    }


    public TicTacToe() {
        setTitle("Tic Tac Toe!");
        setSize(600, 800);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(true);



        p.setLayout(new GridLayout(4,3));
        for (int i=0; i<9; i++) {
            button[i] = new XOButton();
            p.add(button[i]);

        clear.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 


    button[0].setIcon(null);
                    button[1].setIcon(null);
                    button[2].setIcon(null);
                    button[3].setIcon(null);
                    button[4].setIcon(null);
                    button[5].setIcon(null);
                    button[6].setIcon(null);
                    button[7].setIcon(null);
                    button[8].setIcon(null);

            }

        });


        p.add(clear);
        add(p);



        setVisible(true);

    }

}
}

====CLASS for IOButton==========

    import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class XOButton extends JButton implements ActionListener {
    ImageIcon X,O;
    byte value=0;


    public XOButton() {
        X = new ImageIcon(this.getClass().getResource("X.gif"));
        O = new ImageIcon(this.getClass().getResource("O.png"));
        this.addActionListener(this);


    }

    public void actionPerformed(ActionEvent e) {
        value++;
        value%=3;
        switch(value) {
            case 0: 
                setIcon(null);
                break;
            case 1:
                setIcon(X);
                break;
            case 2:
                setIcon(O);
                break;
        }
    }


}

HAVE A LOOK

sebklopfer
  • 23
  • 6