英文:
How to fix fx id bug in javafx?
问题
我试图创建一个简单的JavaFX文件。它包含了主类、一个控制器和一个XML文件。
我在场景构建器中打开了XML文件。然后在锚点面板上添加了一个按钮。给按钮一个名为b1的id。
在我使用@FXML将该按钮添加到我的控制器类之后,我在XML文件中看到了一些错误。它显示为 -
无法将javafx.scene.control.Button设置到字段'b1
这是我的主类
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是控制器类
package sample;
import javafx.fxml.FXML;
import java.awt.*;
public class Controller {
@FXML
private Button b1;
}
而这是XML类,其中包含判决结果
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Button fx:id="b1" layoutX="68.0" layoutY="69.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>
</children>
</GridPane>
标记为"start marked line"的行是判决结果所在的位置。【我已添加了截图。另外,判决结果的文本位于开头】。【截图链接在这里】(https://i.stack.imgur.com/8CWok.png)。
英文:
I was trying to make a simple JavaFX file. It contains the main class, a controller, and an XML file.
I opened the XML file in the scene builder. Then added a button on the anchor pane. Gave the button an id called b1.
After I added that that button to my controller class using @FXML, I see some errors in the XML file. It is denoted as -
> Cannot set javafx.scene.control.Button to field 'b1
This is my main class
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is the controller class
package sample;
import javafx.fxml.FXML;
import java.awt.*;
public class Controller {
@FXML
private Button b1;
}
And this is the XML class where the verdict is located
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
***<Button fx:id="b1" layoutX="68.0" layoutY="69.0" mnemonicParsing="false" text="Button" />***
</children>
</AnchorPane>
</children>
</GridPane>
The start marked line is where the verdict is present.
专注分享java语言的经验与见解,让所有开发者获益!
评论