I have the same codes in every button in my tick tacto game. How to do it?

This tictactoe program is a 2player game. The GUI I made was a frame and buttons, after which I started coding. In fact, my program works in this form of coding.

private String letter= " "; private int count= 0; private void btn7ActionPerformed(java.awt.event.ActionEvent evt) { count++; if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){ letter = "X"; } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){ letter = "O"; } if(evt.getSource() == btn1){ btn1.setText(letter); } else if(evt.getSource() == btn2){ btn2.setText(letter); } else if(evt.getSource() == btn3){ btn3.setText(letter); } else if(evt.getSource() == btn4){ btn4.setText(letter); } else if(evt.getSource() == btn5){ btn5.setText(letter); } else if(evt.getSource() == btn6){ btn6.setText(letter); } else if(evt.getSource() == btn7){ btn7.setText(letter); } else if(evt.getSource() == btn8){ btn8.setText(letter); } else if(evt.getSource() == btn9){ btn9.setText(letter); } } private void btn1ActionPerformed(java.awt.event.ActionEvent evt) { count++; if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){ letter = "X"; } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){ letter = "O"; } if(evt.getSource() == btn1){ btn1.setText(letter); } else if(evt.getSource() == btn2){ btn2.setText(letter); } else if(evt.getSource() == btn3){ btn3.setText(letter); } else if(evt.getSource() == btn4){ btn4.setText(letter); } else if(evt.getSource() == btn5){ btn5.setText(letter); } else if(evt.getSource() == btn6){ btn6.setText(letter); } else if(evt.getSource() == btn7){ btn7.setText(letter); } else if(evt.getSource() == btn8){ btn8.setText(letter); } else if(evt.getSource() == btn9){ btn9.setText(letter); 

Yes, it really works, but my problem with these codes is that I need to put the codes in each button, for example. button7, button1 (this is just an example of my buttons), which are codes - this is the action performed by the button.

I need only one command that each button executes, and I do not want to copy the insert in each button, because they are identical codes. I think there is code for this, what do you think? Please, help!

+4
source share
2 answers

So, basically, you want to collapse your logic into one handler. This handler needs a single link to the button in which it acts ...

Sort of...

 public class ActionHandler implements ActionListener { private JButton master; public ActionHandler(JButton master) { this.master = master; } protected void setText(String text) { master.setText(text); } @Override public void actionPerformed(ActionEvent e) { String text = null; count++; if (count / 2f == Math.round(count / 2f)) { text = "O"; } else { text = "X"; } setText(text); } } 

Now you can customize the code, which may be different, this is just an example ...

 buttons = new JButton[9]; // You can use this to reset the board ;) setLayout(new GridLayout(3, 3)); for (int index = 0; index < 9; index++) { JButton btn = new JButton(Integer.toString(index)); buttons[index] = btn; btn.addActionListener(new ActionHandler(btn)); add(btn); } 

The main idea is that when you create a new button, you assign it your own ActionHandler , passing it a link to the button. This allows the action handler to control the button depending on the state of the game.

+2
source

I really don't know how you are trying to solve this logic of the game, although your code can be compressed into this form:

 int count = -1; String letter = ""; JButton[] button = new JButton[9]; // These three being your Instance Variables for (int i = 0; i < 9; i++, counter++) { final int counter = i; button[i] = new JButton(""); button[i].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { count++; if (count % 2 != 0) letter = "X"; else letter = "O"; button[counter].setText(letter); } }); } 
+3
source

Source: https://habr.com/ru/post/1443155/


All Articles