我只是想用javafx学点新东西。。。但是即使是一个“空的”javafx文档也会抛出错误。。。
所以我用fxml创建了一个新的javafx项目。当我尝试在不更改任何内容的情况下运行它时,会出现以下错误:
ant -f C:\\Users\\noetz\\Documents\\NetBeansProjects\\test -Dnb.internal.action.name=run.single -Djavac.includes=test/Test.java -Drun.class=test.Test run-single
init:
deps-jar:
Created dir: C:\Users\noetz\Documents\NetBeansProjects\test\build
Updating property file: C:\Users\noetz\Documents\NetBeansProjects\test\build\built-jar.properties
Created dir: C:\Users\noetz\Documents\NetBeansProjects\test\build\classes
Created dir: C:\Users\noetz\Documents\NetBeansProjects\test\build\empty
Created dir: C:\Users\noetz\Documents\NetBeansProjects\test\build\generated-sources\ap-source-output
Compiling 1 source file to C:\Users\noetz\Documents\NetBeansProjects\test\build\classes
compile-single:
run-single:
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: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
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 test.Test.start(Test.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 test.Test
C:\Users\noetz\Documents\NetBeansProjects\test\nbproject\build-impl.xml:1367: The following error occurred while executing this line:
C:\Users\noetz\Documents\NetBeansProjects\test\nbproject\build-impl.xml:994: Java returned: 1
BUILD FAILED (total time: 1 second)
我可以在项目上右键单击并使用clean。那就行了(但每次我使用javafx的时候这样做是不对的?)
但在那之后,它不可能改变布局或添加按钮或任何其他。。。我用scenebuilder做什么并不重要。我可以手动改变窗格的大小,它不会改变任何东西。所以我可以把我以前的工作程序的代码粘贴到它里面,它不会改变任何东西。
所以,如果我只是想把一些简单的事情,如改变一个标签,写这个
/*
* 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 test;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
/**
*
* @author noetz
*/
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private Button btn1;
private void handleButtonAction(ActionEvent event) {
label.setText("Gedrückt");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
fxml地址:
<?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="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="test.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<Button fx:id="btn1" layoutX="60.0" layoutY="38.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>
它仍然在向世界问好,按钮甚至没有改变。它就像加载标准模板,我不知道我应该做什么,因为它执行的代码不在那里。。。我希望有人能理解我糟糕的英语,并能帮助我
暂无答案!
目前还没有任何答案,快来回答吧!