お久しぶりです。卒論で死にかけていますがひとまず息はしています。
さて、JavaFXが出てから結構立ったのでそろそろ私も弄ってみようと、FXMLというXML文書でGUIを構成する機能を試してみましたところ、謎の挙動を示したのでここで報告しておこうと思います。
"Getting Started with FXML"にあるSign inの画面とほぼ作ろうとしている物は同じです。ただ、画像を用意するのが面倒だったので、端折ってGridPane部分だけ作っています。
まずはJavaFXを起動するApplicationを拡張したクラス。
package fx; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.*; import javafx.stage.Stage; public class FXTest extends Application{ public void start(Stage stage) throws Exception { stage.setTitle("FMLX Test"); Parent root = FXMLLoader.load(FXTest.class.getResource("login.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
FXMLLoderでclassファイルの場所にあるlogin.fxmlを読み込んでシーンにするだけです。
次に、先にコントローラーにあたるLogin.javaを示します。
package fx; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.*; public class Login { @FXML TextField usnameField; @FXML PasswordField passwordField; @FXML protected void loginAction(ActionEvent a){ System.out.println(usnameField.getText()+ " " + passwordField.getText()); } }
ユーザー名とパスワードを表示するだけです。
最後に問題のlogin.fxmlです。
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.layout.*?> <?import javafx.scene.control.*?> <?import javafx.scene.*?> <?import javafx.scene.image.*?> <GridPane fx:controller="fx.Login" alignment="top_center" hgap="8" vgap="8" style="-fx-padding: 40 0 0 0;" xmlns:fx="http://javafx.com/fxml"> <children> <!-- <Label text="dummy text" GridPane.columnIndex="1" GridPane.rowIndex="0"/> --> <Label text="log in:" style="-fx-font:normal 15 Tahoma;" GridPane.columnIndex="0" GridPane.rowIndex="0"/> <Label text="name" style="-fx-font:normal 10 Tahoma;" GridPane.columnIndex="0" GridPane.rowIndex="1"/> <TextField fx:id="usnameField" GridPane.columnIndex="1" GridPane.rowIndex="1"/> <Label text="password" style="-fx-font:normal 10 Tahoma;" GridPane.columnIndex="0" GridPane.rowIndex="2"/> <PasswordField fx:id="passwordField" style="" GridPane.columnIndex="1" GridPane.rowIndex="2"/> <Button text="submit" style="" onAction="#loginAction" GridPane.columnIndex="1" GridPane.rowIndex="3"/> </children> </GridPane>
ほぼ"Example 8"の丸パクリですね。
まず、これで実行するとこうなります。
styleでフォントサイズをlog inは15に、その他は10にしているのに差がありません。
次にコメントアウトしていたダミーラベルを表示させてみます。
このダミーラベルはstyleが設定されていません。
きちんと文字の大きさに差が出ました。
これってどういうことなんでしょうか?