编辑:有很多代码,如前所述,除了显示的内容外,没有任何更改,但是我已经坚持了两天,不确定是什么原因造成的,所以我上传了项目到git,以便该项目可以为任何足以帮助我https://github.com/AdamSteele-commits/JavaFX-Issue的人运行,请不要因为“ GUI.java”文件是多余的,我忘了删除它
[以前,在尝试进行此更改之前,该游戏是基于文本的。玩家输入他们想做的事情,一个简单的基于文本的游戏。 “往东走”等
以下是启动游戏的代码的样子
package zuul;
/**
*
* @author Adam
*/
public class Main {
/**
* The main entry point
* @param args the command line arguments
*/
public static void main(String[] args) {
String language;
String country;
// TODO improve error handling
if (args.length != 2) {
language = "en";
country = "US";
} else {
language = args[0];
country = args[1];
}
// Start specific game
new zuul.mygame.MyGame(language, country).play();
}
}
然后,我尝试实现一个非常基本的,两个按钮的JavaFX屏幕,其中一个按钮启动了游戏。这样做确实正确,但是现在不再可以单击游戏窗口的文本框。除显示的内容外,其他任何内容均未进行任何更改。这是新代码。
package zuul;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
/**
*
* @author Adam
*/
public class Main extends Application{
private Label myLabel = new Label("Launches World of Zuul
pre-set game");
private Label myLabel2 = new Label("WORLD OF ZUUL");
private Label myLabel3 = new Label("Launches a CSV game
file");
/**
* The main entry point
* @param args the command line arguments
*/
public static void main(String[] args) { launch ( args );}
{
}
@Override
public void start(Stage MenuStage) throws Exception
{
// Create a Button or any control item
Button preButton = new Button("Preset");
Button csvButton = new Button("CSV");
// Create a new grid pane
GridPane pane = new GridPane();
pane.setPadding(new Insets(10, 10, 10, 10));
pane.setMinSize(300, 300);
pane.setVgap(10);
pane.setHgap(10);
//set an action on the button using method reference
preButton.setOnAction(this::buttonClick);
csvButton.setOnAction(this::buttonClick2);
// Add the button and label into the pane
pane.add(myLabel, 20, 31);
pane.add(myLabel2, 20, 0);
pane.add(preButton, 20, 30);
pane.add(csvButton, 20, 40);
pane.add(myLabel3, 20, 41);
// JavaFX must have a Scene (window content) inside a Stage (window)
Scene scene = new Scene(pane, 800,800);
MenuStage.setTitle("JavaFX Example");
MenuStage.setScene(scene);
// Show the Stage (window)
MenuStage.show();
}
/**
*/
private void buttonClick(ActionEvent event)
{
// Launches the game
String language;
String country;
language = "en";
country = "US";
// Start specific game
new zuul.mygame.MyGame(language, country).play();
}
private void buttonClick2(ActionEvent event)
{
// Launches the game
myLabel3.setText("Test");
}
}
一旦我意识到游戏现在已经坏了,我恢复了新班级,回到了旧班级,并尝试再次启动游戏。但是现在出现此错误。
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
at javafx.application.Platform.runLater(Platform.java:83)
at bluej.runtime.ExecServer.runOnTargetThread(ExecServer.java:902)
at bluej.runtime.ExecServer.access$700(ExecServer.java:78)
at bluej.runtime.ExecServer$3.run(ExecServer.java:767)
试图发布的游戏与JavaFX无关(当前)。项目中唯一已更改的内容是显示的内容。任何指导将不胜感激
这些错误仅在我进行了javafx更改,启动游戏,意识到它不再起作用,恢复为原始代码然后启动它之后出现(以前没有问题)。
错误在我关闭bluej并重新打开后消失,但是随后我留下的原始代码是im开头的(未实现JavaFX,这是代码的第一段)。
我稍微弄弄了您的代码,去掉了main方法下的括号。这个(下面的代码)对我来说很好。该按钮确实会更改标签文本。我无法测试启动器的功能,但其他一切看起来都不错。
您是否尝试过独立于BlueJ运行程序? (其他IDE或命令行?)
package zuul;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
/**
* @author Adam
*/
public class Main extends Application {
private Label myLabel = new Label("Launches World of Zuul pre-set game");
private Label myLabel2 = new Label("WORLD OF ZUUL");
private Label myLabel3 = new Label("Launches a CSV game file");
/**
* The main entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) { launch(args); }
@Override
public void start(Stage MenuStage) throws Exception {
// Create a Button or any control item
Button preButton = new Button("Preset");
Button csvButton = new Button("CSV");
// Create a new grid pane
GridPane pane = new GridPane();
pane.setPadding(new Insets(10, 10, 10, 10));
pane.setMinSize(300, 300);
pane.setVgap(10);
pane.setHgap(10);
// set an action on the button using method reference
preButton.setOnAction(this::buttonClick);
csvButton.setOnAction(this::buttonClick2);
// Add the button and label into the pane
pane.add(myLabel, 20, 31);
pane.add(myLabel2, 20, 0);
pane.add(preButton, 20, 30);
pane.add(csvButton, 20, 40);
pane.add(myLabel3, 20, 41);
// JavaFX must have a Scene (window content) inside a Stage (window)
Scene scene = new Scene(pane, 800, 800);
MenuStage.setTitle("JavaFX Example");
MenuStage.setScene(scene);
// Show the Stage (window)
MenuStage.show();
}
private void buttonClick(ActionEvent event) {
// Launches the game
String language;
String country;
language = "en";
country = "US";
// Start specific game
//new zuul.mygame.MyGame(language, country).play(); // commented out because I didn't have the game itself
}
private void buttonClick2(ActionEvent event) {
// Launches the game
myLabel3.setText("Test");
}
}