JavaFX实现简单日历效果
程序员文章站
2022-06-22 08:17:48
本文实例为大家分享了javafx实现简单日历效果的具体代码,供大家参考,具体内容如下1.先看效果:2.代码:1)clockedge.java类这个类(pane)主要是用来设置时钟边缘(为了美观对直了半...
本文实例为大家分享了javafx实现简单日历效果的具体代码,供大家参考,具体内容如下
1.先看效果:
2.代码:
1)clockedge.java类
这个类(pane)主要是用来设置时钟边缘(为了美观对直了半天,其实想想也没必要~。~)
package com.javabasic.javafx; 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; /** * @version 1.00 2016-10-15 * @author administrator * 功能:显示时钟边缘 */ public class clockedge extends pane{ private double w , h; public clockedge(){ this(200, 200); paint(); } public clockedge(double w, double h){ this.w = w; this.h = h; paint(); } public double getw() { return w; } public void setw(double w) { this.w = w; paint(); } public double geth() { return h; } public void seth(double h) { this.h = h; paint(); } public void paint(){ double clockradius = math.min(w, h) * 0.8 * 0.5; double centerx = w / 2; double centery = h / 2; circle circle = new circle(centerx, centery, clockradius); circle.setfill(color.white); circle.setstroke(color.black); //假设一个字符宽7.6个像素,高8.8个像素 text text1 = new text(centerx + clockradius * math.sin(1 * math.pi / 6) - 7.6 + 1.7 * (1 + math.cos(2 * math.pi / 6)) - 2.2 * math.sin(2 * math.pi / 6), centery - clockradius * math.cos(1 * math.pi / 6) + 8.8 + 1.7 * math.sin(2 * math.pi / 6) - 2.2 * (1 - math.cos(2 * math.pi / 6)), "1"); text text2 = new text(centerx + clockradius * math.sin(2 * math.pi / 6) - 7.6 + 1.7 * (1 + math.cos(4 * math.pi / 6)) - 2.2 * math.sin(4 * math.pi / 6), centery - clockradius * math.cos(2 * math.pi / 6) + 8.8 + 1.7 * math.sin(4 * math.pi / 6) - 2.2 * (1 - math.cos(4 * math.pi / 6)), "2"); text text3 = new text(centerx + clockradius * math.sin(3 * math.pi / 6) - 7.6 + 1.7 * (1 + math.cos(6 * math.pi / 6)) - 2.2 * math.sin(6 * math.pi / 6), centery - clockradius * math.cos(3 * math.pi / 6) + 8.8 + 1.7 * math.sin(6 * math.pi / 6) - 2.2 * (1 - math.cos(6 * math.pi / 6)), "3"); text text4 = new text(centerx + clockradius * math.sin(4 * math.pi / 6) - 7.6 + 1.7 * (1 + math.cos(8 * math.pi / 6)) + 2.2 * math.sin(8 * math.pi / 6), centery - clockradius * math.cos(4 * math.pi / 6) + 1.7 * math.sin(8 * math.pi / 6) + 2.2 * (1 - math.cos(8 * math.pi / 6)), "4"); text text5 = new text(centerx + clockradius * math.sin(5 * math.pi / 6) - 7.6 + 1.7 * (1 + math.cos(10 * math.pi / 6)) + 2.2 * math.sin(10 * math.pi / 6), centery - clockradius * math.cos(5 * math.pi / 6) + 1.7 * math.sin(10 * math.pi / 6) + 2.2 * (1 - math.cos(10 * math.pi / 6)), "5"); text text6 = new text(centerx + clockradius * math.sin(6 * math.pi / 6) - 7.6 + 1.7 * (1 + math.cos(12 * math.pi / 6)) + 2.2 * math.sin(12 * math.pi / 6), centery - clockradius * math.cos(6 * math.pi / 6) + 1.7 * math.sin(12 * math.pi / 6) + 2.2 * (1 - math.cos(12 * math.pi / 6)), "6"); text text7 = new text(centerx + clockradius * math.sin(7 * math.pi / 6) - 1.7 * (1 + math.cos(14 * math.pi / 6)) + 2.2 * math.sin(14 * math.pi / 6), centery - clockradius * math.cos(7 * math.pi / 6) - 1.7 * math.sin(14 * math.pi / 6) + 2.2 * (1 - math.cos(14 * math.pi / 6)), "7"); text text8 = new text(centerx + clockradius * math.sin(8 * math.pi / 6) - 1.7 * (1 + math.cos(16 * math.pi / 6)) + 2.2 * math.sin(16 * math.pi / 6), centery - clockradius * math.cos(8 * math.pi / 6) - 1.7 * math.sin(16 * math.pi / 6) + 2.2 * (1 - math.cos(16 * math.pi / 6)), "8"); text text9 = new text(centerx + clockradius * math.sin(9 * math.pi / 6) - 1.7 * (1 + math.cos(18 * math.pi / 6)) + 2.2 * math.sin(18 * math.pi / 6), centery - clockradius * math.cos(9 * math.pi / 6) - 1.7 * math.sin(18 * math.pi / 6) + 2.2 * (1 - math.cos(18 * math.pi / 6)), "9"); text text10 = new text(centerx + clockradius * math.sin(10 * math.pi / 6) - 3.8 * (1 + math.cos(20 * math.pi / 6)) - 2.2 * math.sin(20 * math.pi / 6), centery - clockradius * math.cos(10 * math.pi / 6) + 8.8 - 3.8 * math.sin(20 * math.pi / 6) - 2.2 * (1 - math.cos(20 * math.pi / 6)), "10"); text text11 = new text(centerx + clockradius * math.sin(11 * math.pi / 6) - 3.8 * (1 + math.cos(22 * math.pi / 6)) - 2.2 * math.sin(22 * math.pi / 6), centery - clockradius * math.cos(11 * math.pi / 6) + 8.8 - 3.8 * math.sin(22 * math.pi / 6) - 2.2 * (1 - math.cos(22 * math.pi / 6)), "11"); text text12 = new text(centerx + clockradius * math.sin(12 * math.pi / 6) - 3.8 * (1 + math.cos(24 * math.pi / 6)) - 2.2 * math.sin(24 * math.pi / 6), centery - clockradius * math.cos(12 * math.pi / 6) + 8.8 - 3.8 * math.sin(24 * math.pi / 6) - 2.2 * (1 - math.cos(24 * math.pi / 6)), "12"); getchildren().addall(circle, text1, text2, text3, text4, text5, text6, text7, text8, text9, text10, text11, text12); for(int i =1; i<= 60; i++){ double pointx1 = centerx + clockradius * 0.95 * math.sin(i * (2 * math.pi) / 60); double pointy1 = centery - clockradius * 0.95 * math.cos(i * (2 * math.pi) / 60); double pointx2 = centerx + clockradius * 1.0 * math.sin(i * (2 * math.pi) / 60); double pointy2 = centery - clockradius * 1.0 * math.cos(i * (2 * math.pi) / 60); if(i % 5 == 0) continue; else{ line point = new line(pointx1, pointy1, pointx2, pointy2); getchildren().add(point); } } } }
2)clockpointer.java类
这个类(pane)主要用来显示时钟的三个指针(时、分、秒);
package com.javabasic.javafx; import java.util.calendar; import java.util.gregoriancalendar; import javafx.scene.layout.pane; import javafx.scene.paint.color; import javafx.scene.shape.line; /** * @version 1.00 2016-10-15 * @author administrator * 功能:显示时钟指针 */ public class clockpointer extends pane{ private int hour; private int minute; private int second; private double w = 200, h = 200; public clockpointer(){ setcurrenttime(); } public clockpointer(int hour, int minute, int second){ this.hour = hour; this.minute = minute; this.second = second; paint(); } public int gethour(){ return hour; } public void sethour(int hour){ this.hour = hour; paint(); } public int getminute(){ return minute; } public void setminute(int minute){ this.minute = minute; paint(); } public int getsecond(){ return second; } public void setsecond(int second){ this.second = second; paint(); } public double getw() { return w; } public void setw(double w) { this.w = w; paint(); } public double geth() { return h; } public void seth(double h) { this.h = h; paint(); } public string setcurrenttime(){ string str1, str2, str3; calendar calendar = new gregoriancalendar(); this.hour = calendar.get(calendar.hour_of_day); this.minute = calendar.get(calendar.minute); this.second = calendar.get(calendar.second); str1 = string.valueof(hour); str2 = string.valueof(minute); str3 = string.valueof(second); if (hour < 10) { str1 = "0" + hour; } if (minute < 10) { str2 = "0" + minute; } if (second < 10) { str3 = "0" + second; } paint(); return str1 + " : " + str2 + " : " + str3; } protected void paint(){ getchildren().clear(); double clockradius = math.min(w, h) * 0.8 * 0.5; double centerx = w / 2; double centery = h / 2; 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.red); 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 * (2 * math.pi) / 12); double houry = centery - hlength * math.cos(hour * (2 * math.pi) / 12); line hline = new line(centerx, centery, hourx, houry); mline.setstroke(color.green); getchildren().addall(sline, mline, hline); } }
3)calendarpanel.java类
这个类(pane)用来显示日历(这里用到了一个字体cooper black一般系统应该都有,没有就得添加)
package com.javabasic.javafx; import javafx.scene.layout.pane; import javafx.scene.layout.columnconstraints; import javafx.scene.layout.gridpane; import javafx.scene.control.label; import javafx.scene.paint.color; import javafx.geometry.hpos; import javafx.geometry.insets; import javafx.geometry.pos; import javafx.scene.text.font; import javafx.scene.text.fontweight; import javafx.scene.text.text; import java.util.calendar; /** * @version 1.00 2016-10-17 * @author administrator * 功能:显示日历 */ public class calendarpanel extends pane{ private int year; private int month; private int day; private int firstdayofweek; private int totaldayofmonth; public calendarpanel(){ calendar calendar = calendar.getinstance(); year = calendar.get(calendar.year); month = calendar.get(calendar.month) + 1; day = calendar.get(calendar.day_of_month); totaldayofmonth = calendar.getactualmaximum(calendar.date); calendar.set(calendar.day_of_month, 1); firstdayofweek = calendar.get(calendar.day_of_week) - 1; paint(); } public void paint(){ gridpane cp = new gridpane(); string[] week = new string[]{"sun", "mon", "tue", "wed", "thu", "fri", "sat"}; cp.setpadding(new insets(30, 30, 10, 10)); text text1 = new text(year + "年" + month + "月"); text1.setstroke(color.green); cp.setalignment(pos.center); cp.add(text1, 0, 0); gridpane.setcolumnspan(text1, 7); gridpane.sethalignment(text1, hpos.center); for(int i = 0; i < 7; i++){ label label = new label(week[i]); if(i == 0 || i == 6) label.settextfill(color.red); cp.add(label, i, 1); cp.getcolumnconstraints().add(new columnconstraints(30)); gridpane.sethalignment(label, hpos.center); } for(int j = 0; j < totaldayofmonth; j++){ label label = new label(j + 1 +""); if (j + 1 == day) label.setfont(font.font("cooper black", fontweight.bold, 20)); int k = firstdayofweek + j; if((k % 7 == 0) || (k % 7 == 6)) label.settextfill(color.red); cp.add(label, k % 7, 2 + k / 7); gridpane.sethalignment(label, hpos.right); } getchildren().add(cp); } }
4)主面板clockmainpanel.java
这个类(pane)是用于将上面三个面板合成主面板
package com.javabasic.javafx; import javafx.animation.keyframe; import javafx.animation.timeline; import javafx.application.application; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.scene.scene; import javafx.scene.layout.borderpane; import javafx.scene.layout.stackpane; import javafx.scene.text.font; import javafx.scene.text.text; import javafx.stage.stage; import javafx.util.duration; /** * @version 1.00 2016-10-17 * @author administrator * 功能:各个面板pane合成一个时钟面板clockmainpanel */ public class clockmainpanel extends application { string nowtime; public static void main(string[] args) { // todo auto-generated method stub application.launch(args); } public void start(stage primarystage){ clockedge ce = new clockedge(); clockpointer cp = new clockpointer(); stackpane sp = new stackpane(ce, cp); stackpane nt = new stackpane(); calendarpanel capa = new calendarpanel(); borderpane bp = new borderpane(); bp.setleft(sp); bp.setright(capa); bp.setbottom(nt); scene scene = new scene(bp, 420, 200); eventhandler<actionevent> eventhandler = e ->{ nt.getchildren().clear(); nowtime = (string) cp.setcurrenttime(); text tt= new text(nowtime); tt.setfont(font.font("times new roman", 20)); nt.getchildren().add(tt); }; timeline animation = new timeline( new keyframe(duration.millis(1000), eventhandler)); animation.setcyclecount(timeline.indefinite); animation.play(); primarystage.settitle("perpetual calendar"); primarystage.setscene(scene); primarystage.show(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 基于xml文件的IOC配置