JavaFX快速入门完整代码:点击按钮显示当前系统时间示例
生活随笔
收集整理的這篇文章主要介紹了
JavaFX快速入门完整代码:点击按钮显示当前系统时间示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最終效果
sample.fxml
<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.AnchorPane?><AnchorPane prefHeight="309.0" prefWidth="349.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"><children><Button fx:id="myButton" layoutX="60.0" layoutY="73.0" mnemonicParsing="false" onAction="#showDateTime" prefHeight="23.0" prefWidth="219.0" text="Show Date Time" /><TextField fx:id="myTextField" layoutX="60.0" layoutY="117.0" prefHeight="23.0" prefWidth="219.0" /></children> </AnchorPane>sample.Controller
package sample;import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.TextField; import java.net.URL; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.ResourceBundle;public class Controller implements Initializable {@FXMLprivate Button myButton;@FXMLprivate TextField myTextField;//更多請閱讀:https://www.yiibai.com/javafx/javafx-tutorial-for-beginners.html@Overridepublic void initialize(URL location, ResourceBundle resources) {}// When user click on myButton// this method will be called.public void showDateTime(ActionEvent event) {System.out.println("Button Clicked!");Date now= new Date();DateFormat df = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");String dateTimeString = df.format(now);// Show in VIEWmyTextField.setText(dateTimeString);}}sample.Main
package sample;import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage;public class Main extends Application {@Overridepublic void start(Stage primaryStage) throws Exception{Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));primaryStage.setTitle("Hello World");//primaryStage.setScene(new Scene(root, 800, 600));primaryStage.setScene(new Scene(root));primaryStage.show();}public static void main(String[] args) {launch(args);} }參考鏈接
https://www.yiibai.com/javafx/javafx-tutorial-for-beginners.html
總結
以上是生活随笔為你收集整理的JavaFX快速入门完整代码:点击按钮显示当前系统时间示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spark on yarn相关脚本整理2
- 下一篇: javafx FlowPane布局