Java FX2 を動かしてみたというのはもののついでで、 gistをBloggerに貼れるかどうか試したかったがためにできた記事。
環境
- OS: Ubuntu 12.04 (64bit)
- JDK: 1.7.0_06 64bit
Applicationクラス
アプリケーション全体の起動ポイントかつ各種制御。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.nt67.sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.stage.Stage; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.Pane; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import org.nt67.sample.control.FxSampleControl; | |
public class FxSampleApplication extends Application { | |
public static void main(String[] args){ | |
launch(args); | |
launch(FxSampleApplication.class, args); | |
} | |
@Override | |
public void start(Stage stage) throws Exception{ | |
// set window title. | |
stage.setTitle("nt67"); | |
// create Scene | |
File fxmlFile = new File("sample.fxml"); | |
FXMLLoader loader = new FXMLLoader(); | |
Parent root = (Parent) loader.load(new FileInputStream(fxmlFile)); | |
Scene scene = new Scene(root); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
} |
FXMLファイル
画面の構成、デザイン。CSSも適用できるらしい。Flashで言うところのmxmlか。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import java.lang.*?> | |
<?import java.util.*?> | |
<?import javafx.scene.control.*?> | |
<?import javafx.scene.layout.*?> | |
<?import javafx.scene.paint.*?> | |
<AnchorPane | |
id="AnchorPane" | |
maxHeight="-Infinity" | |
maxWidth="-Infinity" | |
minHeight="-Infinity" | |
minWidth="-Infinity" | |
prefHeight="400.0" | |
prefWidth="600.0" | |
xmlns:fx="http://javafx.com/fxml" | |
fx:controller="org.nt67.sample.control.FxSampleControl"> | |
<children> | |
<TextField | |
fx:id="txtOperatorA" | |
layoutX="14.0" | |
layoutY="14.0" | |
prefWidth="20.0" | |
text=""/> | |
<Label | |
fx:id="lblOperand" | |
layoutX="40.0" | |
layoutY="14.0" | |
prefWidth="200.0" | |
text="+"/> | |
<TextField | |
fx:id="txtOperatorB" | |
layoutX="76.0" | |
layoutY="14.0" | |
prefWidth="20.0" | |
text=""/> | |
<Label | |
fx:id="lblAnswer" | |
layoutX="14.0" | |
layoutY="41.0" | |
text="InitialValue" /> | |
<Button | |
fx:id="btnApply" | |
layoutX="14.0" | |
layoutY="63.0" | |
onAction="#calculation" | |
text="Calculation" /> | |
</children> | |
</AnchorPane> |
コントローラクラス
FXMLの制御。Flashで言うところのmxmlに対するActionScriptファイルか。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.nt67.sample.control; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TextField; | |
public class FxSampleControl { | |
@FXML | |
private TextField txtOperatorA; | |
@FXML | |
private TextField txtOperatorB; | |
@FXML | |
private Label lblOperand; | |
@FXML | |
private Label lblAnswer; | |
@FXML | |
protected void calculation(ActionEvent e){ | |
Long operatorA = Long.valueOf(this.txtOperatorA.getText()); | |
Long operatorB = Long.valueOf(this.txtOperatorB.getText()); | |
Long answer = operatorA + operatorB; | |
this.lblAnswer.setText(answer.toString()); | |
} | |
} |
その他
- ビルド・起動時には、${JAVA_HOME}/jre/lib/rt.ja にパスを通す
- FXMLを相対パスで指定する場合のベースディレクトリはJavaの起動ディレクトリ。リソースファイルとかと同じ。
- "Control"って名前のクラスにしてしまっているけど、Oracleの名づけ的にはControllerですよ。
0 件のコメント:
コメントを投稿