JavaFX快速入门完整代码:点击按钮显示当前系统时间示例
2023-09-14 09:02:01 时间
最终效果
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 {
@FXML
private Button myButton;
@FXML
private TextField myTextField;
//更多请阅读:https://www.yiibai.com/javafx/javafx-tutorial-for-beginners.html
@Override
public 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 VIEW
myTextField.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 {
@Override
public 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
相关文章
- 【推荐系统】知乎live入门2.细节补充
- 机器学习入门18 - 生产机器学习系统(Production ML Systems)
- 【推荐系统】知乎live入门5.常用技能与日常工作
- vue系统修饰符:@click.ctrl.exact CTRL键
- Linux系统负载查看命令合集
- 反向代理-银行系统使用,在防火墙外面放代理服务器,在防火墙里面放上真正的内容服务器
- 解决windows10系统ssh-add报错的问题
- 北京遇上西雅图之:当记账系统遇上并发
- PHP 零基础入门笔记(7):PHP 常用的系统函数
- 【关于ChatGPT的30个问题】29、是否有其他与ChatGPT类似的人工智能系统?/ By 禅与计算机程序设计艺术
- 计算机毕设 SSM Vue 大学生就业推荐系统(含源码+论文)
- y144.第八章 Servless和Knative从入门到精通 -- 消息系统基础(八)
- 【项目实战】Windows系统中环境变量(PATH、CLASSPATH)入门介绍
- Zabbix 监控系统入门简介
- 1-系统C盘迁移到新买的固态硬盘SSD中有影响但不主要,但有必要将系统盘C盘迁移到新安装的 固态硬盘SSD中吗?
- git快速入门(windows系统)
- nginx开发从入门到精通【淘宝核心系统服务器】