JavaFX(使用fxml)和自定义类不会加载

时间:2022-11-18 17:01:31

I am attempting to upload my code, but having issues with IntelliJ and gitHub. But the issue lies in the custom class not being found when I'm trying to load the second scene that contains a custom class. Any examples out there that have multiple scenes and custom classes that can lead me down the right path?

我试图上传我的代码,但有IntelliJ和gitHub的问题。但问题在于,当我尝试加载包含自定义类的第二个场景时,找不到自定义类。那里有任何有多个场景和自定义类的例子可以让我走上正确的道路吗?

I used this sample to start with, and then added my custom class (extends TextField), but as soon as i click the button to go to the second scene it crashes.

我使用这个示例开始,然后添加了我的自定义类(扩展TextField),但是当我单击按钮转到第二个场景时它就会崩溃。

http://www.javafxtutorials.com/tutorials/switching-to-different-screens-in-javafx-and-fxml/

controller class

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import sample.numberTextField;

import java.io.IOException;

public class Controller {

@FXML
Label lbl1;

@FXML
Button btn1;

@FXML
Label lbl2;

@FXML
Button btn2;

@FXML
numberTextField txtField1;

@FXML
public void handleButtonClick(ActionEvent event) throws IOException {
    Stage stage;
    Parent root;

    if (event.getSource() == btn1) {
        stage = (Stage) btn1.getScene().getWindow();

        root = FXMLLoader.load(getClass().getResource("sample2.fxml"));
    } else {
        stage = (Stage) btn2.getScene().getWindow();
        root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    }

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();

}
}

fxml

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import sample.numberTextField?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <AnchorPane prefHeight="150.0" prefWidth="250.0" style="-fx-background-color: blue;">
         <children>
            <Label fx:id="lbl2" layoutX="81.0" layoutY="29.0" text="This is scene 2" textFill="WHITE" />
            <Button fx:id="btn2" layoutX="53.0" layoutY="101.0" mnemonicParsing="false" onAction="#handleButtonClick" text="click to go to scene 1" />
            <numberTextField fx:id="txtField1" layoutX="44.0" layoutY="55.0" />
         </children>
      </AnchorPane>
   </children>
</GridPane>

1 个解决方案

#1


It has been brought to my attention that the naming convention I used on my class file numberTextField needed to be altered to NumberTextField. As soon as I did this, it began working as planned. You have to love case sensitivity, and I don't remember seeing anything anywhere that stated I couldn't do that but all in all I have it working.

我注意到我在类文件numberTextField上使用的命名约定需要更改为NumberTextField。我一做到这一点就开始按计划工作了。你必须喜欢区分大小写,而且我不记得在任何地方看到任何声明我无法做到这一点的东西,但总而言之,我有它的工作。

Thanks everyone for trying to help.

谢谢大家帮忙。

#1


It has been brought to my attention that the naming convention I used on my class file numberTextField needed to be altered to NumberTextField. As soon as I did this, it began working as planned. You have to love case sensitivity, and I don't remember seeing anything anywhere that stated I couldn't do that but all in all I have it working.

我注意到我在类文件numberTextField上使用的命名约定需要更改为NumberTextField。我一做到这一点就开始按计划工作了。你必须喜欢区分大小写,而且我不记得在任何地方看到任何声明我无法做到这一点的东西,但总而言之,我有它的工作。

Thanks everyone for trying to help.

谢谢大家帮忙。