JavaFXå ¥éã®æåã®ã»ã¯ã·ã§ã³ã§ã¯ã ããã§ã³ãã³ãã©ã€ã³ã䜿çšããŠååŸã§ããæãåçŽãªJavaFXã¢ããªã±ãŒã·ã§ã³ãäœæããæ¹æ³ã«ã€ããŠèª¬æããŸãã 2çªç®ã®ã»ã¯ã·ã§ã³ã§ã¯ããã°ã€ã³ããã³ãã¹ã¯ãŒãå ¥åãŠã£ã³ããŠãäœæããããšãææ¡ããŸãã ããã§æåã®ã¢ããªã±ãŒã·ã§ã³ããäœæãããŸã ã ãã®çµæãã³ãŒãã®ãµã€ãºããããã«å¢å ããŸããã
HelloWorld.java
package helloworld; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.scene.layout.GridPane; import javafx.geometry.*; import javafx.scene.text.*; import javafx.scene.control.*; import javafx.scene.paint.*; import javafx.scene.layout.HBox; public class HelloWorld extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("JavaFX Welcome"); GridPane grid = new GridPane(); grid.setAlignment(Pos.CENTER); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(25, 25, 25, 25)); // grid.setGridLinesVisible(true); Text scenetitle = new Text("Welcome"); scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20)); grid.add(scenetitle, 0, 0, 2, 1); Label userName = new Label("User Name:"); grid.add(userName, 0, 1); TextField userTextField = new TextField(); grid.add(userTextField, 1, 1); Label pw = new Label("Password:"); grid.add(pw, 0, 2); PasswordField pwBox = new PasswordField(); grid.add(pwBox, 1, 2); Button btn = new Button("Sign in"); HBox hbBtn = new HBox(10); hbBtn.setAlignment(Pos.BOTTOM_RIGHT); hbBtn.getChildren().add(btn); grid.add(hbBtn, 1, 4); final Text actiontarget = new Text(); grid.add(actiontarget, 1, 6); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { actiontarget.setFill(Color.FIREBRICK); actiontarget.setText("Sign in button pressed"); } }); Scene scene = new Scene(grid, 300, 275); primaryStage.setScene(scene); scene.getStylesheets().add (HelloWorld.class.getResource("HelloWorld.css").toExternalForm()); primaryStage.show(); } }
ã³ã³ãã€ã«ãå®è¡ãjarã§ããã±ãŒãžåãjarãå®è¡ããã³ãã³ãã¯ãããããç¬èªã®CMDãã¡ã€ã«ã§åããŸãŸã§ãã
ã³ãã³ãã©ã€ã³
@"C:\Program Files\Java\jdk1.7.0_40\bin\javac" -d out -classpath "C:\Program Files\Java\jre7\lib\jfxrt.jar" src\helloworld\HelloWorld.java @"C:\Program Files\Java\jdk1.7.0_40\bin\java" -classpath "C:\Program Files\Java\jre7\lib\jfxrt.jar;.\out" helloworld.HelloWorld @"C:\Program Files\Java\jdk1.7.0_40\bin\javafxpackager" -createjar -appclass helloworld.HelloWorld -srcdir .\out -outfile HelloWorld -v @"C:\Program Files\Java\jre7\bin\java.exe" -jar HelloWorld.jar @pause
3çªç®ã®ã»ã¯ã·ã§ã³ãæ±ããŸãããã ãã¥ãŒããªã¢ã«ã§ã¯ã2çªç®ã®ã»ã¯ã·ã§ã³ã®ç©ºçœã䜿çšããããšããå§ãããŸãã ãããŠãLogin.javaããªããHelloWorld.javaã ãããããŸãã CSSãã¡ã€ã«ãäœæããŸãã
IDEã®æäœã«é¢é£ããïŒãã¥ãŒããªã¢ã«ã®ïŒæ瀺ã®ãã€ã³ãã¯ç¡èŠããŠãã ããã 4çªç®ã®æ®µèœã§ã¯ãsrc \ loginãã©ã«ããŒã«CSSãã¡ã€ã«ãäœæããããšãææ¡ããŸãããã®å Žåãsrc \ helloworldã«ãªããŸãã ããŠãä»ã®ãšãããHelloWorld.cssã«åŒãç¶ãæšæ¶ãããŠããã®ã§ãé»è©±ããŠã¿ãŸãããã ããã¯éèŠã§ã¯ãããŸãããã
6çªç®ã®æ®µèœã¯ãJavaã³ãŒãã«è¡ãå¿ èŠãããå€æŽã瀺ããŠããŸãã
Scene scene = new Scene(grid, 300, 275); primaryStage.setScene(scene);
æ¿å
¥ããå¿
èŠããããŸã
scene.getStylesheets().add (HelloWorld.class.getResource("HelloWorld.css").toExternalForm());
ïŒãã¡ã€ã«ãšã¯ã©ã¹ã®ååã«åãããŠèª¿æŽïŒãã¡ããã©å
primaryStage.show();
ããã«ãCSSãã¡ã€ã«ã«ãã£ãŠããã¹ããªããžã§ã¯ããäœããã®æ¹æ³ã§å¶åŸ¡ã§ããããã«ããããã®ãªããžã§ã¯ãã«é©åãªèå¥åãäžããå¿ èŠããããŸãã ãããè¡ãã«ã¯ãè¡ãèŠã€ããŸã
scenetitle.setFont(Font.font(âTahomaâ, FontWeight.NORMAL, 20));
ãããŠ
ã§çœ®ãæããŸãactiontarget.setFill(Color.FIREBRICK);
scenetitle.setId("welcome-text");
ãããŠ
actiontarget.setId("actiontarget");
ããã«å¿ããŠã ãŸããäžãäžã«åããŠäº€æããã®ã§ã¯ãªããã³ã¡ã³ãã§éããŠãäžã®ä¿®æ£è¡ãæžãçããŠãããšããã§ãããã
ãããŠããã¡ãããã¹ã¿ã€ã«ã·ãŒãã§ãã¡ã€ã«ãåããå¿ èŠããããŸãã
HelloWorld.css
.root { -fx-background-image: url("background.jpg"); } .label { -fx-font-size: 12px; -fx-font-weight: bold; -fx-text-fill: #333333; -fx-effect: dropshadow( gaussian , rgba(255,255,255,0.5) , 0,0,0,1 ); } #welcome-text { -fx-font-size: 32px; -fx-font-family: "Arial Black"; -fx-fill: #818181; -fx-effect: innershadow( three-pass-box , rgba(0,0,0,0.7) , 6, 0.0 , 0 , 2 ); } #actiontarget { -fx-fill: FIREBRICK; -fx-font-weight: bold; -fx-effect: dropshadow( gaussian , rgba(255,255,255,0.5) , 0,0,0,1 ); } .button { -fx-text-fill: white; -fx-font-family: "Arial Narrow"; -fx-font-weight: bold; -fx-background-color: linear-gradient(#61a2b1, #2A5058); -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 ); } .button:hover { -fx-background-color: linear-gradient(#2A5058, #61a2b1); }
圌ã¯ãçµæãè³è³ããªããã3çªç®ã®ã»ã¯ã·ã§ã³ã®ããã¹ããåŸã ã«åºå·ããããã«ææ¡ãããŠãããã¹ãŠã®ãã®ãããã«æºãããŸãã-èæ¯ç»åãããã¹ãå¹æããã¿ã³å¹æã
ãããèæ¯ç»åã®ç»åãå¿ èŠã§ã...ãŸããç§ã¯ããŠã³ããŒããæç»ãããŸããã§ããããæåã®ãã®ãã³ããŒããŸããã æåã®ãã©ã«ãã¯ãããã€ãããŒã€ã³ã°\å³é¢ã®ãµã³ãã«ããã©ã«ã-Sunset.jpgããã®èæ¯ïŒç®èãªããšã«ãã¡ããã©ãã®å ŽæïŒã§ãã background.jpgã«ååãå€æŽããCSSãã¡ã€ã«ã®æšªã«é 眮ããŸãã
ã³ã³ãã€ã«ããŸãã æåããŸããã å§ããŸãã åäœããŸããïŒ ãšã©ãŒã®åºåã調æ»ããŸãã é »ç¹ã«çºçããäžæãªãœãŒã¹ããããã£ãŠãäžæãªãœãŒã¹ã¯èŠæããŠããŸãã ããã«ããã®ãã¹ãŠã®äžåèªã®äžã§ããªãã¿ã®æçŽããããŸãïŒ
at helloworld.HelloWorld.start(HelloWorld.java:68)
ãããŠã68è¡ç®ã§ã¯ãCSSãã¡ã€ã«ããã¹ã¿ã€ã«ãéžæããã ãã§ãã ããã¯ãsrc \ helloworldã®æ瀺ã«åŸã£ãŠé 眮ãããŸãã ãã®ãç 究ãã®æèã§ã¯ãNetBeansã§ãã®ãã¡ã€ã«ããœãŒã¹ãã©ã«ããŒã«å¿ èŠãªçç±ãèãããã¯ãããŸããïŒæšæž¬ã§ããïŒã ãã ããã¹ã¿ã€ã«ã·ãŒããžã®ãã¹ãã©ãã«ãæå®ããŠããªããããã¹ã¿ã€ã«ã·ãŒãã䜿çšãããã¹ã®ãã暪ã«é 眮ããå¿ èŠããããŸãã 解åãããããã°ã©ã ã¯ãout \ helloworld \ã«ããHelloWorld.classãã¡ã€ã«ããèµ·åãããŸãã ããã«çããŠãåçãšäžç·ã«æããŸãããã
ã³ã³ãã€ã«ããŠå®è¡ããŸãã 確ãã«ãèæ¯ç»åã¯ãã¥ãŒããªã¢ã«ã®ãã®ãšã¯éåžžã«ç°ãªããèµ€ãèæ¯ã§æŒããããã¿ã³ã«é¢ããèµ€ã¬ã³ã¬ã®ã¡ãã»ãŒãžã¯èŠã«ããã§ããããããã¯äºçŽ°ãªããšã§ãããããã¯å€æŽã§ããŸãã ãã1ã€é¢çœããšããããããŸãã次ã«ãjarãããã³ã°ã³ãã³ããæå®ããŠãjarãã¡ã€ã«ãå®è¡ããŠã¿ãŸãããã
å§ãŸããªãïŒ ãããŠããã¡ã€ã«èªäœãã¯ãªãã¯ããã®ã§ã¯ãªããã³ã³ãœãŒã«ã³ãã³ãã«ãã£ãŠèµ·åãããå Žåããšã©ãŒã衚瀺ããããã®ãã¡ã®ããã€ãã¯ããªãã¿ã§ã...ããããã¢ãŒã«ã€ãã§ãããã®ããã«jarãã¡ã€ã«ã«ç»ããŸãããã ãã©ã«ããŒæ§é com \ javafx \ mainãååšãããã®ååšã¯javafxpackagerãŠãŒãã£ãªãã£ãŒã«ãã£ãŠä¿èšŒãããMETA-INFã¯ãããã§ã¹ãã䜿çšããŠããŸãããããã¯ä»ã§ã¯ããŸãé¢çœããããŸããã ãŸããhelloworldãã©ã«ããŒã¯èå³æ·±ããã®ã§ãHelloWorld.cssã¯ãããŸããããHelloWorld.bssããããŸãã ããã«ãŒã¯ãããŒã ãªãã§ãCSSããã€ããªåœ¢åŒã§åããã±ãŒãžåããããšãããããŸããã ãã§ãã¯ããŠã¿ãŠïŒ 68è¡ç®ã®.cssæ¡åŒµåã.bssã«çœ®ãæããŠã³ã³ãã€ã«ããŸãããããã«ã¯éå§ãããããã«jarãããã¯ããŠå®è¡ããŸãã
ä»ããå§ãŸããŸãã ãããããªããšæ®å¿µãªããšã§ããããHelloWorld.classããã¯å§ãŸããŸããã ç°ãªãã±ãŒã¹ã®ããã«2ã»ããã®ãœãŒã¹ã³ãŒããä¿æããªãã§ãã ããã ããã«ãããŠã³ããŒããé«éåããã«ã¯bssããå§ãããŸãã æžã蟌ãã³ãã³ããcss2bss.cmdãã¡ã€ã«ã«è¿œå ããŸãã
@"C:\Program Files\Java\jdk1.7.0_40\bin\javafxpackager" -createbss -srcdir .\src\helloworld -outdir .\out\helloworld -srcfiles HelloWorld.css -v @pause
CSSãã¡ã€ã«ãšèæ¯ç»åãã¡ã€ã«ãsrc \ helloworld \ïŒæåããèŠæ±ãããããã«ïŒããœãŒã¹ãã¡ã€ã«ã®getStylesheetsïŒïŒãæ¡åŒµå.bssã®è¡ã«æ®ããŸãã ã³ã³ãã€ã«ããŸãã bssãäœæããŸãã ç¶ã«è©°ããŸãã HelloWorld.classãšHelloWorld.jarã®äž¡æ¹ããå®è¡ããŸãã ãããŠãã©ã®ã¹ã¿ã€ã«ãäœã«è²¬ä»»ããããã©ã®ããã«ããããããŸããŸãªæ¹æ³ã§å€æŽã§ããããææ¡ããããã«ãç§ã¯èªåã§ææ¡ããŸãã ãã®ãããã¯ã¯ãJavaFXãšã¯çŽæ¥é¢ä¿ãããŸããã
ããŠããã¥ãŒããªã¢ã«ã®4çªç®ã®ã»ã¯ã·ã§ã³ã¯FXMLã§ãã ããã°ã©ã ã³ãŒãã¯éåžžã«å€æŽããããããå床æžãçŽãã®ãç°¡åã§ãã ããã«ãGetStartFXMLãªã©ã®é£æ¥ãã©ã«ããŒã«ãããžã§ã¯ãã®ã³ããŒãäœæããŸãã ããã§ããã§ã«5ã€ã®ããããã¡ã€ã«ãã³ããŒããoutããã³src \ helloworldãã©ã«ããŒãäœæãããœãŒã¹ãæåŸã«é 眮ããå¿ èŠããããŸãã
HelloWorld.java
package helloworld; import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.*; import javafx.fxml.*; public class HelloWorld extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml")); Scene scene = new Scene(root, 300, 275); stage.setTitle("FXML Welcome"); stage.setScene(scene); stage.show(); } }
次ã«ã4çªç®ã®ã»ã¯ã·ã§ã³ã«æ£åšãããã¡ã€ã«fxml_example.fxmlãåéããå¿ èŠããããŸãã ããããã®ããŒã¿ã¯CSSãã¡ã€ã«ããã ãã§ãªãããªã³ã¶ãã©ã€ã§ååŸããããããããã䜿çšããHelloWorld.classãã¡ã€ã«ã®ããé£ã«äœæãã䟡å€ããããŸããããŸã ååšããŠããŸããã ã€ãŸããout \ helloworldã§ã¯ãæåã®ã³ã³ãã€ã«ã®åã«ãèªåèªèº«ã§ãµããã©ã«ããŒãäœæããŸãã
fxml_example.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import java.net.*?> <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> <GridPane fx:controller="helloworld.FXMLExampleController" xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10" styleClass="root"> <padding><Insets top="25" right="25" bottom="10" left="25"/></padding> <Text id="welcome-text" text="Welcome" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="2"/> <Label text="User Name:" GridPane.columnIndex="0" GridPane.rowIndex="1"/> <TextField GridPane.columnIndex="1" GridPane.rowIndex="1"/> <Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="2"/> <PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="2"/> <HBox spacing="10" alignment="bottom_right" GridPane.columnIndex="1" GridPane.rowIndex="4"> <Button text="Sign In" onAction="#handleSubmitButtonAction"/> </HBox> <Text fx:id="actiontarget" GridPane.columnIndex="1" GridPane.rowIndex="6"/> <stylesheets> <URL value="@HelloWorld.css" /> </stylesheets> </GridPane>
ãã®ãã¡ã€ã«ã«ã¯ããã«CSSã®æäœãå«ãŸããŸããããªãäœãããã«ããã®ã§ãã ãããã£ãŠãæ°ããäœæãããHelloWorld.cssããã³background.jpgã®æšªã«é 眮ããŸãã ãŸã ååãªãã¿ã³ã€ãã³ãåŠçããããŸããã 4çªç®ã®ã»ã¯ã·ã§ã³ã§ã¯ãFXMLã§èšåãããŠããå¥ã®ãã¡ã€ã«ã䜿çšããããšãææ¡ããŠããŸãïŒGridPane fxïŒcontroller = "helloworld.FXMLExampleController"ã
FXMLExampleController.java
package helloworld; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.text.Text; public class FXMLExampleController { @FXML private Text actiontarget; @FXML protected void handleSubmitButtonAction(ActionEvent event) { actiontarget.setText("Sign in button pressed"); } }
ãã¡ãããã³ã³ãã€ã«ããããšãå¿ããªãã§ãã ãããcompile.cmdã«è¿œå ã®è¡ãè¿œå ããŸãã ããã«ãã³ã³ãã€ã«ã³ãã³ãã®åã«ãããç¬ããåé€ããŠãèµ·åãšåäœã®äž¡æ¹ã確èªããŸããã
"C:\Program Files\Java\jdk1.7.0_25\bin\javac" -d out -classpath "C:\Program Files\Java\jre7\lib\jfxrt.jar" src\helloworld\HelloWorld.java "C:\Program Files\Java\jdk1.7.0_40\bin\javac" -d out -classpath "C:\Program Files\Java\jre7\lib\jfxrt.jar" src\helloworld\FXMLExampleController.java @pause
ã³ã³ãã€ã«ããŸãã ç¶ãéããŸãã ã¯ã©ã¹ãéå§ããããjarãèµ·åããŸãã åäœããŸããåã®äŸãšå€èŠ³ãåäœã«éãã¯ãããŸããã ããããã¢ããªã±ãŒã·ã§ã³ãå€æ°ã®ã¢ãžã¥ãŒã«ã«åå²ããããšãå€æããŸãããã·ãŒã³ãäœæããåå¥ã®ã¡ã€ã³ã¯ã©ã¹ããã©ãŒã ã®åå¥ã®èª¬æãåå¥ã®ã€ãã³ããã³ãã©ãŒãåå¥ã®èŠçŽ ã¹ã¿ã€ã«ã§ãã ããã«ãã¹ã¿ã€ã«ã·ãŒããšãã©ãŒã ã®èª¬æã¯ãã¯ã©ã¹ãåã³ã³ãã€ã«ããã«å€æŽã§ããŸããããšãã°ãGridPaneããŒãã«ã®ã»ã«å ã®èŠçŽ ã亀æããããèæ¯ç»åãå€æŽãããã§ããŸãã
ãã ããåäœããŸãããjarã¢ãŒã«ã€ããèŠãŠã¿ãŸãããã ã¡ãªã¿ã«ãHelloWorld.cssãã¡ã€ã«ã¯ãããŸãããã3çªç®ã®ã»ã¯ã·ã§ã³ã®äŸã®ããã«ã.bssããããŸãã Fxml_example.fxmlã¯HelloWorld.cssã«èšåããŠããŸãã ãã ããã¯ã©ã¹ãšjarã®äž¡æ¹ãæ©èœããŸãã åã®äŸã®ããã«ãHelloWorld.cssãšbackground.jpgãout \ helloworldããsrc \ helloworldã«è»¢éããŸãïŒFXMLãã¡ã€ã«ã¯ãã®ãŸãŸã«ããŠãããŸãïŒã ã³ã³ãã€ã«ããŸãã cssããbssãäœæããŸãã ç¶ãéããŸãã ã¯ã©ã¹ãéå§ããŠãããjarãèµ·åããŸãã åã³æ©èœããŸãïŒ ããããããã¯äœã§ãã-圌女ã«ãšã£ãŠäžè¬çã«ããŸãã¯äœããéãã¯ãããŸãããïŒ ãããŠãã¹ã¿ã€ã«ã·ãŒããã¡ã€ã«ã®æ¡åŒµåã¯éèŠã§ã¯ãªãã®ã§ãfxml_example.fxmlã§ããããããã«æ¶å»ããŸããïŒ ä»£ããã«
<stylesheets> <URL value="@HelloWorld.css" /> </stylesheets>
ããŸã
<stylesheets> <URL value="@HelloWorld" /> </stylesheets>
ããããããã°ã©ã ã¯å®éã«ã¯ã¹ã¿ã€ã«ã·ãŒãã®æ¡åŒµã«ã€ããŠæ°ã«ããŸãã-æã§æã®æ¹åã瀺ãã ãã§ãããã«ãããããããŸãã ããããã§ãããïŒ
ãããã®ãã¬ãŒãã³ã°ã®å Žãã§ããŸãããããããçš®é¡ã®ã¹ã¿ã€ã«ã®å€æŽããã¶ã€ã³ãã€ãã³ãåŠçãªã©ãè©Šãããšãã§ããŸãã ãã®ã¯ãŒã¯ããŒã¹ã¯ãã¹ã¯ãããã§ã¯ãªããã©ãŠã¶ã§å®è¡ã§ããŸããããã«ã€ããŠã¯ããã¥ãŒããªã¢ã«ã®6çªç®ã®ã»ã¯ã·ã§ã³ã§å°ã説æããŸãã ããããã»ãšãã©é¢çœããªããã®ããããŸããã¢ããªã±ãŒã·ã§ã³ã®å±éã«ã€ããŠã¯ãå¥ã®ã¬ã€ãããããŸãã 5çªç®ã®ã»ã¯ã·ã§ã³ã«ãåãçµã¿ãããããŸãããHabréã§ã¯ã ããã§JavaFXã®èŠèŠå¹æã«ã€ããŠæ¢ã«ååã«è¿°ã¹ãããŠããã®ã§ããã¥ãŒããªã¢ã«ã§äžããããäŸã¯æåã®4ã€ã®ã»ã¯ã·ã§ã³ã®ãã¬ãŒã ã¯ãŒã¯ã«ã¯ãŸã£ããé©åããŸããã
jfxpub-overviewã«åºã¥ãJavaFXã®HelloWorldããŒãã¯äœ¿ãæãããããšèããŠããŸãã