JavaFX实现简单日历效果

本文实例为大家分享了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();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 学习Java中的日期和时间处理及Java日历小程序的编写

    Java 在 java.util 包中提供了 Date 类,这个类封装了当前的日期和时间. Date 类支持两种构造函数.第一个构造函数初始化对象的当前日期和时间. Date( ) 下面的构造函数接收一个参数等于自1970年1月1日午夜起已经过的毫秒数 Date(long millisec) 一旦有一个可用的日期对象,可以调用以下任何一种支持的方法使用时间: SN 方法和描述 1 boolean after(Date date) 如果调用Date对象包含或晚于指定的日期则返回true,否则,返回

  • JavaWeb项目FullCalendar日历插件使用的示例代码

    本文介绍了JavaWeb项目FullCalendar日历插件使用的示例代码,分享给大家,具体如下: 使用FullCalendar需要引用的文件 1.css文件 2.js文件 <link href="${base}/assets/global/plugins/fullcalendar/fullcalendar.min.css" rel="external nofollow" rel="stylesheet" type="text/c

  • Java实现按年月打印日历功能【基于Calendar】

    本文实例讲述了Java实现按年月打印日历功能.分享给大家供大家参考,具体如下: import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class CalendarBook { public static void main(String[] args) throws ParseException { C

  • Java实现的日历功能完整示例

    本文实例讲述了Java实现的日历功能.分享给大家供大家参考,具体如下: 应用名称:Java日历 用到的知识:Java GUI编程,日期操作 开发环境:win8+eclipse+jdk1.8 功能说明:一个很简单的万年历,可以选择年份和月份,也可以用按钮翻页,日历会实时更新日期,最下方会显示当前操作系统的时间. 效果图: 源代码: CalendarFrame.java import java.awt.BorderLayout; import java.awt.GridLayout; import

  • java实现日历(某年的日历,某月的日历)用户完全自定义

    用户可以自定义打印某一年的年历,即:把某一年的日历全部打印出来 如把2013年的年历打印出来如下: 复制代码 代码如下: January  2013          ---------------------------------------------   Sun   Mon   Tue   Wed   Thu   Fri   Sat     2     3     4     5     7     8     9     10    11    12    14    15    16

  • 日历显示读出输入的年月的java代码

    复制代码 代码如下: import java.awt.*;import java.awt.event.*;import java.util.Calendar; import javax.swing.JOptionPane; class CalendarBean {  //定义一个日历类 String day[]; //天数数组 int year = 2011, month = 9; //给定一个初始年月 public void setYear(int year) {   this.year =

  • Java中的Calendar日历API用法完全解析

    第一部分 Calendar介绍 Calendar 定义: public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> {} Calendar 可以看作是一个抽象类. 它的实现,采用了设计模式中的工厂方法.表现在:当我们获取Calendar实例时,Calendar会根据传入的参数来返回相应的Calendar对象.获取Calendar实例,有以下两种方式: (1) 当我们通过 Cal

  • Java Calendar日历与Date日期的相互转换详解

    Calendar转化为Data 返回标准化的Date public static Date calendarToData(int year, int month, int day) { Calendar calendar = Calendar.getInstance();//日历类的实例化 calendar.set(year, month - 1, day);//设置日历时间,月份必须减一 Date date = calendar.getTime(); // 从一个 Calendar 对象中获取

  • Java实现简单日历小程序 Java图形界面小日历开发

    今天给大家介绍一下如何用Java swing开发一款简单的小日历,下面我们来看代码: 首先创建一个CalendarBean类,用于基本的日期计算: package other1; import java.util.Calendar; public class CalendarBean { String day[]; int year=2005,month=0; public void setYear(int year) { this.year=year; } public int getYear

  • java制作android 日历代码分享

    代码很简单,就不多废话了 复制代码 代码如下: //读取日历事件     public static void getCalendarInfo(Activity activity,String tag){         String[] projection = new String[]{CalendarContract.Events._ID,CalendarContract.Events.TITLE};         ContentResolver cr = activity.getCon

随机推荐