孤獨者 - 心在黑暗裡旅行 ...... ... 至尊會員
遊蕩的過客 - 枉入紅塵若許年 .. ... - UID
- 18273
- 帖子
- 12122
- 精華
- 1273
- 積分
- 240737
- 金幣
- 492641
- 威望
- 5001
- 推廣
- 0
- 閱讀權限
- 99
- 來自
- 尋覓中
- 註冊時間
- 2006-6-24
- 最後登錄
- 2015-5-27
|
1樓
大 中
小 發表於 2006-8-7 20:48
[JAVA] 最新的月曆
引用:import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class b7 extends JFrame implements ActionListener//畫圖物件是JFrame的擴充
{
Container c;
Color bgcolor=Color.white;
Color textcolor=Color.red;
JLabel lab1=new JLabel();
JButton rot0,rot1;
int year,month;
JTable dsp;
int[] temp_month={31,28,31,30,31,30,31,31,30,31,30,31,}; //月份的天數
String[] weekname = {"週日","週一","週二","週三","週四","週五","週六"};
public void gen_cal()
{
int temp_year,days,i,last;
int xpt,ypt;
int row=6;
//取得輸入
String syear = JOptionPane.showInputDialog("請輸入年");
String smonth = JOptionPane.showInputDialog("請輸入月");
year=Integer.parseInt(syear);
month=Integer.parseInt(smonth);
lab1.setText(year+" 年 "+month+" 月份日曆");
//---------判斷是否為閏年及計算總天數----------------------------
temp_year=year-1;
days=365*temp_year+temp_year/4-temp_year/100+temp_year/400;
if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 ==0)))// 判斷該年是否為閏年,若是閏年則將該年2月的日期改為29天
temp_month[1]=29;
else temp_month[1]=28;
for (i=0;i<(month-1);i++) //算總天數
days=days+temp_month;
last=((days % 7)+1) % 7 ; //算第一行空白的格數
if ((temp_month[month-1]+last-7)>28) row=6; //算算要幾列
else row=5;
Object[][] data =new String[row][7];
dsp=new JTable(data,weekname); //設定JTable
//for (i=0;i<7;i=i+1) //設定標題列
// dsp.setValueAt(weekname,0,i);
xpt=last;
ypt=0;
for (i=1;i<=temp_month[month-1];i=i+1)//排每一天
{
dsp.setValueAt(""+i,ypt,xpt);
xpt=xpt+1;
if (xpt%7 ==0 )
{
xpt=0;
ypt=ypt+1;
}
}
}
public b7() //建構元
{
super("月曆");
gen_cal();
c=getContentPane();
rot0=new JButton("離開");
setSize(600,300);
c.setLayout(new FlowLayout(FlowLayout.CENTER));
c.add(lab1);
c.add(dsp.getTableHeader());//排進標題
c.add(dsp);
c.add(rot0);
//安裝listener
rot0.addActionListener(this);
show();
}
public void paint(Graphics g) //真正的畫圖設定
{
super.paint(g);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==rot0)//結束
{
System.exit(0);
}
} //actionPerformed
public static void main(String args[]) //程式起點
{
b7 app=new b7(); //啟動物件
app.addWindowListener(new WindowAdapter(){ //匿名內部類別
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}); //處理視窗關閉要求
}
}
|