?? datejframe.java
字號(hào):
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.border.*;
class DateJFrame extends JFrame{
JPanel panel=new JPanel(new BorderLayout());
JPanel panel1=new JPanel();
JPanel panel2=new JPanel(new GridLayout(7,7));
JPanel panel3=new JPanel();
JLabel []label=new JLabel[49];
JLabel y_label=new JLabel("年份");
JLabel m_label=new JLabel("月份");
JComboBox com1=new JComboBox();
JComboBox com2=new JComboBox();
Color color=new Color(1,1,200,150); //定義一種顏色
JButton button=new JButton("查看");
int re_year,re_month;
int x_size,y_size;
String year_num;
Font font=new Font("宋體",Font.ITALIC,20);
Font font2=new Font("楷體",Font.PLAIN,18);
Calendar now=Calendar.getInstance(); //實(shí)例化Calendar
Calendar now2=Calendar.getInstance();
DateJFrame()
{
super("日歷-dreamboy");
setSize(300,350);
x_size=(int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y_size=(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x_size-300)/2,(y_size-350)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(y_label);
panel1.add(com1);
panel1.add(m_label);
panel1.add(com2);
panel1.add(button);
for(int i=0;i<49;i++)
{
label[i]=new JLabel("",JLabel.CENTER);//將顯示的字符設(shè)置為居中
label[i].setBorder(LineBorder.createBlackLineBorder());
panel2.add(label[i]);
}
panel3.add(new Clock(this));
panel.add(panel1,BorderLayout.NORTH);
panel.add(panel2,BorderLayout.CENTER);
panel.add(panel3,BorderLayout.SOUTH);
//panel.setBackground(Color.white);
panel1.setBackground(color);
panel2.setBackground(Color.white);
panel3.setBackground(Color.blue);
Init();
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int c_year,c_month,c_week;
c_year=Integer.parseInt(com1.getSelectedItem().toString()); //得到當(dāng)前所選年份
c_month=Integer.parseInt(com2.getSelectedItem().toString())-1; //得到當(dāng)前月份,并減1,計(jì)算機(jī)中的月為0-11
c_week=use(c_year,c_month); //調(diào)用函數(shù)use,得到星期幾
Resetday(c_week,c_year,c_month); //調(diào)用函數(shù)Resetday
}
}
);
setContentPane(panel);
setVisible(true);
setResizable(false);
}
public void setForeground()
{
for(int i=0;i<49;i=i+7)
{
label[i].setForeground(Color.red); //將星期日的日期設(shè)置為紅色
}
for(int i=6;i<49;i=i+7)
{
label[i].setForeground(Color.green);//將星期六的日期設(shè)置為綠色
}
}
public void Init()
{
int year,month_num,first_day_num;
String log[]={"日","一","二","三","四","五","六"}; //定義周期
for(int i=0;i<7;i++)
{
label[i].setText(log[i]);
} //
setForeground();
for(int i=1990;i<10000;i++)
{
com1.addItem(""+i);
}
for(int i=1;i<13;i++)
{
com2.addItem(""+i);
}
month_num=(int)(now.get(Calendar.MONTH)); //得到當(dāng)前時(shí)間的月份
year=(int)(now.get(Calendar.YEAR)); //得到當(dāng)前時(shí)間的年份
com1.setSelectedIndex(year-1990); //設(shè)置下拉列表顯示為當(dāng)前年
com2.setSelectedIndex(month_num); //設(shè)置下拉列表顯示為當(dāng)前月
first_day_num=use(year,month_num);
Resetday(first_day_num,year,month_num);
}
public int use(int reyear,int remonth)
{
int week_num;
now.set(reyear,remonth,1); //設(shè)置時(shí)間為所要查詢的年月的第一天
week_num= (int)(now.get(Calendar.DAY_OF_WEEK));//得到第一天的星期
return week_num;
}
public void Resetday(int week_log,int year_log,int month_log)
{
int month_score_log; //判斷是否是閏年的標(biāo)記
int month_day_score; //存儲(chǔ)月份的天數(shù)
int count;
month_score_log=0;
month_day_score=0;
count=1;
if(year_log%4==0&&year_log%100!=0||year_log%400==0) //判斷是否為閏年
{
month_score_log=1;
}
month_log=month_log+1; //將傳來的月份數(shù)加1
switch(month_log){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
month_day_score=31;
break;
case 4:
case 6:
case 9:
case 11:
month_day_score=30;
break;
case 2:
if(month_score_log==1)
{
month_day_score=29;
}
else{
month_day_score=28;
}
break;
}
for(int i=7;i<49;i++) //初始化標(biāo)簽
{
label[i].setText("");
}
week_log=week_log+6; //將星期數(shù)加6,使顯示正確
month_day_score=month_day_score+week_log;
for(int i=week_log;i<month_day_score;i++,count++)
{
if(count==((int)(now2.get(Calendar.DATE)))&&year_log==((int)(now2.get(Calendar.YEAR)))&&(month_log-1)==((int)(now2.get(Calendar.MONTH))))
{
label[i].setBorder(new LineBorder(Color.red,10));
label[i].setFont(font);
label[i].setForeground(color);
}
else{
label[i].setFont(font2);
label[i].setForeground(Color.black);
label[i].setBackground(Color.white);
label[i].setBorder(LineBorder.createBlackLineBorder());
setForeground();
}
label[i].setText(count+"");
}
}
public static void main(String [] args)
{
new DateJFrame();
}
}
class Clock extends Canvas implements Runnable{
DateJFrame mf;
Thread t;
String time;
Clock(DateJFrame mf){
this.mf=mf;
setSize(400,40);
setBackground(Color.white);
t=new Thread(this); //實(shí)例化線程
t.start(); //調(diào)用線程
}
public void run(){
while(true){
try{
t.sleep(1000); //休眠1秒鐘
}catch(InterruptedException e){
System.out.println("異常");
}
this.repaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋體",Font.BOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化時(shí)間顯示類型
Calendar now=Calendar.getInstance();
time="今天是:"+SDF.format(now.getTime()); //得到當(dāng)前日期和時(shí)間
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,70,25);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -