空白窗口 JavaFX



如何解决这个问题,运行项目时会打开一个空白窗口

这是我的主要:

package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;

public class Main extends Application {
private Stage primaryStage;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
primaryStage.show();
}
public void mainWindow() {
try {
FXMLLoader loader =new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));
AnchorPane pane = loader.load();
MainWindowController mainWindowController = loader.getController();
mainWindowController.setMain(this);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

这是我的观点 XML:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="1080.0" prefWidth="1920.0" 
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="application.MainWindowController">
<children>
<Label fx:id="label" alignment="CENTER" layoutY="420.0" text="Label" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<font>
<Font size="71.0" />
</font>
</Label>
<HBox alignment="CENTER" layoutX="774.0" layoutY="540.0" spacing="20.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<TextField layoutX="774.0" layoutY="540.0" />
<Button layoutX="1268.0" layoutY="540.0" mnemonicParsing="false" onAction="#handleButton" text="Click Me!" />
</children>
</HBox>
</children>
</AnchorPane>

这是我的控制器:

package application;
import java.awt.Label;
import java.awt.TextField;
import javafx.fxml.FXML;
public class MainWindowController {
@FXML private Label label;
@FXML private TextField field;

private Main main;
public void setMain(Main main) {
this.main = main;
}
public void handleButton() {
String text = field.getText();
label.setText(text);
}
}

这个链接有一个链接,指向 hastebin 中的所有这三个代码,如果你想以这种方式查看(https://pastebin.com/raw/hbGBJUng),我是 Java 和 JavaFX 的新手,不确定我做错了什么,因为当我在 Eclipse 中运行项目时,它向我显示一个空窗口,就像我在场景生成器中打开 XML 时一样, 我添加了一个按钮、一个标签和一个文本字段。

主窗口中的代码应该处于启动状态,目前它没有运行,这只会导致启动代码运行,因此不会暂存任何内容,并且显示空阶段。

你只显示没有场景的舞台,我们可以说空场景,所以如果你需要保存你的代码,我的意思是保存你的方法主窗口,只需将你的舞台作为参数传递,如下所示:

package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;

public class Main extends Application {
private Stage primaryStage;
@Override
public void start(Stage primaryStage) {
mainWindow(primaryStage);  
}
public void mainWindow(Stage primaryStage) {
try {
FXMLLoader loader =new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));
AnchorPane pane = loader.load();
MainWindowController mainWindowController = loader.getController();
mainWindowController.setMain(this);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

最新更新