最近在学习java窗体开发,自己做了一个万年历,还请大家多多指点
package awt;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class MyCalendar extends JFrame implements ActionListener
{
//private JTextField text;
TextField text=new TextField("2012");
int year;
int month=0,date=1;
int week=0;
int max=1;
JTextArea trea =new JTextArea();//创建一个文本域对象
public MyCalendar()
{
super("万年历");
this.setBounds(300, 300, 400, 400);
this.setResizable(true);
this.setBackground(Color.BLUE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
this.add(text);
JScrollPane scr = new JScrollPane(trea);//创建一个滚动条对象
//JScrollPane scr=new JScrollPane(trea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scr.setBounds(120, 10, 300, 300);
Font font=new Font("宋体",Font.PLAIN,15);
this.add(scr);
text.setEditable(true);
text.addActionListener(this);
this.actionPerformed(null);
this.setVisible(true);
trea.setFont(font);
trea.setBackground(Color.cyan);
trea.setForeground(Color.red);
trea.setColumns(33);
trea.setRows(20);
}
public void actionPerformed(ActionEvent e)
{
try
{
int years=Integer.parseInt(text.getText());
year=years;
Calendar c=Calendar.getInstance();
c.clear();
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH,month);
c.set(Calendar.DATE,date);
int weeks=c.get(Calendar.DAY_OF_WEEK);
week=weeks;
//trea.append(year+"年日历"+"\n");//在JTextArea中显示日历
trea.setText(year+"年日历"+"\n");
//do
while(month<=11)
{
switch(month+1)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
max=31;
break;
case 2:
if(year%400==0||year%100!=0&&year%4==0)
max=29;
else
max=28;
break;
case 4 :
case 6 :
case 9 :
case 11:
max=30;
break;
}
trea.append("\n");
trea.append(month+1+"月份"+"\n");
trea.append(" 日 一 二 三 四 五 六"+"\n");
for(int i=1;i<week;i++)
{
trea.append(String.format("%4c",' '));
}
for(int day=1;day<=max;day++)
{
trea.append(String.format("%4d",day));
week++;
week%=7;
if (week==1)
trea.append("\n");
}
trea.append("\n");
month++;
}
//while(month<=11);
month=0;
}
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(this,"\""+text.getText()+"\"必须是整数,请重新输入");
}
finally{}
}
public static void main(String args[])
{
new MyCalendar();
}
}
- 大小: 42 KB
分享到:
相关推荐
java万年历课程设计,有源代码 源代码片段:public class wannianli extends JFrame implements ActionListener, MouseListener { private Calendar cld = Calendar.getInstance(); private String [] astr = {...
该实验报告是关于java 万年历的实验报告对写java方面实验报告的有帮助 里面详细介绍了实验的步骤
java万年历源代码
JAVA万年历设计报告
java万年历小程序,玩具
java 万年历的class源文件 简单实用
自己大二java写的一个万年历,包含原代码.
用Java实现的万年历源码,涉及到Java集合类、图形用于界面、多线程、异常等知识。
万年历 用java语言编写的 很清楚明白 通俗易懂
java万年历源程序 适合课程设计 非常不错
本程序采用Java,开发者应具有Java程序设计语言,Swing基本GUI组件,多线程,文件使用的基本知识和技能,程序初始显示出当前的日期,而后根据用户的选择来跳转至用户想要查看的年份与月份。
一个java万年历小程序.
1.2功能设计 本程序要构建的万年历程序,其功能有以下几个方面: (1)通过网页形式运行,实现图形界面。 (2)能以月历形式显示日期与星期。 (3)支持用户自己输入年份,并提供月份的下拉形式菜单来选择月份。 (4)...
万年历 是个不错的Java程序,适合初学者学习
Java万年历