JavaFX 标签中的场景生成器不起作用
Scene builder in JavaFX label not working
尊敬的先生,
我是一个完整的初学者,通过一些教程学习,同时遵循它我试图制作一个简单的按钮,将“Hello WOrld”打印到标签上。但是当我 运行 按钮出现的代码时,无论我按多少次它都不会打印 helloWorld ...
1.Below是HelloWorld.JAVA代码:
enter code here
package helloworld;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
@Override
public void start(Stage primaryStage){
try {
Parent root = FXMLLoader.load(getClass().getResource("/helloworld/Tester.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
2.Below是TesterController.JAVA代码:
enter code here
package helloworld;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class TesterController implements Initializable {
@FXML
private Label l1;
void printsHelloWorld(ActionEvent event){
l1.setText("Hello World");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
3.Below是Tester.fxml代码:
enter code here
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="helloworld.TesterController">
<children>
<Button layoutX="201.0" layoutY="155.0" mnemonicParsing="false" prefHeight="161.0" prefWidth="175.0" text="Button" />
<Label layoutX="170.0" layoutY="59.0" prefHeight="46.0" prefWidth="237.0" />
</children>
</AnchorPane>
4.Below是运行之后的输出框文字:
enter code here
ant -f C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld jfxsa-run
init:
Deleting: C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld\build\built-jar.properties
compile:
Detected JavaFX Ant API version 1.3
jfx-deployment:
jar:
Copying 12 files to C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld\dist\run141170950
jfx-project-run:
Executing C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld\dist\run141170950\HelloWorld.jar using platform C:\Program Files\Java\jdk1.8.0_111\jre/bin/java
Jul 24, 2021 4:31:25 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.171 by JavaFX runtime of version 8.0.111
Deleting directory C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld\dist\run141170950
jfxsa-run:
BUILD SUCCESSFUL (total time: 8 seconds)
您遗漏了三件事:
TesterController.printsHelloWorld(ActionEvent)
方法需要注解@FXML
.
@FXML
void printsHelloWorld(ActionEvent event) {
l1.setText("Hello, World!");
}
这使得方法对FXMLLoader
“可见”。
FXML 文件中的 Button
元素需要设置其 onAction
属性。
<Button onAction="#printsHelloWorld" .../>
这告诉 FXMLLoader
将引用的控制器方法“link”到按钮的动作处理程序。请注意 #
前缀必须存在。
FXML 文件中的 Label
元素需要一个 fx:id
属性等于控制器中的字段名称。
<Label fx:id="l1" .../>
这让 FXMLLoader
将创建的 Label
实例注入到控制器的字段中。在这种情况下,该字段是 @FXML private Label l1;
字段。
尊敬的先生, 我是一个完整的初学者,通过一些教程学习,同时遵循它我试图制作一个简单的按钮,将“Hello WOrld”打印到标签上。但是当我 运行 按钮出现的代码时,无论我按多少次它都不会打印 helloWorld ...
1.Below是HelloWorld.JAVA代码:
enter code here
package helloworld;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
@Override
public void start(Stage primaryStage){
try {
Parent root = FXMLLoader.load(getClass().getResource("/helloworld/Tester.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
2.Below是TesterController.JAVA代码:
enter code here
package helloworld;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class TesterController implements Initializable {
@FXML
private Label l1;
void printsHelloWorld(ActionEvent event){
l1.setText("Hello World");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
3.Below是Tester.fxml代码:
enter code here
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="helloworld.TesterController">
<children>
<Button layoutX="201.0" layoutY="155.0" mnemonicParsing="false" prefHeight="161.0" prefWidth="175.0" text="Button" />
<Label layoutX="170.0" layoutY="59.0" prefHeight="46.0" prefWidth="237.0" />
</children>
</AnchorPane>
4.Below是运行之后的输出框文字:
enter code here
ant -f C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld jfxsa-run
init:
Deleting: C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld\build\built-jar.properties
compile:
Detected JavaFX Ant API version 1.3
jfx-deployment:
jar:
Copying 12 files to C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld\dist\run141170950
jfx-project-run:
Executing C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld\dist\run141170950\HelloWorld.jar using platform C:\Program Files\Java\jdk1.8.0_111\jre/bin/java
Jul 24, 2021 4:31:25 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.171 by JavaFX runtime of version 8.0.111
Deleting directory C:\Users\Abdullah\Documents\NetBeansProjects\HelloWorld\dist\run141170950
jfxsa-run:
BUILD SUCCESSFUL (total time: 8 seconds)
您遗漏了三件事:
TesterController.printsHelloWorld(ActionEvent)
方法需要注解@FXML
.@FXML void printsHelloWorld(ActionEvent event) { l1.setText("Hello, World!"); }
这使得方法对
FXMLLoader
“可见”。FXML 文件中的
Button
元素需要设置其onAction
属性。<Button onAction="#printsHelloWorld" .../>
这告诉
FXMLLoader
将引用的控制器方法“link”到按钮的动作处理程序。请注意#
前缀必须存在。FXML 文件中的
Label
元素需要一个fx:id
属性等于控制器中的字段名称。<Label fx:id="l1" .../>
这让
FXMLLoader
将创建的Label
实例注入到控制器的字段中。在这种情况下,该字段是@FXML private Label l1;
字段。