如何修复JavaFX中的fx id错误?

huangapple 未分类评论48阅读模式
英文:

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(&quot;sample.fxml&quot;));
        primaryStage.setTitle(&quot;Hello World&quot;);
        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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import javafx.scene.control.Button?&gt;
&lt;?import javafx.scene.layout.AnchorPane?&gt;
&lt;?import javafx.scene.layout.ColumnConstraints?&gt;
&lt;?import javafx.scene.layout.GridPane?&gt;
&lt;?import javafx.scene.layout.RowConstraints?&gt;


&lt;GridPane alignment=&quot;center&quot; hgap=&quot;10&quot; vgap=&quot;10&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; xmlns=&quot;http://javafx.com/javafx/8.0.171&quot; fx:controller=&quot;sample.Controller&quot;&gt;
   &lt;columnConstraints&gt;
      &lt;ColumnConstraints /&gt;
   &lt;/columnConstraints&gt;
   &lt;rowConstraints&gt;
      &lt;RowConstraints /&gt;
   &lt;/rowConstraints&gt;
   &lt;children&gt;
      &lt;AnchorPane prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot;&gt;
         &lt;children&gt;
            ***&lt;Button fx:id=&quot;b1&quot; layoutX=&quot;68.0&quot; layoutY=&quot;69.0&quot; mnemonicParsing=&quot;false&quot; text=&quot;Button&quot; /&gt;***
         &lt;/children&gt;
      &lt;/AnchorPane&gt;
   &lt;/children&gt;
&lt;/GridPane&gt;

The start marked line is where the verdict is present. 如何修复JavaFX中的fx id错误?

huangapple
  • 本文由 发表于 2020年5月4日 04:08:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61580945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定