这是我的总体代码,当我第一次运行我的应用程序时,它可以正常工作,但是当我重置它并倾向于单击任何按钮时,应用程序就会卡住而不响应。单击重置后,我无法单击主窗口上的任何预设按钮,我必须将其强制关闭,因为它卡住了。我检测到重置游戏后,当我单击按钮时,它会生成事件以进入startgame()函数,但正在开始循环并在同一函数中生成多个事件
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("TIC TAC TOE");
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
controller.java
package sample;
import com.jfoenix.controls.JFXButton;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import java.util.concurrent.TimeUnit;
public class Controller {
@FXML
private JFXButton A1;
@FXML
private JFXButton A2;
@FXML
private JFXButton A3;
@FXML
private JFXButton B1;
@FXML
private JFXButton B2;
@FXML
private JFXButton B3;
@FXML
private JFXButton C1;
@FXML
private JFXButton C2;
@FXML
private JFXButton C3;
@FXML
private Label status;
@FXML
private Button exit;
@FXML
private Button reset;
public JFXButton button;
int turn=1;
public void startgame(ActionEvent event)
{
if(turn==1)
{
status.setText("Start the game :)");
}
button = ((JFXButton)event.getSource());
if (((turn%2==0) && (( button.getText().equals(" ")))))
{
status.setText("Its Player 1`s Turn");
button.setText("O");
turn++;
}
else if ((turn%2!=0) && (( button.getText().equals(" "))))
{ status.setText("Its Player 2`s Turn");
button.setText("X");
turn++;
}
else {
status.setText("already filled");
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
if(turn%2==0)
{ status.setText("Player 2's turn");}
else
{ status.setText("Player 1's turn");}
turn--;
startgame(event);
}
if((A1.getText().equals("X")&&A2.getText().equals("X")&&A3.getText().equals("X"))||(B1.getText().equals("X")&&B2.getText().equals("X")&&B3.getText().equals("X"))||(C1.getText().equals("X")&&C2.getText().equals("X")&&C3.getText().equals("X"))||(A1.getText().equals("X")&&B2.getText().equals("X")&&C3.getText().equals("X"))|| (A3.getText().equals("X")&&B2.getText().equals("X")&&C1.getText().equals("X"))||(A2.getText().equals("X")&&B2.getText().equals("X")&&C2.getText().equals("X"))||(A1.getText().equals("X")&&B1.getText().equals("X")&&C1.getText().equals("X"))||(A2.getText().equals("X")&&B2.getText().equals("X")&&C2.getText().equals("X"))||(A3.getText().equals("X")&&B3.getText().equals("X")&&C3.getText().equals("X")))
{
status.setText("game ended \n p1 Won ");
lock();
}
if((A1.getText().equals("O")&&A2.getText().equals("O")&&A3.getText().equals("O"))||(B1.getText().equals("O")&&B2.getText().equals("O")&&B3.getText().equals("O"))||(C1.getText().equals("O")&&C2.getText().equals("O")&&C3.getText().equals("O"))||(A1.getText().equals("O")&&B2.getText().equals("O")&&C3.getText().equals("O"))|| (A3.getText().equals("O")&&B2.getText().equals("O")&&C1.getText().equals("O"))||(A2.getText().equals("O")&&B2.getText().equals("O")&&C2.getText().equals("O"))||(A1.getText().equals("O")&&B1.getText().equals("O")&&C1.getText().equals("O"))||(A2.getText().equals("O")&&B2.getText().equals("O")&&C2.getText().equals("O"))||(A3.getText().equals("O")&&B3.getText().equals("O")&&C3.getText().equals("O")))
{
status.setText("game ended \n p2 Won");
lock();
}
else if (turn>9)
{
status.setText("game Drawn");
lock();
}
}
private void lock()
{
A1.setDisable(true);
A2.setDisable(true);
A3.setDisable(true);
B1.setDisable(true);
B2.setDisable(true);
B3.setDisable(true);
C1.setDisable(true);
C2.setDisable(true);
C3.setDisable(true);
}
@FXML
void exitGame(ActionEvent event) {
System.exit(0);
}
@FXML
void resetGame(ActionEvent event)
{
A1.setDisable(false);
A2.setDisable(false);
A3.setDisable(false);
B1.setDisable(false);
B2.setDisable(false);
B3.setDisable(false);
C1.setDisable(false);
C2.setDisable(false);
C3.setDisable(false);
A1.setText("");
A2.setText("");
A3.setText("");
B1.setText("");
B2.setText("");
B3.setText("");
C1.setText("");
C2.setText("");
C3.setText("");
status.setText("game restarted");
turn=1;
}
}
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<HBox layoutY="27.0" prefHeight="110.0" prefWidth="500.0">
<children>
<JFXButton fx:id="A1" onAction="#startgame" prefHeight="113.0" prefWidth="165.0" text=" " />
<Separator orientation="VERTICAL" prefHeight="128.0" prefWidth="6.0" style="-fx-background-color: BLACK;" />
<JFXButton fx:id="A2" onAction="#startgame" prefHeight="117.0" prefWidth="162.0" text=" " />
<Separator orientation="VERTICAL" prefHeight="200.0" style="-fx-background-color: BLACK;" />
<JFXButton fx:id="A3" onAction="#startgame" prefHeight="112.0" prefWidth="163.0" text=" " />
</children>
</HBox>
<Label fx:id="status" alignment="BOTTOM_LEFT" layoutX="113.0" layoutY="372.0" prefHeight="49.0" prefWidth="253.0" textAlignment="JUSTIFY" />
<Separator layoutX="-1.0" layoutY="137.0" prefHeight="3.0" prefWidth="500.0" style="-fx-background-color: BLACK;" />
<HBox layoutX="-1.0" layoutY="140.0" prefHeight="116.0" prefWidth="500.0">
<children>
<JFXButton fx:id="B1" onAction="#startgame" prefHeight="107.0" prefWidth="166.0" text=" " />
<Separator orientation="VERTICAL" prefHeight="119.0" prefWidth="6.0" style="-fx-background-color: BLACK;" />
<JFXButton fx:id="B2" onAction="#startgame" prefHeight="109.0" prefWidth="163.0" text=" " />
<Separator orientation="VERTICAL" prefHeight="126.0" prefWidth="6.0" style="-fx-background-color: BLACK;" />
<JFXButton fx:id="B3" onAction="#startgame" prefHeight="107.0" prefWidth="163.0" text=" " />
</children>
</HBox>
<Separator layoutX="4.0" layoutY="249.0" prefHeight="3.0" prefWidth="500.0" style="-fx-background-color: BLACK;" />
<HBox layoutX="-1.0" layoutY="249.0" prefHeight="116.0" prefWidth="500.0">
<children>
<JFXButton fx:id="C1" onAction="#startgame" prefHeight="118.0" prefWidth="165.0" text=" " />
<Separator orientation="VERTICAL" prefHeight="200.0" style="-fx-background-color: BLACK;" />
<JFXButton fx:id="C2" onAction="#startgame" prefHeight="117.0" prefWidth="162.0" text=" " />
<Separator orientation="VERTICAL" prefHeight="200.0" style="-fx-background-color: BLACK;" />
<JFXButton fx:id="C3" onAction="#startgame" prefHeight="117.0" prefWidth="162.0" text=" " />
</children>
</HBox>
<Button fx:id="exit" layoutX="415.0" layoutY="447.0" mnemonicParsing="false" onAction="#exitGame" text="Exit" />
<Button fx:id="reset" layoutX="24.0" layoutY="447.0" mnemonicParsing="false" onAction="#resetGame" text="RESET" />
</children>
</AnchorPane>
答案 0 :(得分:0)
在测试验证中,您测试按钮是否等于一个空格:x.equal(" ")
(startGame)
但是当您重置游戏时,会将ButtonText设置为空:A1.setText("");
(resetGame)
"" != " "
,因此您需要在重置的每个按钮中添加一个空格或确认其为空。
此外,您在startGame的else中调用startGame,所以由于条件为假,您将陷入无限循环