gpt4 book ai didi

JavaFX 线程问题

转载 作者:行者123 更新时间:2023-12-01 10:50:06 25 4
gpt4 key购买 nike

我真的尽力不在这里寻求帮助,除非我绝望到学校作业失败的地步,成为一名新程序员。话虽这么说,我花了三天时间试图解决线程问题。我试图实例化一个单独包中的 javafx 类,并不断遇到可怕的“java.lang.IllegalStateException:仅在事件线程上允许此操作;currentThread = main”异常。

I have tried calling theGamePreBoard.start(new Stage()), which doesnt work, and i have also tried calling its start method during construction of that object with a new Stage() passed in during construction. Please help!!! 

How can i instantiate this PreBoard() class and get it's start method to run without throwing this?

主类:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package battleship.model;
import battleship.viewcon.*;
import javafx.stage.Stage;

/**
*
* @author foolishklown
*/
public class MainApp {
Player player1;
Player player2;
Board board1;
Board board2;
BattleshipGame theGame;
PreBoard theGamePreBoard;

public void go() {
theGame = new BattleshipGame();
theGamePreBoard = new PreBoard();
theGamePreBoard.start(new Stage());
System.out.println(theGamePreBoard);
theGamePreBoard.setBattleshipGame(theGame);
}

public static void main(String[] args) {
MainApp app = new MainApp();
app.go();
}
}

PreBoard 类:

/*
* PreBoard object. This is the starter class for the different JavaFX stages (different windows - username screen,
* and each players window)
* After first username input, this class hides and calls on a new P1Board object
*/
package battleship.viewcon;
import battleship.model.*;

import javafx.geometry.Insets;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;

/**
*
*
* @author c-dub
*/
public class PreBoard extends Application {
private boolean turn; // field to determine which players name to put into which board
private String player;
private Button hideBtn;
private Button showBtn;
private TextField userText;
private ViewCon controller;
private P1Board p1B;
private P2Board p2B;
private BattleshipGame game;
private Stage theStage;


@Override
public void start(Stage primaryStage) {
turn = false;
p1B = new P1Board();
p2B = new P2Board();
controller = new ViewCon();
controller.setp1(p1B);
controller.setp2(p2B);
controller.setPreB(this);
this.game = controller.getBattleshipGame();

primaryStage.setTitle("Battleship setup"); //Main stage (window container)
//Gridpane for using rows/columns for child node placement
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER_LEFT);
grid.setHgap(10);
grid.setVgap(5);
grid.setPadding(new Insets(100, 25, 25, 25));

// label in window
Text sceneTitle = new Text("Setup");
sceneTitle.setId("setup-text");
grid.add(sceneTitle, 0, 0, 2, 1);

// label and textfield
Label userName = new Label("Enter UserName:");
userName.setId("user-name");
grid.add(userName, 0, 1);
TextField userTextField = new TextField();
userTextField.setId("text-field");
grid.add(userTextField, 0, 2);

// button for setup, with actionListener to save player name or default if its left blank
Button setupBtn = new Button("Setup Board");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_LEFT);
hbBtn.getChildren().add(setupBtn);
grid.add(hbBtn, 0, 3);

setupBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
// determine which player name to use to pass into which player board
if(turn == false) {
String temp1 = userTextField.getText();
if(temp1.equals("")) {
player = "Player1";
} else {
player = temp1;
}
controller.setPlayer1(player);
game.setPlayer1(player);
turn = true;
Stage stage = new Stage();
p1B.start(stage);
grid.getChildren().remove(userTextField);
userText = new TextField();
userText.setId("text-field2");
grid.add(userText, 0, 2);
hideBtn.fire();
} else {

String temp2 = userText.getText();
if(temp2.equals("")) {
player = "Player2";
} else {
player = temp2;
}
controller.setPlayer2(player);
game.setPlayer2(player);
Stage stage2 = new Stage();
p2B.start(stage2);
hideBtn.fire();
}
}
});


hideBtn = new Button();
hideBtn.setId("hideBtn");
hideBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
primaryStage.hide();
}
});

showBtn = new Button();
showBtn.setId("showBtn");
showBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
primaryStage.show();
}
});

controller.setPreShowBtn(showBtn);
controller.setPreHideBtn(hideBtn);
// Add the entire scene into the main window(stage) after setting the scene dimensions
Scene scene = new Scene(grid, 580, 200);
primaryStage.setScene(scene);
// Attach css stylesheet
scene.getStylesheets().add(PreBoard.class.getResource("styles/PreBoardStyle.css").toExternalForm());
// Show this window(stage) upon instantiation
primaryStage.show();
}

public void setLink(ViewCon v) {
this.controller = v;
}

public static void main(String[] args) {
Application.launch(args);
}

public void setBattleshipGame(BattleshipGame b) {
this.game = b;
}



}

最佳答案

我认为这与线程没有任何关系:我看不出您有任何理由在此应用程序中创建另一个线程。您似乎缺少的部分是 JavaFX 应用程序的实际生命周期。 (您可能需要了解一些关于 JavaFX 如何管理线程的信息,但这里有点偶然。)

Application 类代表整个应用程序。您的应用程序通常应该只有一个 Application 子类和该类的一个实例。当您调用静态 Application.launch() 方法时(或者当您从命令行执行 Application 子类时,这实际上会调用 为您启动)。

何时 launch被调用时,JavaFX 工具包(包括 FX 应用程序线程)启动。系统会为您创建一个Application 子类的实例,然后在 FX 应用程序线程上的该实例上调用 start(...)

这意味着 start(...) 方法是 JavaFX 应用程序的入口点(启动)。由于这里最常见的操作是在窗口中显示某些内容,因此为了方便起见,将窗口 (Stage) 传递到此方法中:但是您可以忽略它,如果您愿意,只需创建自己的窗口.

典型的 start(...) 方法应该非常短,通常只会创建一些 UI(可能在另一个类或 FXML 文件中定义),将该 UI 放入场景中,并将场景显示在舞台上。在更复杂的应用程序中,您将在此处创建模型类的实例,创建一些 View 和 Controller ,为 Controller 提供对模型的引用,然后组装 View 。

有关简单的结构示例,请参阅我对 Java: How do I start a standalone application from the current one when both are in the same package? 的回答(我认为这是一个类似的问题)。

关于JavaFX 线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33966259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com