MPCBexplorer 2023-11-20 23:13 采纳率: 100%
浏览 3
已结题

提问:@FAML下控件返回null,如何处理?

我是一个javafx初学者,正在学习fxml
实践中控件startButton总是为null,这是为什么?求指教!
java:

package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;

import javafx.fxml.*;
import javafx.scene.Parent;

import javafx.scene.control.*;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/appilcation/welcome_scene.fxml"));
            loader.setController(this);
            Scene scene = new Scene(loader.load(),640,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
        welcome();
    }
    @FXML
    Button startButton;
    public static void main(String[] args) {
        launch(args);    
    }
    public void welcome() {
        startButton.setOnAction(e -> {
            System.out.println(1);
        });
    }
}


fxml:

<?xml version="1.0" encoding="UTF-8"?>


<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
  <children>
    <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
      <children>
        <Label alignment="CENTER" layoutX="149.0" layoutY="36.0" style="&#10;" text="MPCB_STAR_WARS" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
          <font>
            <Font size="37.0" />
          </font>
        </Label>
            <Button fx:id="startButton" layoutX="409.0" layoutY="107.0" mnemonicParsing="false" prefHeight="47.0" prefWidth="142.0" text="START_GAME" />
            <Button layoutX="409.0" layoutY="189.0" mnemonicParsing="false" prefHeight="47.0" prefWidth="142.0" text="SETTING" />
            <Button layoutX="409.0" layoutY="270.0" mnemonicParsing="false" prefHeight="47.0" prefWidth="142.0" text="HELP" />
      </children>
    </AnchorPane>
  </children>
</VBox>



  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 11月28日
    • 创建了问题 11月20日

    悬赏问题

    • ¥15 无源定位系统的时差估计误差标准差
    • ¥15 请问这个代码哪里有问题啊
    • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
    • ¥15 通过GaussianView进行结构微调消除虚频
    • ¥15 调用transformers库
    • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
    • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
    • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
    • ¥15 excel 上下按钮 显示行
    • ¥20 云卓h12pro 数传问题