求Java 日历的小程序的源代码
发布网友
发布时间:2022-04-29 04:23
我来回答
共4个回答
热心网友
时间:2023-10-11 07:38
也不知道你具体需求是什么,以前改过一个日历程序,一共四个java类,放在同一个包里。经测试可以运行。
//Start.java
import java.awt.*;
import javax.swing.*;
class Start{
public static void main(String [] args){
DateFrame frame=new DateFrame();
frame.setLocationRelativeTo(frame);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//DateInfo.java
import java.util.*;
public class DateInfo{
private int mYear, mMonth;
private int mDayOfMonth, mFristWeek;
public DateInfo(int year, int month) throws DateException{
mYear = year;
if (month < 0 || month > 12){
throw (new DateException());
}
mMonth = month;
mDayOfMonth = getDayOfMonth(mYear, mMonth);
mFristWeek = getFristWeek(mYear, mMonth);
}
private int getDayOfMonth(int year, int month){
int[][] ary = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
return (ary[isLeapYear(year)][month]);
}
private int isLeapYear(int year){
if (year % 4 == 0 && year % 100 != 0 ||year % 400 == 0){
return (1);
}
else{
return (0);
}
}
private int getFristWeek(int year, int month){
java.util.Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, 1);
return (cal.get(Calendar.DAY_OF_WEEK) - 1);
}
public String toString(){
String str;
str = "\t\t" + mYear + "年" + mMonth + "月\n";
str += "日\t一\t二\t三\t四\t五\t六\n";
int i;
for (i = 1; i <= mFristWeek; i++){
str += " \t";
}
for (int j = 1; j <= mDayOfMonth; j++, i++){
str +=j+"\t" ;
if (i % 7 == 0){
str += "\n";
}
}
return (str);
}
}
//DateFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
class DateFrame extends JFrame implements Runnable{
Calendar date=Calendar.getInstance();
String[] str={"1","2","3","4","5","6","7","8","9","10","11","12"};
JLabel lblYear=new JLabel("年 ");
JLabel lblMonth=new JLabel("月 ");
JLabel lblDate=new JLabel("现在的时间是:");
JLabel lblShowDate=new JLabel();
//javax.swing.JTextField trxt =new JTextField(10);
//trxt.setHorizontalAlignment(JTextField.RIGHT); //设置文本从右边输入
JComboBox cboMonth=new JComboBox(str);
JComboBox cboYear=new JComboBox();
JTextArea txaShow=new JTextArea();
JPanel pnlNorth=new JPanel();
JPanel pnlSOUTH=new JPanel();
JButton btnShow=new JButton("显示");
JButton btnClose=new JButton("关闭");
JScrollPane jsp=new JScrollPane(txaShow);
Container c=this.getContentPane();
public DateFrame(){
Thread thread=new Thread(this);
thread.start();
this.setTitle("玩玩日历拉!!!");
this.setSize(300,260);
for (int i = 1990; i<=2025; i++) {
cboYear.addItem(""+i);
}
cboYear.setSelectedItem(""+(date.get(Calendar.YEAR)));
cboMonth.setSelectedItem(""+(date.get(Calendar.MONTH)+1));
pnlNorth.add(cboYear);
txaShow.setTabSize(4); //设置tab键的距离
txaShow.setForeground(Color.GREEN);
pnlNorth.add(lblYear);
pnlNorth.add(cboMonth);
pnlNorth.add(lblMonth);
pnlNorth.add(lblDate);
pnlNorth.add(lblShowDate);
c.add(pnlNorth,BorderLayout.NORTH);
c.add(jsp);
pnlSOUTH.add(btnShow);
pnlSOUTH.add(btnClose);
c.add(pnlSOUTH,BorderLayout.SOUTH);
btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int year=Integer.parseInt((String)cboYear.getSelectedItem());
int month=Integer.parseInt((String)cboMonth.getSelectedItem());
try {
DateInfo date=new DateInfo(year,month);
txaShow.setText(""+date);
}
catch (DateException ex) {
ex.printStackTrace();
}
}
});
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
public void run(){
try {
while(true){
Thread.sleep(1000);
int hour=date.get(Calendar.HOUR);
int minute=date.get(Calendar.MINUTE);
int second=date.get(Calendar.SECOND);
String str=hour+":"+minute+":"+second;
lblShowDate.setText(str);
//this.repaint();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
//DateException.java
public class DateException extends Exception{
public DateException(){
super("日期数据不合法.");
}
}
热心网友
时间:2023-10-11 07:38
好巧,今天正好网友问过这个问题,顺便发一下,嘿嘿
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CalendarTest {
Calendar d;
int today,thisMonth,thisYear;
int month,year;
public CalendarTest(){
d = new GregorianCalendar(); //默认构造 用今日 初始化参数
today = d.get(Calendar.DAY_OF_MONTH); //当前日
thisMonth = month = d.get(Calendar.MONTH); //当前月
thisYear = year = d.get(Calendar.YEAR); //当前年
}
public void setCalendarMonth(int month){
this.month = month;
}
public void setCalendarYear(int year){
this.year = year;
}
public void printCalendar(){
d.set(Calendar.MONTH, month); //设置月
d.set(Calendar.YEAR, year); //设置年
d.set(Calendar.DAY_OF_MONTH, 1);//设置当前月的第一天
int weekday=d.get(Calendar.DAY_OF_WEEK);//DAY_OF_WEEK 是个从1到7的整数,不是从0开始的数字
int firstDayOfWeek=d.getFirstDayOfWeek();
int indent=0;
while(weekday!=firstDayOfWeek){
indent++;
d.add(Calendar.DAY_OF_MONTH, -1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
//String [] weekdayNames=new DateFormatSymbols().getShortWeekdays();
String [] weekdayNames={"","日","一","二","三","四","五","六"};
do{
System.out.printf("%2s", weekdayNames[weekday]);
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}while(weekday != firstDayOfWeek);
System.out.println();
for(int i=0;i<indent;i++){
System.out.print(" ");
d.add(Calendar.DAY_OF_MONTH, 1);
}
d.set(Calendar.DAY_OF_MONTH, 1);
do{
int day=d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d",day);
if (day == today && d.get(Calendar.MONTH) == thisMonth
&& d.get(Calendar.YEAR) == thisYear)//判断是不是今日
System.out.print("*");
d.add(Calendar.DAY_OF_MONTH, 1);
weekday=d.get(Calendar.DAY_OF_WEEK);
if(weekday==firstDayOfWeek)System.out.println();
}while(d.get(Calendar.MONTH)==month);
}
public static void main(String [] args){
CalendarTest ct = new CalendarTest();
//如果未指定年、月,那么打印出来的月历 就是当前月,*代表今天
//ct.setCalendarMonth(7); //指定月
//ct.setCalendarYear(2011); //指定年
ct.printCalendar();
}
}
热心网友
时间:2023-10-11 07:38
package mike.util.calendar;
import mike.util.Print;
/**
* This class is used for printing some year's calendar.
* You should have a foundation what day is the first day ?
* Like the 2000 year. the first day is saturday.
* @author mike
* @time 2011.7.27
*
*/
public class MyCalendar {
/**
* This method for getting the what day is the first day of year.
* The first day is sunday of 2000 year.
* @param y the year
* @return what day
*/
public static int getYearWeekDay(int y){
if(y < 1900 || y > 2100)
throw new IllegalArgumentException("The year is between 1900 and 2100");
int sum = 0;
if(y >= 2000){
for(int year = 2000; year < y; year++)
sum += getYearDays(year);
return (sum + 6) % 7;
}
else{
for(int year = y; year < 2000; year++)
sum += getYearDays(y);
return (-sum % 7 + 6) % 7;//sum = n*7 + 6-x
}
}
/**
* returns days of y.
*/
public static int getYearDays(int y) {
return isLeapYear(y)?366:365;
}
/**
* Judge whether in 2000 for a leap year.
*/
public static boolean isLeapYear(int y) {
/*if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
return true;
return false;*/
return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}
/**
* count some month' first day is what day.
*/
public static int getMonthWeekDay(int y, int m){
if( y < 1900 || y > 2100 || m < 1 || m >12)
throw new IllegalArgumentException("Illegal argument.");
int sum = 0;
for( int i = 1 ; i < m; i++){
sum += getMonthDays(y,i);
}
return (getYearWeekDay(y) + sum) % 7;
}
public static int getMonthDays(int y, int m) {
switch(m){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return isLeapYear(y)?29:28;
default:
return 0;
}
}
public static void printYear(int y){
for(int i = 1; i <= 12; i++)
printMonth(y,i);
}
private static void printMonth(int y, int m) {
Print.print(" ---------------- " + y + " - " + m + " --------------- ");
Print.print("sun" + "\t" + "mon" + "\t" + "tues" +" \t" + "wedsen" + "\t" +
"thurs" + "\t" + "fri" + "\t" + "satur");
int w = getMonthWeekDay(y,m);
if( w != 0)
for( int i = 0 ; i < w ; i++)
Print.printnb("\t");
for(int i = 1 ; i <= getMonthDays(y,m);i++){
Print.printnb(i + "\t");
w++;
w %= 7;
if( w == 0 && i <= getMonthDays(y,m))
Print.print();
}
Print.print();
}
public static void main(String [] args){
MyCalendar.printYear(2011);
}
}
自己写的。输出语句自己改为System.out.println();
热心网友
时间:2023-10-11 07:39
当在一段代码块定义一个变量时,Java就在栈中 为这个变量分配内存空间,当该变量退出该作用域后,Java会自动释放掉为该变量所分配的内存空间,该内存空间可以立即被另作他用。
Java内存分配中的堆
堆内存用来存放由new创建的对象和数组。 在堆中分配的内存,由Java虚拟机的自动垃圾回收器来管理。