2016-07-07 12 views
-1

私は最近、FXMLと共にJavaFXの使用を開始しました。 私は、FXMLのいくつかの機能をテストするための小さくてシンプルなプログラムを書きました。ここに私のコードです。 メインクラス:JavaFXとFXMLファイル

package main; 

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

import javax.swing.*; 
import java.io.IOException; 

public class Main extends Application { 

    @FXML 
    private RadioButton button; 

    @Override 
    public void start(Stage primaryStage) { 

     Parent root = null; 
     try { 
      root = FXMLLoader.load(getClass().getResource("Main.fxml")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     String input = JOptionPane.showInputDialog("Enter something."); 
     button.setText(input); 

     Scene scene = new Scene(root,300,200); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

    } 

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

} 

Main.fxml:

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="248.0" prefWidth="366.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.Main"> 
    <children> 
     <RadioButton layoutX="140.0" layoutY="116.0" mnemonicParsing="false" fx:id="rb" on/> 
    </children> 
</Pane> 

私はこのプログラムを実行すると、私はエラーメッセージが表示されます:

javafx.fxml.LoadException: 
/C:/Users/kenta/IdeaProjects/Other%20projects/Test/out/production/Test/main/Main.fxml:7 

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103) 
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922) 
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971) 
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220) 
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744) 
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) 
    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 main.Main.start(Main.java:24) 
    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) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.ClassNotFoundException: main.Controller 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920) 
    ... 22 more 
Exception in Application start method 
Exception in thread "main" 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 
    at main.Main.start(Main.java:30) 
    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 

Process finished with exit code 1 

私はラジオボタンの変数を扱うことができますどのように"ボタン"?

Kenta1561

+1

の可能性のある重複した[JavaFXの - アプリケーションクラスが指定でき、コントローラクラス](http://stackoverflow.com/questions/33303167/javafx-can -application-class-be-the-controller-class) – Itai

+0

リンクされた質問を参照してください。副次的なこととして、JavaFXとSwing(JOptionPaneはSwingコンポーネント)を混在させることは、一般的には悪い考えです。それはできますが、可能であれば通常は避けるべきです。 – Itai

+0

私は独自のControllerクラスを作成しても構いませんが、まだエラーが発生しています.FXMLファイルで定義されたクラスで変数を操作する方法を知りたいです。 – Kenta1561

答えて

1

あなたのコードに以下の修正を適用してみてください。

これはあなたのメインクラスのコントローラを行います

FXMLLoader loader=new FXMLLoader(getClass().getResource("Main.fxml")); 
loader.setController(this); 
root = loader.load(); 

root = FXMLLoader.load(getClass().getResource("Main.fxml")); 

を交換してください。これをやらなければ、FXMLLoaderはメインクラスの新しいインスタンスを作成します。このインスタンスは、呼び出し元のFXMLLoader.load(URL)のインスタンスとは異なります。

を次のようにあなたのFXMLファイルを変更し

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 


<Pane minHeight="0" minWidth="0" prefHeight="248.0" prefWidth="366.0" xmlns:fx="http://javafx.com/fxml"> 
    <children> 
     <RadioButton layoutX="140.0" layoutY="116.0" mnemonicParsing="false" fx:id="button"/> 
    </children> 
</Pane> 

FXの値:idはあなたがラジオボタンを保存するフィールドの名前と同じでなければなりません。また、名前空間宣言はxmlns:fx="http://javafx.com/fxml"でなければなりません。さらに、maxHeightおよびmaxWidthプロパティを-Infinityに設定すると、レイアウトのサイズが0ピクセルより大きくならなくなります。また、<children>タグと</children>タグを省略することもできます。あなたはFXMLを学びたいのであれば

が、私はこの文書をお勧め:Introduction to FXML

関連する問題