扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
按照你的要求编写的Java swing 带界面的万年历代码如下
成都创新互联公司是一家集网站建设,枝江企业网站建设,枝江品牌网站建设,网站定制,枝江网站建设报价,网络营销,网络优化,枝江网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
//日历
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class CCI extends JFrame implements ActionListener{
JButton jb1=new JButton("");
JButton jb2=new JButton("");
JButton jb3=new JButton("");
JButton jb4=new JButton("");
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JPanel jp4=new JPanel();
JLabel jl1=new JLabel();
JLabel jl2=new JLabel();
JLabel[]jl=new JLabel[49];
String []week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
Calendar c=Calendar.getInstance();
int year,month,day;
int nowyear,nowmonth,nowday;
CCI(){
super("简单日历");
nowyear=c.get(Calendar.YEAR);
nowmonth=c.get(Calendar.MONTH)+1;
nowday=c.get(Calendar.DAY_OF_MONTH);
year=nowyear;
month=nowmonth;
day=nowday;
String s=year+"年"+month+"月";
jl1.setForeground(Color.RED);
jl1.setFont(new Font(null,Font.BOLD,20));
jl1.setText(s);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jp1.add(jb1);jp1.add(jb2);jp1.add(jl1);jp1.add(jb3);jp1.add(jb4);
jp2.setLayout(null);
createMonthPanel();
jp2.add(jp3);
jl2.setFont(new Font(null,Font.BOLD,20));
jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日");
jp4.add(jl2);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.CENTER);
add(jp4,BorderLayout.SOUTH);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb1){
year=year-1;
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb2){
if(month==1){
year=year-1;
month=12;
}else{
month=month-1;
}
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb3){
if(month==12){
year=year+1;
month=1;
}else{
month=month+1;
}
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb4){
year=year+1;
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
}
public static void main(String[] args) {
new CCI();
}
public int getMonthDays(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if ((year%4==0year%100!=0)||year%400==0) {
return 29;
} else {
return 28;
}
default:
return 30;
}
}
public void createMonthPanel(){
c.set(year, month-1, getMonthDays(year,month));
int weekOfMonth=c.get(Calendar.WEEK_OF_MONTH);
if(weekOfMonth==6){
jp3.setLayout(new GridLayout(7,7));
jp3.setBounds(50, 20, 420, 350);
}else{
jp3.setLayout(new GridLayout(6,7));
jp3.setBounds(50, 20, 420, 300);
}
jp3.setBorder(BorderFactory.createEtchedBorder());
for(int i=0;i7;i++){
jl[i]=new JLabel(week[i],JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
c.set(year, month-1, 1);
int emptyFirst=c.get(Calendar.DAY_OF_WEEK)-1;
int daysOfMonth=getMonthDays(year,month);
for(int i=6+emptyFirst;i=7;i--){
int intyear=year;
int intmonth=month;
if(intmonth==1){
intyear=intyear-1;
intmonth=12;
}else{
intmonth=intmonth-1;
}
int intdays=getMonthDays(intyear,intmonth);
jl[i]=new JLabel((intdays+7-i)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
for(int i=7+emptyFirst;idaysOfMonth+7+emptyFirst;i++){
jl[i]=new JLabel((i-7-emptyFirst+1)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
if((i+1)%7==0 || (i+1)%7==1){
jl[i].setForeground(Color.RED);
}else if((i-7-emptyFirst+1)==nowdaymonth==nowmonthyear==nowyear)
jl[i].setForeground(Color.BLUE);
else
jl[i].setForeground(Color.BLACK);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
if(weekOfMonth==6)
for(int i=48;i=daysOfMonth+emptyFirst+7;i--){
jl[i]=new JLabel((49-i)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
else
for(int i=41;i=daysOfMonth+emptyFirst+7;i--){
jl[i]=new JLabel((42-i)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class MyCalendar
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String reg = "^(\\d+)[^\\d]+((0?[1-9])|(1[012]))$";
while(true)
{
System.out.println("输入年月(年和月用非数字隔开:如2015.1)(什么都不输入直接退出)");
String line = scanner.nextLine().trim();
if("".equals(line))
{
scanner.close();
break;
}
if(!line.matches(reg))
{
continue;
}
int year = Integer.parseInt(line.replaceAll(reg, "$1"));
int month = Integer.parseInt(line.replaceAll(reg, "$2"));
System.out.println("日\t一\t二\t三\t四\t五\t六");
Calendar calendar = Calendar.getInstance();
// 这个月的1号是星期几
calendar.set(year, month - 1, 1);
int day = calendar.get(Calendar.DAY_OF_WEEK);
int start = Calendar.SUNDAY;
calendar.add(Calendar.DATE, -day + start);
while(start day)
{
System.out.print(calendar.get(Calendar.DATE) + "\t");
calendar.add(Calendar.DATE, 1);
start++;
}
calendar.set(year, month - 1, 1);
Date now = calendar.getTime();
calendar.set(year, month, 1);
Date next = calendar.getTime();
for(Date cur = now; cur.before(next);)
{
calendar.setTime(cur);
int x = calendar.get(Calendar.DATE);
String tmp = x 10 ? "0" + x : x + "";
System.out.print(tmp + "\t");
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
{
System.out.println();
}
calendar.add(Calendar.DATE, 1);
cur = calendar.getTime();
}
calendar.add(Calendar.DATE, -1);
int to = calendar.get(Calendar.DAY_OF_WEEK);
int end = Calendar.SATURDAY;
while(to end)
{
calendar.add(Calendar.DATE, 1);
int x = calendar.get(Calendar.DATE);
String tmp = x 10 ? "0" + x : x + "";
System.out.print(tmp + "\t");
to++;
}
System.out.println();
}
}
}
import java.io.*;
class putout{
public void putout(int f,int x,int y){
int i;
int a[]= new int[40];
System.out.println(" 日 一 二 三 四 五 六 "+" "+f+"月");
for (i=0;ix;i++)
{System.out.print(" "); }
for(i=x;ix+y;i++)
a[i]=i-x+1;
for(i=x;ix+y;i++)
{
if ((i%7==0)(i0))
System.out.print("\n");
if (a[i]10)
System.out.print(" "+a[i]);
else System.out.print(" "+a[i]);
}
System.out.println("\n");
}
}
class st{
public static void main(String args[])throws IOException{
putout p=new putout();
int year,mouth,y=1,t,i;
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.print("请输入一个年份:");
String s=in.readLine();
year=Integer.parseInt(s);
if((year%4==0 year%100!=0)||(year%400==0))
mouth=1;
else
mouth=0;
y=year;
for(i=1;iyear;i++)
{if((i%4==0 i%100!=0)||(i%400==0))
y++;}
y=y%7;
for(i=1;i13;i++){
switch(i){
case 1: {p.putout(1,y,31);y=(y+31)%7;break;}
case 2: {p.putout(2,y,28+mouth);y=(y+28+mouth)%7;break;}
case 3: {p.putout(3,y,31);y=(y+31)%7;break;}
case 4: {p.putout(4,y,30);y=(y+30)%7;break;}
case 5: {p.putout(5,y,31);y=(y+31)%7;break;}
case 6: {p.putout(6,y,30);y=(y+30)%7;break;}
case 7: {p.putout(7,y,31);y=(y+31)%7;break;}
case 8: {p.putout(8,y,31);y=(y+31)%7;break;}
case 9: {p.putout(9,y,30);y=(y+30)%7;break;}
case 10: {p.putout(10,y,31);y=(y+31)%7;break;}
case 11: {p.putout(11,y,30);y=(y+30)%7;break;}
case 12: {p.putout(12,y,31);y=(y+31)%7;break;}
}
}
}
}
import java.text.SimpleDateFormat; import java.util.Calendar; public class TestDate { public static final String[] weeks = { "日", "一", "二", "三", "四", "五", "六" }; public static void main(String[] args) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR,2011);//2011年 c.set(Calendar.MONTH,0);//java中Calendar类,月从0开始, 0代表一月 c.set(Calendar.DATE,1);//1号 int day = c.get(Calendar.DAY_OF_WEEK);//获致是本周的第几天地, 1代表星期天...7代表星期六 System.out.println(new SimpleDateFormat( "yyyy-MM-dd ").format(c.getTime())); System.out.println("星期" + weeks[day-1]); } } 把以上测试代码写作一个方法 方法的参数名为年月日, 即可。当然Calendar 还有很多功能,比如一周的第几天,一年的第几个月……
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流