JDK8自帶JavaFx庫,無需額外導入jar包,JDK8下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htmlhtml
Eclipse luna:http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/lunasr1ajava
NetBeans:https://netbeans.org/downloads/oracle
搭建好環境後,下面看看如何用JavaFx開發一個最簡單的用戶界面。eclipse
public class BasicFrame extends Application{ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Basic Frame"); primaryStage.show(); } }
效果:ide
若是要在界面上顯示東西,則須要多一個Scene對象,能夠經過Stage對象的setScene()方法設置一個Scene。一個Scene對象須要一個根節點,能夠把佈局做爲根節點,爲了在後面學習各類控件時方便展現與對比,如無另外說明,則一概使用HBox這個將其子元素橫向排列的佈局。此時的代碼爲這樣:佈局
public class BasicFrame extends Application{ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Basic Frame"); HBox root = new HBox(); primaryStage.setScene(new Scene(root, 400, 300)); primaryStage.show(); } }
這時若是要在界面上顯示東西,則把要顯示的界面元素加到root的子元素列表裏便可:
學習
root.getChildren().add(new Label("sample"));
這時的整體代碼:
spa
public class BasicFrame extends Application{ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Basic Frame"); HBox root = new HBox(); root.getChildren().add(new Label("sample")); primaryStage.setScene(new Scene(root, 400, 300)); primaryStage.show(); } }
效果:code
若是但願元素能夠居中顯示,能夠爲HBox佈局增長居中設置:htm
root.setAlignment(Pos.CENTER);
效果:
至此,用來學習JavaFx各類界面元素的最小支持環境就準備就緒了,從下一篇文章咱們開始學習使用JavaFx基本控件。