JS中写日历时候遇见的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS中写日历时候遇见的问题相关的知识,希望对你有一定的参考价值。
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT>
var leap;
var month;
var year;
var day;
var week;
var today=new Date();
var a=today.getMonth()+1;
function getday()//得到当前日期
document.write(today.getYear(),"年",today.getMonth()+1,"月",today.getDate(),"日");
function Calendar()
//得到输入的年和月
year=document.all.pyear.value; month=document.yearmonthform.Selectmonth.options[document.yearmonthform.Selectmonth.selectedIndex].value;
//判断闰年
if(year%4==0&&year%100!=0||year%400==0)
leap=1;
else
leap=0;
//判断每个月有多少天
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
day=31;
else
if(month==4||month==6||month==9||month==11)
day=30;
else
if(leap==1&&month==2)
day=29;
else day=28;
//按照日历格式输出每个月的月历表
if(month==1||month==2)
year=year-1;
if(month==1)
month=13;
else
month=14;
else month;
var y=year%100;
var z=parseInt(y/4);//强制转换为整形
var century=parseInt(year/100);
var b=parseInt(century/4);
var m=month;
var a=parseInt(26*(parseInt(m)+1)/10);
var firstday=1;//求这个月的第一天为星期几
week=((y+z)+b-2*century+a+firstday-1)%7;
if(week<0) //出现的负的星期时候对7取模即可得到正的星期几
week=week+7;
else week;
if(month==13||month==14)
year=year+1;
if(month==13)
month=1;
else month=2;
else month;
//alert(day);
//alert("y="+y+"z="+z+"century="+century+"b="+b+"m="+m+"a="+a+"firstday="+firstday);
//alert(year+"年"+month+"月有"+day+"天"+"这个月的第一天是星期"+w);
//PrintCalendar print=new PrintCalendar();
//print();
//alert(year+"年"+month+"月有"+day+"天"+"这个月的第一天是星期"+week);
var myMonth=new Array();
for(var i=0,j=1;i<=42;i++)
if(i>=week&&j<=day)
myMonth[i]=j,j++;
else myMonth[i]="";
this.printDate=function()
document.write("<table border=4 height=50 width=400 bgColor=#99FFFF borderColor=#FF3399><tr><td>星期日</td><td>星期一</td><td>星期二</td><td>星期三</td><td>星期四</td><td>星期五</td><td>星期六</td></tr>");
for(var i=0,n=0;i<6;i++)
document.write("<tr>");
for(var j=0; j<7 ;j++)
document.write("<td>");
document.write(myMonth[n]),n++;
document.write("</td>");
document.write("</tr>")
document.write("</table>");
function Day()
var c=new Calendar();
c.printDate();
alert(year+"年"+month+"月有"+day+"天"+"这个月的第一天是星期"+week);
</SCRIPT>
</HEAD>
JS程序如上所示,有详细的注释,下面body里面是调用,字数限制无法在写出来
这是我自己写了一个日历,一点击一个空白框的话可以显示出来一个表格table,里面可以选择一个日期,并点击查询按钮进行日期的查询.
问题出来了,现在关键是当在点击该查询按钮后就是第二页显示出来结果,但是我想让它在点击查询按钮后当前页显示出结果,问怎么办?
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class MyCalendar extends JApplet
private static final long serialVersionUID = 4398452463891616204L;
public static final String WEEK_SUN = "SUN";
public static final String WEEK_MON = "MON";
public static final String WEEK_TUE = "TUE";
public static final String WEEK_WED = "WED";
public static final String WEEK_THU = "THU";
public static final String WEEK_FRI = "FRI";
public static final String WEEK_SAT = "SAT";
public static final Color background = Color.white;
public static final Color foreground = Color.black;
public static final Color headerBackground = Color.blue;
public static final Color headerForeground = Color.white;
public static final Color selectedBackground = Color.blue;
public static final Color selectedForeground = Color.white;
private JPanel cPane;
private JLabel yearsLabel;
private JSpinner yearsSpinner;
private JLabel monthsLabel;
private JComboBox monthsComboBox;
private JTable daysTable;
private AbstractTableModel daysModel;
private Calendar calendar;
public MyCalendar()
cPane = (JPanel) getContentPane();
public void init()
cPane.setLayout(new BorderLayout());
calendar = Calendar.getInstance();
//calendar = Calendar.getInstance();
yearsLabel = new JLabel("Year: ");
yearsSpinner = new JSpinner();
yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));
yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
yearsSpinner.addChangeListener(new ChangeListener()
public void stateChanged(ChangeEvent changeEvent)
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue()).intValue());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
updateView();
);
JPanel yearMonthPanel = new JPanel();
cPane.add(yearMonthPanel, BorderLayout.NORTH);
yearMonthPanel.setLayout(new BorderLayout());
yearMonthPanel.add(new JPanel(), BorderLayout.CENTER);
JPanel yearPanel = new JPanel();
yearMonthPanel.add(yearPanel, BorderLayout.WEST);
yearPanel.setLayout(new BorderLayout());
yearPanel.add(yearsLabel, BorderLayout.WEST);
yearPanel.add(yearsSpinner, BorderLayout.CENTER);
monthsLabel = new JLabel("Month: ");
monthsComboBox = new JComboBox();
for (int i = 1; i <= 12; i++)
monthsComboBox.addItem(new Integer(i));
monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
monthsComboBox.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent actionEvent)
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
updateView();
);
JPanel monthPanel = new JPanel();
yearMonthPanel.add(monthPanel, BorderLayout.EAST);
monthPanel.setLayout(new BorderLayout());
monthPanel.add(monthsLabel, BorderLayout.WEST);
monthPanel.add(monthsComboBox, BorderLayout.CENTER);
daysModel = new AbstractTableModel()
private static final long serialVersionUID = -5996965100536784508L;
public int getRowCount()
return 7;
public int getColumnCount()
return 7;
public Object getValueAt(int row, int column)
if (row == 0)
return getHeader(column);
row--;
Calendar calendar = (Calendar) MyCalendar.this.calendar.clone();
calendar.set(Calendar.DAY_OF_MONTH, 1);
int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int index = row * 7 + column;
int dayIndex = index - moreDayCount + 1;
if (index < moreDayCount || dayIndex > dayCount)
return null;
else
return new Integer(dayIndex);
;
daysTable = new CalendarTable(daysModel, calendar);
daysTable.setCellSelectionEnabled(true);
daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer()
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
String text = (value == null) ? "" : value.toString();
JLabel cell = new JLabel(text);
cell.setOpaque(true);
if (row == 0)
cell.setForeground(headerForeground);
cell.setBackground(headerBackground);
else
if (isSelected)
cell.setForeground(selectedForeground);
cell.setBackground(selectedBackground);
else
cell.setForeground(foreground);
cell.setBackground(background);
return cell;
);
updateView();
cPane.add(daysTable, BorderLayout.CENTER);
public static String getHeader(int index)
switch (index)
case 0:
return WEEK_SUN;
case 1:
return WEEK_MON;
case 2:
return WEEK_TUE;
case 3:
return WEEK_WED;
case 4:
return WEEK_THU;
case 5:
return WEEK_FRI;
case 6:
return WEEK_SAT;
default:
return null;
public void updateView()
daysModel.fireTableDataChanged();
daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
calendar.get(Calendar.WEEK_OF_MONTH));
daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK) - 1,
calendar.get(Calendar.DAY_OF_WEEK) - 1);
public static class CalendarTable extends JTable
private static final long serialVersionUID = 2808554667609229840L;
private Calendar calendar;
public CalendarTable(TableModel model, Calendar calendar)
super(model);
this.calendar = calendar;
public void changeSelection(int row, int column, boolean toggle, boolean extend)
super.changeSelection(row, column, toggle, extend);
if (row == 0)
return;
Object obj = getValueAt(row, column);
if (obj != null)
calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());
@SuppressWarnings("deprecation")
public static void main(String[] args)
JFrame frame = new JFrame("Calendar Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCalendar myCalendar = new MyCalendar();
myCalendar.init();
frame.getContentPane().add(myCalendar);
frame.setSize(240, 172);
frame.show();
参考技术A 网上这些很多,自己去找一个,对比吧
以上是关于JS中写日历时候遇见的问题的主要内容,如果未能解决你的问题,请参考以下文章