?? 例13-6.txt
字號:
網頁文件如下:
<applet code=Example13_6.class width=330 height=240>
<Param name="year" value ="2008">
</applet>
【例13-6】
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Example13_6 extends
JApplet implements ActionListener{
JTable table;
Object a[][]=new Object[6][7];
Object name[]=
{"星期日","星期一","星期二","星期三", "星期四","星期五","星期六"};
JButton nextMonth,previousMonth;
int year=2008,month=1;
CalendarBean calendar;
JLabel showMessage=new JLabel("",JLabel.CENTER);
public void init(){
calendar=new CalendarBean();
String s=getParameter("year"); //從html得到"year"的值
try{ year=Integer.parseInt(s);
}
catch(Exception e){
year=2008;
}
calendar.setYear(year);
calendar.setMonth(month);
String day[]=calendar.getCalendar();
table=new JTable(a,name);
table.setRowSelectionAllowed(false);
setTable(day);
nextMonth=new JButton("下月");
previousMonth=new JButton("上月");
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
JPanel pNorth=new JPanel(),
pSouth=new JPanel();
pNorth.add(previousMonth);
pNorth.add(nextMonth);
pSouth.add(showMessage);
showMessage.setText("日歷:"+calendar.getYear()+"年"+ calendar.getMonth()+"月" );
Container con=getContentPane();
con.add(new JScrollPane(table),BorderLayout.CENTER);
con.add(pNorth,BorderLayout.NORTH);
con.add(pSouth,BorderLayout.SOUTH);
con.validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==nextMonth){
month=month+1;
if(month>12)
month=1;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
setTable(day);
table.repaint();
}
else if(e.getSource()==previousMonth){
month=month-1;
if(month<1)
month=12;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
setTable(day);
table.repaint();
}
showMessage.setText("日歷:"+calendar.getYear()+"年"+calendar.getMonth()+"月" );
}
public void setTable(String day[]){ //設置表格單元格中的數據
int n=0;
for(int i=0;i<6;i++){
for(int j=0;j<7;j++){
a[i][j]=day[n];
n++;
}
}
}
}
class CalendarBean{
String day[];
int year=2005,month=0;
public void setYear(int year){
this.year=year;
}
public int getYear(){
return year;
}
public void setMonth(int month){
this.month=month;
}
public int getMonth(){
return month;
}
public String[] getCalendar(){
String a[]=new String[42]; //存放號碼的一維數組
Calendar 日歷=Calendar.getInstance();
日歷.set(year,month-1,1);
int 星期幾=日歷.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
day=31;
if(month==4||month==6||month==9||month==11)
day=30;
if(month==2)
if(((year%4==0)&&(year%100!=0))||(year%400==0))
day=29;
else
day=28;
for(int i=星期幾,n=1;i<星期幾+day;i++){
a[i]=String.valueOf(n) ;
n++;
}
return a;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -