gpt4 book ai didi

javafx - onAction ="#ClickedClickMe"在 JavaFX FXML 中不起作用

转载 作者:行者123 更新时间:2023-12-02 20:48:02 24 4
gpt4 key购买 nike

早上好!我刚刚开始学习 Java FX 和 Java FX FXML。我使用 Scene Builder 构建 GUI,并使用 Netbeans 来编写剩余的 Java FX 程序。

我面临的问题是在 FXML 文件中。 Netbeans 编辑器在 onAction 事件的 .fxml 文件中显示以下代码错误。

我在 fxml 文件中看到的错误:
Controller 未在根组件上定义。

异常文本:

Executing sample1.jar using platform C:\Program Files\Java\jdk1.8.0_121\jre/bin/java Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:745) Caused by: javafx.fxml.LoadException: No controller specified. file:/C:/Users/hmitty/Documents/NetBeansProjects/sample1/dist/run1317822864/sample1.jar!/sample1/FXMLDocument.fxml:21

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597) at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103) at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557) at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599) at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770) at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) at sample1.Sample1.start(Sample1.java:22) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ... 1 more Exception running application sample1.Sample1 Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true Java Result: 1

如果我使用 Netbeans 创建一个示例项目,它会使用 AnchorPane,并且一切似乎都很好。但是,如果我删除 AnchorPane 并添加任何其他 Pane 和按钮,onAction 事件将不起作用!

我是这门语言的初学者,因此非常感谢任何帮助/指导!

注意:我花了近半天的时间在 StackOverFlow 上尽可能浏览所有可用的解决方案。

我得到的最接近的是这个,所以任何帮助肯定会为我带来帮助! https://bugs.openjdk.java.net/browse/JDK-8091582

所有代码都粘贴在下面。如果还需要什么,请告诉我!

FXML 代码:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.event.ActionEvent?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button mnemonicParsing="false" onAction="#ClickedClickMe" text="Click Me!" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label fx:id="label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
</GridPane>

Sample1.java

package sample1;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Sample1 extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

Scene scene = new Scene(root);

stage.setScene(scene);
stage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

FXMLDocumentController.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sample1;

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 FXMLDocumentController implements Initializable {

@FXML
private Label label;

@FXML
private void ClickedClickMe(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Clicked Try me!!");
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}

}

最佳答案

在fxml文件中设置 Controller

<GridPane fx:controller="sample1.FXMLDocumentController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">

关于javafx - onAction ="#ClickedClickMe"在 JavaFX FXML 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43465091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com