How to Create a Tic Tac Toe Game in Java NetBeans
Вставка
- Опубліковано 7 лют 2025
- Create a TicTacToe games in Java NetBeans using Methods, call to methods, if statement, JFrame, jButtons, jPanels, jLabels and setting panels properties.
To support more videos from DJ Oamen, visit POamen Paypal
www.paypal.me/...
To Become a Channel member click on the Link below to JOIN:
/ @djoamen
Winning Solution for Player O below:
if (b1==("O") && b2 == ("O") && b3 == ("O"))
{
JOptionPane.showMessageDialog(this,"Player O Wins", "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
oCounter++;
gameScore();
jButton1.setBackground(Color.PINK);
jButton2.setBackground(Color.PINK);
jButton3.setBackground(Color.PINK);
}
if (b4==("O") && b5 == ("O") && b6 == ("O"))
{
JOptionPane.showMessageDialog(this,"Player O Wins", "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
oCounter++;
gameScore();
jButton4.setBackground(Color.YELLOW);
jButton5.setBackground(Color.YELLOW);
jButton6.setBackground(Color.YELLOW);
}
if (b7==("O") && b8 == ("O") && b9 == ("O"))
{
JOptionPane.showMessageDialog(this,"Player O Wins", "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
oCounter++;
gameScore();
jButton7.setBackground(Color.MAGENTA);
jButton8.setBackground(Color.MAGENTA);
jButton9.setBackground(Color.MAGENTA);
}
if (b1==("O") && b4 == ("O") && b7 == ("O"))
{
JOptionPane.showMessageDialog(this,"Player O Wins", "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
oCounter++;
gameScore();
jButton1.setBackground(Color.MAGENTA);
jButton4.setBackground(Color.MAGENTA);
jButton7.setBackground(Color.MAGENTA);
}
if (b2==("O") && b5 == ("O") && b8 == ("O"))
{
JOptionPane.showMessageDialog(this,"Player O Wins", "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
oCounter++;
gameScore();
jButton2.setBackground(Color.MAGENTA);
jButton5.setBackground(Color.MAGENTA);
jButton8.setBackground(Color.MAGENTA);
}
if (b3==("O") && b6 == ("O") && b9 == ("O"))
{
JOptionPane.showMessageDialog(this,"Player O Wins", "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
oCounter++;
gameScore();
jButton3.setBackground(Color.PINK);
jButton6.setBackground(Color.PINK);
jButton9.setBackground(Color.PINK);
}
if (b1==("O") && b5 == ("O") && b9 == ("O"))
{
JOptionPane.showMessageDialog(this,"Player O Wins", "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
oCounter++;
gameScore();
jButton1.setBackground(Color.PINK);
jButton5.setBackground(Color.PINK);
jButton9.setBackground(Color.PINK);
}
if (b3==("O") && b5 == ("O") && b7 == ("O"))
{
JOptionPane.showMessageDialog(this,"Player O Wins", "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
oCounter++;
gameScore();
jButton3.setBackground(Color.PINK);
jButton5.setBackground(Color.PINK);
jButton7.setBackground(Color.PINK);
}
thanks for the massive help you have provided to me through this tutorial
Thank you
Very nice video. Really helped me in class
Great 👍
The score counter is not working need help
Check out the function called "WinningGame". Why have you not subscribe to the channel😒
nice representation of UI
You're very welcome!
@@DJOamen
Can you make a video on "How to make this game as a application and could be shared..........................."
pls
I really
appreciate your hard work !!!!!!!
You are fantastic.I want to try it!!!
Nice one
thank you so much for the tutorial.this help me a lot for our defense
👍
You are the best man !!!💪🤘
Thank you
Amazing thank youu!
Thank you
Thnx sir...one more query in my exit button Yes option is working ...but NO oprtion is not working
What do you want NO to do?
Sir remain in the game bt when I click on no option it gets quit
I can't understand, what you want, but check your code
Sir, Is there also need of database to create tic tac TOC?
No, however, you can do whatever you like.
i have installed java jdk and did all the setup but jdk file is not opening when I run it.What should I do now?
Re-install your Java NetBeans, you can also see some tutorials on youtube of how to install Java NetBeans
Great video, any suggestions on code for a tie game?
Not that I know of
thankyouuuuu❤️
No problem
you are awesone :)
Thanks
Hi, I loved this video and have thought of some quality of life improvements, however I am struggling to implement them. For instnace: How would I prevent a player from overwriting an already occupied button? Please help me out and thanks for the great work.
You can disable the button or use on Mouse Enter event.
if(jButton1.getText()!="")
{
return;
}
Put this code at the start of each button function and change jButton1 for respective button function.
where did you declare the JOptionPane?
Right at the top: import javax.swing.JOptionPane;
when O win it will not show the winner, just can X to win leh?
See the description area of the video for support
oic thank you
sir im not able to get who o player wins??????im not getting msg of o player wins
See solution on the description area of the video
if the player "O" WIN the massage the player "o" dont appear???please i need help
See solution on the description area of the video
Hello sir, the O player can't win...
I already put this code on my winningGame function:
if(b1 == ("O") && b2 == ("O") && b3 == ("O"))
{
JOptionPane.showMessageDialog(this, "Player O wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
oCount++;
gameScore();
jButton1.setBackground(Color.RED);
jButton2.setBackground(Color.RED);
jButton3.setBackground(Color.RED);
}
and so on for the other combinations
The o player in my code still doesn't win.
Nevermind sir, mine is working already, instead of an "O" a letter, i misinputted a "0" a number. LOL. Thanks for the wonderful video
Thanks 👍
Helpppp only x show when i click the button
See codes in the description
Why does JOption has error for me
Carefully watch the video and check your code
Sir, my program "O" wins are not working
See the solution on the describtion area of the video: The codes is right there
@@DJOamen thanks sir
Great! :)
Thks
Exit no not working?????
Check your code
Why can't Player "O"win?
Check your code
@@DJOamen It also happen with me
Check the winning function for "O": Such as below:
if (b1==("O") && b2 == ("O") && b3 == ("O"))
{
JOptionPane.showMessageDialog(this,"Player O Wins", "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
oCounter++;
gameScore();
jButton1.setBackground(Color.PINK);
jButton2.setBackground(Color.PINK);
jButton3.setBackground(Color.PINK);
}
Check the winning function for "O": Such as below:
if (b1==("O") && b2 == ("O") && b3 == ("O"))
{
JOptionPane.showMessageDialog(this,"Player O Wins", "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
oCounter++;
gameScore();
jButton1.setBackground(Color.PINK);
jButton2.setBackground(Color.PINK);
jButton3.setBackground(Color.PINK);
}
@@DJOamen Should we merge that in X wins? (Sorry for my bad english)