JavaFX实现简易时钟效果(二)_java

来源:脚本之家  责任编辑:小易  

Parent root = FXMLLoader.load(getClass().getResource("/fxml/dh.fxml"))//getResource(“”)里面纳轿是fxml的地2113址Stage dh=new Stage();//新建差派Stagedh.setTitle("菜单导航");//给舞台赋5261予名称Scene scene = new Scene(root, 600, 400);dh.setScene(scene);dh.show();//打开新的窗4102口1653//获取当前按钮所在的窗口,其中login是按钮洞庆肆的fx:idStage main = (Stage) login.getScene().getWindow();//关闭窗口main.close();,可以在“按钮点2113击事件”中写上读取fmxl为节点对5261象Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml")); 然后把父节点放入某个4102new Scene(root,320,200);然后在显1653示在所用的舞台stage上。追问"显示在所用的舞台stage上"是什么意思呢?不明白。。。追答JavaFX 2.x的所有控件都是在stage上显示,最上层的stage由底层平台提供。比如Application提供,或者Swing接口JFXPanel提供。读取fxml后产生的节点Node需要放在一个场景Scene中,而场景Scene要放在某个舞台stage上。舞台stage由平台(Application, Swing, SWT)产生和提供本回答被提问者采纳www.zgxue.com防采集请勿采集本网。

本文实例为大家分享了JavaFX实现简易时钟效果的具体代码,供大家参考,具体内容如下

其实就是启用一个线bai程,该线程循环地执行: 1.用nanoTime算出每帧的用时。 2.调用worker,并查看信号du量。 3.如果信号量已被释放,那么通过Platform.runLater把renderer传递给JavaFx图形线程(执行完renderer后会释放信号量)。 4.回到第1步

在前一篇博客中,我们已经绘制了一个静止时钟。

怎样在JavaFX中实现菜单 import javafx.scene.*; import javafx.scene.control.*; import javafx.stage.Stage; import javafx.application.Application; import javafx.collections.*; public class FXComboExample extends Application { publi

绘制简易时钟(一)

http://zhidao.baidu.com/question/2012693426283593748 把swing的修改一下吧

首先进行一个微调:让表盘根据窗口大小自动调整大小:

其实就是启用一个线程,该线程循环地执行: 1.用nanoTime算出每帧的用时。 2.调用worker,并查看信号量。 3.如果信号量已被释放,那么通过Platform.runLater把renderer传递给JavaFx图形线程(执行完renderer后会释放信号量)。 4.回到第1步。 这

在 ShowClock.start() 中,添加对面板长宽的监听。

一个ListView通常有两个职责。 (1)将数据填充到布局。 (2)处理用户的选择点击等操作。 第一点很好理解,ListView就是实现这个功能的。第二点也不难做到,在后面的学习中读者会发现,这非常简单。 一个ListView的创建需要3个元素。 (1)List

pane.widthProperty().addListener(ov -> clock.setW(pane.getWidth()));pane.heightProperty().addListener(ov -> clock.setH(pane.getHeight()));

添加对时间和钟表大小的更改方法

在 ClockPane 类中添加:

/** Construct a clock with specified hour, minute, and second */ public ClockPane(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; paintClock(); } /** Set a new hour */ public void setHour(int hour) { this.hour = hour; paintClock(); } /** Set a new minute */ public void setMinute(int minute) { this.minute = minute; paintClock(); } /** Set a new second */ public void setSecond(int second) { this.second = second; paintClock(); } /** Return clock pane's width */ public double getW() { return w; } /** Set clock pane's width */ public void setW(double w) { this.w = w; paintClock(); } /** Return clock pane's height */ public double getH() { return h; } /** Set clock pane's height */ public void setH(double h) { this.h = h; paintClock(); }

用 Timeline 实现动态钟表

在 ShowClock 类中添加:

//设置事件处理对象EventHandler<ActionEvent> eventHandler = e -> { clock.setCurrentTime(); };//每秒结束后触发eventHandlerTimeline animation = new Timeline( new KeyFrame(Duration.millis(1000), eventHandler)); animation.setCycleCount(Timeline.INDEFINITE); //无限循环 animation.play(); //开始动画

就可以让时钟动起来了。

完整代码

ShowClock.java

package primier;import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.stage.Stage;import javafx.scene.layout.*;import javafx.scene.control.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.util.Duration;import javafx.animation.KeyFrame;import javafx.animation.Timeline;public class ShowClock extends Application { @Override //Override the start method in the Application class public void start(Stage primaryStage) { ClockPane clock = new ClockPane(); //设置事件处理对象 EventHandler<ActionEvent> eventHandler = e -> { clock.setCurrentTime(); }; //每秒结束后触发eventHandler Timeline animation = new Timeline( new KeyFrame(Duration.millis(1000), eventHandler)); animation.setCycleCount(Timeline.INDEFINITE); //无限循环 animation.play(); //开始动画 BorderPane pane = new BorderPane(); pane.setCenter(clock); Scene scene = new Scene(pane, 250,250); primaryStage.setTitle("Display Clock"); primaryStage.setScene(scene); primaryStage.show(); pane.widthProperty().addListener(ov -> clock.setW(pane.getWidth())); pane.heightProperty().addListener(ov -> clock.setH(pane.getHeight())); } public static void main (String[] args) { Application.launch(args); }}

ClockPane.java

package primier;import java.util.Calendar;import java.util.GregorianCalendar;import javafx.scene.layout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.scene.shape.Line;import javafx.scene.text.Text;public class ClockPane extends Pane { private int hour; private int minute; private int second; // Clock pane's width and height private double w = 250, h = 250; /** Construct a default clock with the current time*/ public ClockPane() { setCurrentTime(); } /** Construct a clock with specified hour, minute, and second */ public ClockPane(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; paintClock(); } /** Return hour */ public int getHour() { return hour; } /** Set a new hour */ public void setHour(int hour) { this.hour = hour; paintClock(); } /** Return minute */ public int getMinute() { return minute; } /** Set a new minute */ public void setMinute(int minute) { this.minute = minute; paintClock(); } /** Return second */ public int getSecond() { return second; } /** Set a new second */ public void setSecond(int second) { this.second = second; paintClock(); } /** Return clock pane's width */ public double getW() { return w; } /** Set clock pane's width */ public void setW(double w) { this.w = w; paintClock(); } /** Return clock pane's height */ public double getH() { return h; } /** Set clock pane's height */ public void setH(double h) { this.h = h; paintClock(); } /** Set the current time for the clock */ public void setCurrentTime() { //Construct a calendar for the current date and time Calendar calendar = new GregorianCalendar(); //Set current hour, minute and second this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); paintClock(); } /** Paint the clock */ protected void paintClock() { // Initialize clock parameters double clockRadius = Math.min(w,h)*0.8*0.5; double centerX = w/2; double centerY = h/2; // Draw circle Circle circle = new Circle(centerX, centerY, clockRadius); circle.setFill(Color.WHITE); circle.setStroke(Color.BLACK); Text t1 = new Text(centerX-5, centerY-clockRadius+12,"12"); Text t2 = new Text(centerX-clockRadius+3, centerY +5, "9"); Text t3 = new Text(centerX+clockRadius-10, centerY+3, "3"); Text t4 = new Text(centerX-3, centerY+clockRadius-3,"6"); // Draw second hand double sLength = clockRadius * 0.8; double secondX = centerX + sLength * Math.sin(second * (2 * Math.PI / 60)); double secondY = centerY - sLength * Math.cos(second * (2 * Math.PI / 60)); Line sLine = new Line(centerX, centerY, secondX, secondY); sLine.setStroke(Color.GRAY); // Draw minute hand double mLength = clockRadius * 0.65; double minuteX = centerX + mLength * Math.sin(minute * (2 * Math.PI / 60)); double minuteY = centerY - mLength * Math.cos(minute * (2 * Math.PI / 60)); Line mLine = new Line(centerX, centerY, minuteX, minuteY); mLine.setStroke(Color.BLUE); // Draw hour hand double hLength = clockRadius * 0.5; double hourX = centerX + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)); double hourY = centerY - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)); Line hLine = new Line(centerX, centerY, hourX, hourY); sLine.setStroke(Color.GREEN); getChildren().clear(); getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine); }}

1. 后台public class Check {public static boolean checkreturn(String account,String password){boolean checkbool = false;if("account".equals(account)&&"password".equals(password)){checkbool = true;}return checkbool;}}import java.net.URL;import java.util.ResourceBundle;import javafx.event.ActionEvent;import javafx.fxml.FXML;import javafx.fxml.Initializable;import javafx.scene.control.PasswordField;import javafx.scene.control.TextField;public class LoginController implements Initializable {@5261FXML private TextField account;@FXML private PasswordField password;private FXMLTest application;public void setApp(FXMLTest application){this.application = application;}@FXMLpublic void LOGIN_M(ActionEvent event) {application.userlogin(account.getText(), password.getText());}@FXMLprivate void CLEAR_M(ActionEvent event) {account.setText(null);password.setText(null);}@Overridepublic void initialize(URL url, ResourceBundle rb) {// TODO}}import java.net.URL;import java.util.ResourceBundle;import javafx.event.ActionEvent;import javafx.fxml.FXML;import javafx.fxml.Initializable;public class MainController implements Initializable{private FXMLTest application;public void setApp(FXMLTest application){this.application = application;}@FXMLprivate void OUT_M(ActionEvent event) {application.useroutmain();}@Overridepublic void initialize(URL url, ResourceBundle rb) {}}import check.Check;import java.io.InputStream;import java.util.logging.Level;import java.util.logging.Logger;import javafx.application.Application;import javafx.fxml.FXMLLoader;import javafx.fxml.Initializable;import javafx.fxml.JavaFXBuilderFactory;import javafx.scene.Scene;import javafx.scene.layout.AnchorPane;import javafx.stage.Stage;public class FXMLTest extends Application {private Stage stage;private final double MINIMUM_WINDOW_WIDTH = 400.0;private final double MINIMUM_WINDOW_HEIGHT = 250.0;@Overridepublic void start(Stage primaryStage) throws Exception {stage = primaryStage;stage.setTitle("FXML Login Sample");stage.setMinWidth(MINIMUM_WINDOW_WIDTH);stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);gotologin();stage.show();}public void gotologin(){try {LoginController login = (LoginController) replaceSceneContent("FXML_LOGIN.fxml");login.setApp(this);} catch (Exception ex) {Logger.getLogger(FXMLTest.class.getName()).log(Level.SEVERE, null, ex);}}public void gotomain(){try {MainController main = (MainController) replaceSceneContent("FXML_MAIN.fxml");main.setApp(this);} catch (Exception ex) {Logger.getLogger(FXMLTest.class.getName()).log(Level.SEVERE, null, ex);}}public void userlogin(String account,String password){if(Check.checkreturn(account,password)){gotomain();}}public void useroutmain(){gotologin();}private Initializable replaceSceneContent(String fxml) throws Exception {FXMLLoader loader = new FXMLLoader();InputStream in = FXMLTest.class.getResourceAsStream(fxml);loader.setBuilderFactory(new JavaFXBuilderFactory());loader.setLocation(FXMLTest.class.getResource(fxml));AnchorPane page;try {page = (AnchorPane) loader.load(in);} finally {in.close();}Scene scene = new Scene(page, 800, 600);stage.setScene(scene);stage.sizeToScene();return (Initializable) loader.getController();}public static void main(String[] args) {launch(args);}}2. xml配置<?xml version="1.0" encoding="UTF-8"?><?import java.lang.*?><?import java.util.*?><?import javafx.geometry.*?><?import javafx.scene.control.*?><?import javafx.scene.effect.*?><?import javafx.scene.layout.*?><?import javafx.scene.paint.*?><?import javafx.scene.text.*?><AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="250.0" prefWidth="400.0"xmlns:fx="http://javafx.com/fxml" fx:controller="fxmltest.LoginController"><children><VBox layoutX="3.0" layoutY="0.0" prefHeight="250.0" prefWidth="390.0" spacing="20.0"><children><HBox minHeight="37.0" prefHeight="37.0" prefWidth="411.0"><children><Label text="登陆界面4102"><effect><DropShadow height="7.845238095238096" radius="4.1815476190476195" width="10.880952380952381" /></effect><font><Font name="System Bold" size="30.0" /></font><HBox.margin><Insets left="140.0" /></HBox.margin></Label></children></HBox><GridPane alignment="TOP_RIGHT" prefWidth="380.0"><children><Label alignment="TOP_RIGHT" text="Account" GridPane.columnIndex="0" GridPane.halignment="RIGHT" GridPane.rowIndex="0"><effect><DropShadow blurType="TWO_PASS_BOX" height="7.845238095238096" radius="3.675595238095238" width="8.857142857142858" /></effect><font><Font size="25.0" fx:id="x1" /></font><GridPane.margin><Insets right="8.0" fx:id="x2" /></GridPane.margin></Label><Label font="$x1" text="password" GridPane.columnIndex="0" GridPane.halignment="RIGHT" GridPane.margin="$x2" GridPane.rowIndex="1"><effect><DropShadow blurType="TWO_PASS_BOX" height="7.845238095238094" radius="3.6755952380952372" width="8.857142857142854" /></effect></Label><TextField fx:id="account" prefHeight="26.0" prefWidth="268.0" promptText="please input your account" GridPane.columnIndex="1" GridPane.rowIndex="0" /><PasswordField fx:id="password" prefWidth="223.0" promptText="please input your password" GridPane.columnIndex="1" GridPane.rowIndex="1" /><Button id="btn_login" mnemonicParsing="false" onAction="#LOGIN_M" prefWidth="80.0" text="登陆" GridPane.columnIndex="0" GridPane.halignment="RIGHT" GridPane.rowIndex="2"><effect><DropShadow /></effect><GridPane.margin><Insets top="15.0" /></GridPane.margin></Button><Button id="btn_clear" mnemonicParsing="false" onAction="#CLEAR_M" prefWidth="80.0" text="清除1653" GridPane.columnIndex="1" GridPane.rowIndex="2"><effect><DropShadow /></effect><GridPane.margin><Insets left="80.0" top="15.0" /></GridPane.margin></Button></children><columnConstraints><ColumnConstraints hgrow="SOMETIMES" maxWidth="205.0" minWidth="10.0" prefWidth="147.0" /><ColumnConstraints hgrow="SOMETIMES" maxWidth="318.0" minWidth="10.0" prefWidth="243.0" /></columnConstraints><rowConstraints><RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /><RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /><RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /></rowConstraints></GridPane></children><padding><Insets top="30.0" /></padding></VBox></children></AnchorPane><?xml version="1.0" encoding="UTF-8"?><?import java.lang.*?><?import java.util.*?><?import javafx.scene.control.*?><?import javafx.scene.layout.*?><?import javafx.scene.paint.*?><?import javafx.scene.text.*?><AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"xmlns:fx="http://javafx.com/fxml" fx:controller="fxmltest.MainController"><children><Hyperlink layoutX="86.0" layoutY="111.0" text="退出登录!" onAction="#OUT_M"><font><Font size="45.0" /></font></Hyperlink></children></AnchorPane>内容来自www.zgxue.com请勿采集。


  • 本文相关:
  • java web过滤器验证登录防止未登录进入界面
  • 使用java设置字型和颜色的方法详解
  • java连接mysql数据库的方法
  • rocketmq事务消息发送代码流程详解
  • java emoji持久化mysql过程详解
  • java并发系列之abstractqueuedsynchronizer源码分析(概要分析)
  • java实现dijkstra最短路径算法
  • java的arrays数组排序示例分享
  • java 图片与base64相互转化的示例
  • spring security如何使用url地址进行权限控制
  • javafx怎么实现页面的跳转
  • javaFX如何用Button实现跳转到一个已创建的fxml界面
  • 求助,使用JavaFX做的项目,如何实现点击按钮
  • JavaFX如何实现模态窗口
  • 如何理解JavaFX的图形化的编程思想和实现过程?
  • 怎样在JavaFX中实现菜单
  • 求用java中的javafx实现单摆的图形化界面!!!
  • JavaFX怎么实现局部刷新
  • javafx的ListView如何实现多选的操作
  • javafx怎么连接数据库使javafx的界面实现增删查改...
  • 网站首页网页制作脚本下载服务器操作系统网站运营平面设计媒体动画电脑基础硬件教程网络安全c#教程vbvb.netc 语言java编程delphijavaandroidiosswiftscala易语言汇编语言其它相关首页javajava实现动态时钟并设置闹钟功能java多线程编程制作电子时钟java实现的小时钟示例分享java实现的简单数字时钟功能示例java编程小实例—数字时钟的实现代码示例java实现动态模拟时钟java实现时钟效果java实现动态数字时钟javafx实现简易时钟效果javafx实现时钟效果java web过滤器验证登录防止未登录进入界面使用java设置字型和颜色的方法详解java连接mysql数据库的方法rocketmq事务消息发送代码流程详解java emoji持久化mysql过程详解java并发系列之abstractqueuedsynchronizer源码分析(概要分析)java实现dijkstra最短路径算法java的arrays数组排序示例分享java 图片与base64相互转化的示例spring security如何使用url地址进行权限控制java使double保留两位小数的多方intellij idea 2020最新激活码(亲java8 十大新特性详解java.net.socketexception: connjava写入文件的几种方法分享java环境变量的设置方法(图文教程java 十六进制与字符串的转换java list用法示例详解java中file类的使用方法java 字符串截取的三种方法(推荐详解spring中bean的几种注入方式eclipse中导入maven web项目并配置其在tospringboot 注册服务注册中心(zk)的两种方java 汉诺塔hanoi递归、非递归(仿系统递归java泛型详解mybatis实现动态查询、模糊查询功能java中byte输出write到文件的实现方法讲解在action中以struts2的方式输出json数据的在java web项目中添加定时任务的方法使用springboot搭建oauth2.0 server的方法
    免责声明 - 关于我们 - 联系我们 - 广告联系 - 友情链接 - 帮助中心 - 频道导航
    Copyright © 2017 www.zgxue.com All Rights Reserved