Hello im new here i am making a small tictactoe game where i have made a gui as follows:
public static void main(String[] args) {
Frame frame1 =new Frame("TickTacToe");
frame1.setLayout(null);
frame1.setBounds(250,150,500,500);
frame1.setVisible(true);
frame1.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
final Button button11 = new Button("");
button11.addActionListener(null);
final Button button12 = new Button("");
button11.addActionListener(null);
final Button button13 = new Button("");
button11.addActionListener(null);
final Button button21 = new Button("");
button11.addActionListener(null);
final Button button22 = new Button("");
button11.addActionListener(null);
final Button button23 = new Button("");
button11.addActionListener(null);
final Button button31 = new Button("");
button11.addActionListener(null);
final Button button32 = new Button("");
button11.addActionListener(null);
final Button button33 = new Button("");
button11.addActionListener(null);
button11.setBounds(100, 100, 80, 70);
button12.setBounds(100, 200, 80, 70);
button13.setBounds(100, 300, 80, 70);
button21.setBounds(200, 100, 80, 70);
button22.setBounds(200, 200, 80, 70);
button23.setBounds(200, 300, 80, 70);
button31.setBounds(300, 100, 80, 70);
button32.setBounds(300, 200, 80, 70);
button33.setBounds(300, 300, 80, 70);
frame1.add(button11);
frame1.add(button12);
frame1.add(button13);
frame1.add(button21);
frame1.add(button22);
frame1.add(button23);
frame1.add(button31);
frame1.add(button32);
frame1.add(button33);
}
I want to add action listeners to the buttons but not in this void ideall even a different class so i can create a way to run a loop of functions such as a player turn changer where if it is turn one it will set button text to x and o if it is the otheI know more orless the code i would need to use but i cant figure out a way to use the gui from anzwhere else than its own void. I dont quite know what im searching for so any help is much appreciated.
First of all, this program structure is pretty bad. From the looks of it I can tell you came from a procedural programming language like C or Basic or something of the sort. Java is all about Object Oriented architecture. Java can run procedurally but it was not meant to be. First thing that I would start with is get out of main(). Here is a good way to do that:
TicTacToe might seem simple but it has a lot of things going on at once, people are clicking buttons so you need ActionListeners, you need to update the UI of the screen after every move, you need to check to make sure each move is valid, you need to check for a win after every move and much much more. This is almost impossible to do inside of main.
It’s generally a bad idea to add any sort of Component straight into a JFrame. Its better to put a JPanel inside of a JFrame and then add the components to the JPanel.
Try making a class structure for your game. Here is what I would do:
If I was you I would read a book about Java Game Development or take a class. If you want to get good at Java that is a good place to start. Your missing a lot of crucial knowledge that you need to do even a simple task such as make a TicTacToe game.