이 코드가 의도 한대로 작동하지 않는 이유에 대해 확신하지 못해서 알아낼 수 있기를 바랍니다. 나는 jbuttons를 사용하는 일련의 자습서에서 tictactoe 게임을 연구 중이다. 내가 가지고있는 것은 메인 클래스에서 버튼을 클릭 할 때마다 "checksforwin"이라는 메서드입니다. print 문 사용하기 getText(). equals ("_") 조건은 제대로 작동하지 않지만 버튼을 클릭 할 때마다 내 메서드가 실행되는 것을 발견했습니다.TicTacToe 게임에 문제가 있습니까? getText(). jButton을 사용하여 equals() 사용
이 코드는 내 tictactoe.java의 일부이며 제대로 작동합니다. 이 이벤트는 각 버튼에 대해 9 번 복사됩니다.
public void button1ActionPerformed(ActionEvent e) {
if(button1.getText().equals("")){
if(Main.playerTurn==true){
button1.setText("X");
Main.checkforwin();
Main.playerTurn = false;
}else{
button1.setText("O");
Main.checkforwin();
Main.playerTurn = true;
}
}
}
이 코드는 checkforwin 메소드가 포함 된 main.java의 일부입니다. 플레이어 1과 컴퓨터 2 (player2)에 대해 tictactoe에서 각 가능한 승리에 대해 코드의 승리 청크에 대한 확인이 여러 번 반복됩니다.
public class Main {
public static boolean playerTurn = true;
public static boolean playerWon = false;
public static boolean computerWon = false;
public static tictactoe board = new tictactoe();
public static void checkforwin(){
System.out.println("testing1");
//horizontal row 1
if(board.button1.getText().equals("X")){
System.out.println("testing2");
if(board.button2.getText().equals("X")){
if(board.button3.getText().equals("X")){
playerWon = true;
computerWon = false;
System.out.println("Player 1 won");
}
}
}
}
방법은 출력 testing1 버튼을 클릭 할 때마다, 그러나 그것은 조건 내에서 인쇄하지 않을 것입니다.
도움이나 조언을 주시면 감사하겠습니다.
에 저장하든 'System.out.println (board.button1.getText())'. –
버튼 텍스트 인쇄 또는 코드 디버깅은 어떻게됩니까? – Andreas
빠른 응답을 주셔서 감사합니다, 나는 checkforwin 메서드의 맨 위에 놓았습니다. 그러나 System.out.println ("임의의 텍스트")으로 바꾸면 아무것도 인쇄되지 않습니다. –