前提
Java入門書を用いてjavafxを勉強している初心者です。
素人だと思ってきいていただけると幸いです。
発生している問題
Eclipseで簡単なGUIの作成を試みています。以下のサイトを参考にしました。
https://qiita.com/Kei_22/items/f54e6ba7cd4bf71b14c8
実行するとコンソールにエラーは出ず、実行されるのですが、javaのアイコンがDockに現れるのみでウィンドウが現れません。
どのように処理したらよいでしょうか、
該当のソースコード
Java
1package application; 2 3import javafx.fxml.FXML; 4import javafx.scene.control.Button; 5import javafx.scene.control.Label; 6import javafx.scene.control.TextField; 7 8 9public class FormControls { 10 @FXML private TextField field_Name; 11 @FXML private Button button_Enter; 12 @FXML private Label label_Output; 13 14 @FXML public void onButtonClicked() { 15 label_Output.setText("Hello" + field_Name.getText()+"!!"); 16 17 } 18 19}
Java
1package application; 2 3import javafx.application.Application; 4import javafx.fxml.FXMLLoader; 5import javafx.scene.Scene; 6import javafx.scene.layout.AnchorPane; 7import javafx.stage.Stage; 8 9 10public class Main extends Application { 11 @Override 12 public void start(Stage primaryStage) { 13 try { 14 AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Form.fxml")); 15 Scene scene = new Scene(root,400,400); 16 scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 17 primaryStage.setScene(scene); 18 primaryStage.show(); 19 } catch(Exception e) { 20 e.printStackTrace(); 21 } 22 } 23 24 public static void main(String[] args) { 25 launch(args); 26 } 27} 28
fxml
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.control.*?> 4<?import java.lang.*?> 5<?import javafx.scene.layout.*?> 6<?import javafx.scene.layout.AnchorPane?> 7 8 9<AnchorPane prefHeight="400.0" prefWidth="400.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="application.FormControls"> 10 <children> 11 <TextField fx:id="field_Name" layoutX="33.0" layoutY="179.0" prefHeight="0.0" prefWidth="167.0" /> 12 <Button fx:id="button_Enter" layoutX="200.0" layoutY="179.0" mnemonicParsing="false" onAction="#onButtonClicked" text="Button" /> 13 <Label fx:id="label_Output" layoutX="15.0" layoutY="98.0" prefHeight="40.0" prefWidth="370.0" text="Label" /> 14 </children> 15</AnchorPane> 16
実行手順
新規javafxプロジェクト
ビルドパスに、javafxのpathを追加
javaアプリケーションで実行
以上が行った手順です