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

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

Parent root = FXMLLoader.load(getClass().getResource("/fxml/dh.fxml"))//getResource(“”)里面是fxml的地址Stage dh=new Stage();//新建2113Stagedh.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“按钮点击事件”中5261写上读取fmxl为节点对象Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml")); 然后4102把父节点放入某1653个new Scene(root,320,200);然后在显示在所用的舞台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的修改一下吧

主函数文件 ShowClock:

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

package primier;import javafx.application.Application;import javafx.geometry.Insets;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.stage.Stage;import javafx.scene.paint.Color;import javafx.scene.layout.*;import javafx.scene.control.*;import javafx.scene.image.Image;import javafx.scene.image.ImageView;import javafx.scene.shape.Line;public class ShowClock extends Application { @Override //Override the start method in the Application class public void start(Stage primaryStage) { // 创建时钟面板 ClockPane clock = new ClockPane(); // 当前时间整理为字符串 String timeString = clock.getHour() + ":" + clock.getMinute() + ":" + clock.getSecond(); Label lbCurrentTime = new Label(timeString); BorderPane pane = new BorderPane(); pane.setCenter(clock); pane.setBottom(lbCurrentTime); // 将时钟字符串设为靠上居中 BorderPane.setAlignment(lbCurrentTime, Pos.TOP_CENTER); Scene scene = new Scene(pane, 250,250); primaryStage.setTitle("Display Clock"); primaryStage.setScene(scene); primaryStage.show(); } public static void main (String[] args) { Application.launch(args); }}

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

ClockPane 类

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; // 时钟面板的宽度和高度 private double w = 250, h = 250; /** 用当前时间创建时钟 */ public ClockPane() { setCurrentTime(); } /** Return hour */ public int getHour() { return hour; } /** Return minute */ public int getMinute() { return minute; } /** Return second */ public int getSecond() { return second; } /** Set the current time for the clock */ public void setCurrentTime() { // 用当前时间创建Calendar类 Calendar calendar = new GregorianCalendar(); this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); paintClock(); } /** 绘制时钟 */ protected void paintClock() { double clockRadius = Math.min(w,h)*0.4; // 时钟半径 // 时钟中心x, y坐标 double centerX = w/2; double centerY = h/2; // 绘制钟表 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"); // 秒针 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); // 分针 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); // 时针 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. 后台2113public 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="清除" 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="退出登录1653!" onAction="#OUT_M"><font><Font size="45.0" /></font></Hyperlink></children></AnchorPane>内容来自www.zgxue.com请勿采集。


  • 本文相关:
  • java 中动态代理(jdk,cglib)实例代码
  • java stringbuilder和stringbuffer源码分析
  • 探索java i/o 模型的演进
  • mybatis-plus generator配置详解
  • java中获取键盘输入值的三种方法介绍
  • java基于jedislock—redis分布式锁实现示例代码
  • spring.net控制反转ioc入门使用
  • spring中使用atomikos+druid实现经典分布式事务的方法
  • java并发编程之同步器代码示例
  • mybatis中#{}和${}的区别详解
  • javafx怎么实现页面的跳转
  • javaFX如何用Button实现跳转到一个已创建的fxml界面
  • 求助,使用JavaFX做的项目,如何实现点击按钮
  • JavaFX如何实现模态窗口
  • 如何理解JavaFX的图形化的编程思想和实现过程?
  • 怎样在JavaFX中实现菜单
  • 求用java中的javafx实现单摆的图形化界面!!!
  • JavaFX怎么实现局部刷新
  • javafx的ListView如何实现多选的操作
  • javafx怎么连接数据库使javafx的界面实现增删查改...
  • 网站首页网页制作脚本下载服务器操作系统网站运营平面设计媒体动画电脑基础硬件教程网络安全c#教程vbvb.netc 语言java编程delphijavaandroidiosswiftscala易语言汇编语言其它相关首页javajavafx实现简易时钟效果javafx实现时钟效果java实现时钟效果java实现动态数字时钟java实现动态模拟时钟java实现的简单数字时钟功能示例java实现动态时钟并设置闹钟功能java编程小实例—数字时钟的实现代码示例java多线程编程制作电子时钟java实现的小时钟示例分享java 中动态代理(jdk,cglib)实例代码java stringbuilder和stringbuffer源码分析探索java i/o 模型的演进mybatis-plus generator配置详解java中获取键盘输入值的三种方法介绍java基于jedislock—redis分布式锁实现示例代码spring.net控制反转ioc入门使用spring中使用atomikos+druid实现经典分布式事务的方法java并发编程之同步器代码示例mybatis中#{}和${}的区别详解java使double保留两位小数的多方intellij idea 2020最新激活码(亲java8 十大新特性详解java.net.socketexception: connjava写入文件的几种方法分享java环境变量的设置方法(图文教程java 十六进制与字符串的转换java list用法示例详解java中file类的使用方法java 字符串截取的三种方法(推荐java使用scanner类进行控制台输入实现方法java设置pdf有序和无序列表的知识点总结java编程中二维数组的初始化和基本操作实解决idea中下载free maven plugin插件无效springboot 整合jdbc和mybatis的方法java跨域问题的处理详解java在线打开word文档并强制留痕的方法详解springboot应用服务启动与安全终止java语言实现简单ftp软件 ftp软件效果图预maven安装过程图文详解
    免责声明 - 关于我们 - 联系我们 - 广告联系 - 友情链接 - 帮助中心 - 频道导航
    Copyright © 2017 www.zgxue.com All Rights Reserved