javaFX制作helloWorld的几种方式
//起因:因为即便是使用了JavaFX Scene Builder来直接图形化排版,但是仍然对应javaAPI,了解整体结构还是很有必要的,这是有清晰逻辑的基础。
environment:{
Spring Tool Suite
Version: 3.8.4.RELEASE
Build Id: 201703310825
Platform: Eclipse Neon.3 (4.6.3)
}
1 package application; 2 3 import javax.swing.JOptionPane; 4 import javafx.application.Application; 5 //import javafx.event.ActionEvent; 6 import javafx.stage.Stage; 7 import javafx.scene.Scene; 8 //import javafx.scene.control.Alert; 9 //import javafx.scene.control.Alert.AlertType; 10 import javafx.scene.control.Button; 11 //import javafx.scene.control.Dialog; 12 //import javafx.scene.layout.BorderPane; 13 import javafx.scene.layout.FlowPane; 14 public class Main extends Application { 15 @Override 16 public void start(Stage primaryStage) { 17 try { 18 FlowPane root = new FlowPane(); 19 //-----------------test-----------------add----------( 20 Button button = new Button("hello JavaFX"); 21 root.getChildren().add(button); 22 button.setOnAction((event)->{//lambda表达式 23 //不会用FX的弹出对话框 24 JOptionPane.showMessageDialog(null, "Hello JavaFX"); 25 }); 26 //-----------------test-----------------add----------) 27 Scene scene = new Scene(root,400,400); 28 scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 29 primaryStage.setScene(scene); 30 primaryStage.show(); 31 } catch(Exception e) { 32 e.printStackTrace(); 33 } 34 } 35 36 public static void main(String[] args) { 37 launch(args); 38 39 } 40 }View Code
2.way2:way1不要直接Finish,next到最后,Declarative UI->language:FXML
1 package application; 2 3 import javafx.application.Application; 4 import javafx.stage.Stage; 5 import javafx.scene.Scene; 6 import javafx.scene.layout.FlowPane; 7 import javafx.fxml.FXMLLoader; 8 9 10 public class Main extends Application { 11 @Override 12 public void start(Stage primaryStage) { 13 try { 14 FlowPane root = (FlowPane)FXMLLoader.load(getClass().getResource("Sample.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 }View Code
1 package application; 2 3 import javafx.application.Application; 4 import javafx.stage.Stage; 5 import javafx.scene.Scene; 6 import javafx.scene.layout.FlowPane; 7 import javafx.fxml.FXMLLoader; 8 9 10 public class Main extends Application { 11 @Override 12 public void start(Stage primaryStage) { 13 try { 14 FlowPane root = (FlowPane)FXMLLoader.load(getClass().getResource("Sample.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 }View Code
SampleController.java:
1 package application; 2 3 import java.net.URL; 4 import java.util.ResourceBundle; 5 6 import javafx.event.ActionEvent; 7 import javafx.fxml.FXML; 8 import javafx.fxml.Initializable; 9 //import javafx.scene.control.Button; 10 import javafx.scene.control.Label; 11 12 public class SampleController implements Initializable { 13 @FXML 14 private Label show_text; 15 @Override 16 public void initialize(URL location, ResourceBundle resources) { 17 // TODO Auto-generated method stub 18 19 } 20 public void showText(ActionEvent actionEvent){ 21 show_text.setText("你点了按钮"); 22 System.out.println("console"); 23 } 24 }View Code
Sample.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.FlowPane?> 7 8 <FlowPane prefHeight="497.0" prefWidth="564.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 9 fx:controller="application.SampleController"> 10 <children> 11 <Label fx:id="show_text" text="我是一个标签" /> 12 <Button id="btn1" mnemonicParsing="false" onAction="#showText" text="如何设置设置事件处理" /> 13 </children> 14 </FlowPane>View Code
还有一种.fxgraph的,,,,待定--------
优质内容筛选与推荐>>