JavaFXで、ボタンを押したらラベルに文字を表示しようとしています。
コードにエラーは出ないのですが実行するとやっぱりコンソールに赤字が出てきます。
Controllerがないと、正常に表示されるのですがControllerの中身を書いたらエラーになります。
どこを改善すべきでしょうか、よろしくお願いします。
JavaSE11です。
Main
1package application; 2 3import javafx.application.Application; 4import javafx.fxml.FXMLLoader; 5import javafx.scene.Parent; 6import javafx.scene.Scene; 7import javafx.stage.Stage; 8 9 10public class Main extends Application { 11 public static void main(String[] args) { 12 launch(args); 13 } 14 15 @Override 16 public void start(Stage stage) throws Exception{ 17 //fxml読み 18 Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml")); 19 //scene 20 Scene scene = new Scene(root); 21 stage.setScene(scene); 22 //表示 23 stage.show(); 24 } 25}
SampleController
1package application; 2 3import javafx.event.ActionEvent; 4import javafx.fxml.FXML; 5import javafx.scene.control.Button; 6import javafx.scene.control.Label; 7 8public class SampleController { 9 @FXML private Button SampleButton; 10 @FXML private Label SampleLabel; 11 12 @FXML 13 public void ButtonClicked(ActionEvent event) { 14 this.SampleLabel.setText("HelloWorld"); 15 } 16 17}
Sample
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.control.Button?> 4<?import javafx.scene.control.Label?> 5<?import javafx.scene.layout.AnchorPane?> 6<?import javafx.scene.text.Font?> 7 8<AnchorPane prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController"> 9 <children> 10 <Label fx:id="SampleLabel" layoutY="14.0" prefHeight="200.0" prefWidth="400.0" textAlignment="CENTER"> 11 <font> 12 <Font size="75.0" /> 13 </font> 14 </Label> 15 <Button fx:id="SampleButton" layoutX="140.0" layoutY="214.0" mnemonicParsing="false" onAction="#ButtonClicked" prefHeight="48.0" prefWidth="120.0" text="Button" /> 16 </children> 17</AnchorPane> 18
Error
1Exception in Application start method 2java.lang.reflect.InvocationTargetException 3 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 4 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 5 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 6 at java.base/java.lang.reflect.Method.invoke(Method.java:566) 7 at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464) 8 at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363) 9 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 10 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 11 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 12 at java.base/java.lang.reflect.Method.invoke(Method.java:566) 13 at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051) 14Caused by: java.lang.RuntimeException: Exception in Application start method 15 at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900) 16 at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195) 17 at java.base/java.lang.Thread.run(Thread.java:834) 18Caused by: javafx.fxml.LoadException: 19/Applications/Eclipse_2020-03.app/Contents/workspace/javafx/bin/application/Sample.fxml:10 20 21 at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625) 22 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603) 23 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466) 24 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237) 25 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194) 26 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163) 27 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136) 28 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113) 29 at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106) 30 at javafx/application.Main.start(Main.java:18) 31 at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846) 32 at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455) 33 at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428) 34 at java.base/java.security.AccessController.doPrivileged(Native Method) 35 at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427) 36 at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) 37Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private javafx.scene.control.Button application.SampleController.SampleButton accessible: module javafx does not "opens application" to module javafx.fxml 38 at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:340) 39 at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:280) 40 at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:176) 41 at java.base/java.lang.reflect.Field.setAccessible(Field.java:170) 42 at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.addAccessibleFields(FXMLLoader.java:3479) 43 at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.access$3900(FXMLLoader.java:3328) 44 at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor$1.run(FXMLLoader.java:3444) 45 at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor$1.run(FXMLLoader.java:3440) 46 at java.base/java.security.AccessController.doPrivileged(Native Method) 47 at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.addAccessibleMembers(FXMLLoader.java:3439) 48 at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.getControllerFields(FXMLLoader.java:3378) 49 at javafx.fxml/javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1170) 50 at javafx.fxml/javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:105) 51 at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:865) 52 at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:759) 53 at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722) 54 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552) 55 ... 14 more 56Exception running application application.Main 57
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/20 13:01
2020/04/21 12:11
2020/04/21 12:20
2020/04/21 12:20
2020/04/21 13:06