java实现时钟日历代码 java时钟程序代码( 三 )


Calendar now = new GregorianCalendar();
int nowh = now.get(Calendar.HOUR_OF_DAY);
int nowm = now.get(Calendar.MINUTE);
int nows = now.get(Calendar.SECOND);
String st=fmTime.format(now.getTime());
//在窗体上显示时间
g.setColor(Color.pink);
g.fillRect(L, T, 50, 28);
g.setColor(Color.blue);
g.drawString(st,L+2,T+26);
//计算时间与度数的关系
ss = 90 - nows*6;
mm = 90 - nowm*6;
hh = 90 - nowh*30 - nowm/2;
x0 = r+40+L;
y0 = r+40+T;
g2D.setStroke(new BasicStroke(1.2f));
//擦除秒针
//if(olds_x0){
//g.setColor(getBackground());
//// g.setColor(Color.gray);
//g.drawLine(x0, y0, olds_x, h-olds_y); // (?)
//}
//绘制秒针
x = (int)(r*0.9*Math.cos(RAD*ss))+x0;
y = (int)(r*0.9*Math.sin(RAD*ss))+y0-2*T;
g.setColor(Color.yellow);
g.drawLine(x0, y0, x, h-y);
olds_x = x;
olds_y = y;
g2D.setStroke(new BasicStroke(2.2f));
//擦除分针
//if(old_m!=mm){
//g.setColor(getBackground());
//g.drawLine(x0,y0,oldm_x,h-oldm_y);
//}
//绘制分针
x = (int)(r*0.7*Math.cos(RAD*mm))+x0;
y = (int)(r*0.7*Math.sin(RAD*mm))+y0-2*T;
g.setColor(Color.green);
g.drawLine(x0,y0,x,h-y);
oldm_x = x;
oldm_y = y;
old_m = mm;
g2D.setStroke(new BasicStroke(3.2f));
//擦除时针
//if(old_h!=hh){
//g.setColor(getBackground());
//g.drawLine(x0,y0,oldh_x,h-oldh_y);
//}
//绘制时针
x = (int)(r*0.5*Math.cos(RAD*hh))+x0;
y = (int)(r*0.5*Math.sin(RAD*hh))+y0-2*T;
g.setColor(Color.red);
g.drawLine(x0,y0,x,h-y);
oldh_x = x;
oldh_y = y;
old_h = hh;
}
public static void main(String[] args){
new ClockPointer();
}
}
//整理一下
Java做日历,日历格式参考Windows系统托盘的日期和时间程序 。import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class Game {
public void theCalendar(String str) throws ParseException {
Date date = DateFormat.getDateInstance().parse(str);
Calendar c = new GregorianCalendar();
c.setTime(date);
int year= c.get(Calendar.YEAR); //返回年
int month = c.get(Calendar.MONTH); // 返回月
int today = c.get(Calendar.DAY_OF_MONTH);// 返回日
System.out.println("日\t一\t二\t三\t四\t五\t六");
c.set(Calendar.DAY_OF_MONTH, 1);//把当前日期设置为1号
int a = c.get(Calendar.DAY_OF_WEEK);//返回当前月份1号是星期几
for(int i=1; ia; i++) {
System.out.print("\t");
}
while(c.get(Calendar.MONTH) == month) {
if(c.get(Calendar.DAY_OF_MONTH) == today) {
System.out.printf("%2d●\t", c.get(Calendar.DAY_OF_MONTH));
} else {
System.out.printf("%2d\t", c.get(Calendar.DAY_OF_MONTH));
}
if(c.get(Calendar.DAY_OF_WEEK) == 7) {
System.out.println();
}
c.add(Calendar.DAY_OF_MONTH, 1);
}
}
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("请按照如下格式输入字符串1990-09-07");
String str = sc.nextLine();
new Game().theCalendar(str);
}
}
怎么用java写日历以下是两个类,请楼主分别存成两个java文件:
其中
MainFrame.java是显示日历程序,Clock.java是日历计算程序 。编译后运行MainFrame这个类即可 。
1.MainFrame.java
---
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

推荐阅读