应用程序启动方法中的 JavaFx Gui 异常

JavaFx Gui Exception in Application start method

我基本上只有一个未编码的 Gui,尽管之前有一些可用的 GUI 和一些代码,但仍然出现了该异常。所以最令人困惑的是..基本上只有一个图形用户界面,没有太多代码可以删除,修复..为什么这个图形用户界面没有出现? p.s。 fxml 肯定在正确的目录中

主要

package application;

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



public class Main extends Application {

public static void main(String[] args) {
    launch(args);

}

@Override
public void start(Stage stage) throws Exception {

        AnchorPane pane =  FXMLLoader.load(getClass().getResource("Gui.fxml"));
    Scene scene = new Scene(pane);
    stage.setScene(scene);
    stage.show();


}
}

控制器

public class Controller implements Initializable {
DropShadow ds = new DropShadow();
@FXML
ButtonGroup group;
@FXML
RadioButton radio1;
@FXML
RadioButton radio2;
@FXML
RadioButton radio3;
@FXML
Label changelog;
@FXML
TextArea changelogarea;
@FXML
Pane panechange;
@FXML
AnchorPane mainanchor;

Controller(){
changelog.setEffect(ds);    
}







@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

}

}

FXML

<?import javafx.scene.text.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane fx:id="mainanchor" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
   <children>
      <Pane fx:id="panechange" layoutX="106.0" layoutY="-3.0" prefHeight="406.0" prefWidth="493.0">
     <children>
        <Label fx:id="changelog" layoutX="20.0" layoutY="21.0" prefHeight="49.0" prefWidth="463.0" text="                     Change log" textAlignment="CENTER">
           <font>
              <Font size="27.0" />
           </font>
        </Label>
        <TextArea fx:id="changelogarea" layoutX="1.0" layoutY="70.0" prefHeight="329.0" prefWidth="487.0" />
     </children>
  </Pane>
  <RadioButton fx:id="radio2" layoutX="14.0" layoutY="62.0" mnemonicParsing="false" text="Valiuta">
     <toggleGroup>
        <ToggleGroup fx:id="group" />
     </toggleGroup>
  </RadioButton>
  <RadioButton fx:id="radio3" layoutX="14.0" layoutY="94.0" mnemonicParsing="false" text="Atstumai" toggleGroup="$group" />
  <RadioButton fx:id="radio1" layoutX="14.0" layoutY="32.0" mnemonicParsing="false" selected="true" text="Naujienos" toggleGroup="$group" />
   </children>
</AnchorPane>

错误

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:483) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherIm pl.java:363) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java: 303) 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:483) 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 :875) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication7(Launche rImpl.java:157) at com.sun.javafx.application.LauncherImpl$$Lambda/128893786.run(Unknown Sou rce) at java.lang.Thread.run(Thread.java:745) Caused by: javafx.fxml.LoadException: /E:/Programming/Java/WorkSpace/FxConverter/bin/application/Gui.fxml:11

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595) at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:104) at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:930) at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoade r.java:967) at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216) at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740) at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2701) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2521) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3208) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091) at application.Main.start(Main.java:21) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(Launch erImpl.java:821) at com.sun.javafx.application.LauncherImpl$$Lambda/1596489439.run(Unknown So urce) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.j ava:323) at com.sun.javafx.application.PlatformImpl$$Lambda/1051754451.run(Unknown So urce) at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:29 2) at com.sun.javafx.application.PlatformImpl$$Lambda/1001120425.run(Unknown So urce) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.jav a:291) at com.sun.javafx.application.PlatformImpl$$Lambda/1775282465.run(Unknown So urce) 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$null1(WinApplication.java:102) at com.sun.glass.ui.win.WinApplication$$Lambda/1109371569.run(Unknown Source ) ... 1 more Caused by: java.lang.IllegalAccessException: Class sun.reflect.misc.ReflectUtil can not access a member of class application.Controller with modifiers "" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:101) at java.lang.Class.newInstance(Class.java:432) at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51) at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:923) ... 27 more Exception running application application.Main

这里有两个问题:

  1. 控制器的构造函数必须是 public。请参阅此线程以供参考:JavaFX IllegalAccessException during FXML load()
  2. 控制器构造函数将不起作用,因为您正在尝试访问此时不可用的字段。

所以对于 2. 将 changelog.setEffect(ds); 从控制器移动到 initialize 方法。