实验一 Java常用工具类编程 下载本文

//计算当前月的1号为星期几

cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号 int firstweek=cal.get(Calendar.DAY_OF_WEEK);

。。。。。 }

package job1;

import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar;

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

public class CalendarTable extends JFrame implements ActionListener{ private JLabel label_year=new JLabel(\年\private JLabel label_month=new JLabel(\月\private JComboBox box_year=new JComboBox(); private JComboBox box_month=new JComboBox(); private JButton btn=new JButton(\确定\private JTable table;

private String week[]={\日\一\二\三\四\五\六\private String rows[][]=new String[6][7];

public CalendarTable(){ }

public void actionPerformed(ActionEvent e){

Calendar cal=Calendar.getInstance();

int year=cal.get(Calendar.YEAR);//box_year.getSelectedIndex(); int month=box_month.getSelectedIndex(); showCalendar(year,month);

table.setModel(new DefaultTableModel(rows,week)); JPanel jp_north=new JPanel(new GridLayout(1,5)); for(int i=1;i<13;i++)

box_month.addItem(i);

jp_north.add(box_year); jp_north.add(label_year); jp_north.add(box_month); jp_north.add(label_month); jp_north.add(btn);

table=new JTable(rows,week);

JScrollPane scroll=new JScrollPane(table); this.add(jp_north,BorderLayout.NORTH); this.add(scroll); calendarinit();

btn.addActionListener(this); box_year.addActionListener(this); box_month.addActionListener(this);

}

private void calendarinit(){ }

private void showCalendar(int year,int month){

Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month);

int days=cal.getActualMaximum(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH, 1);

int firstweek=cal.get(Calendar.DAY_OF_WEEK); for(int i=0;i

for(int i=1;i

if(i%7==0){

for(int j=0;j

rows[i][j]=\

Calendar cal=Calendar.getInstance();

box_year.addItem(new Integer(cal.get(Calendar.YEAR))); cal.set(Calendar.DAY_OF_MONTH, 1);

box_month.setSelectedIndex(cal.get(Calendar.MONTH)); showCalendar(Calendar.YEAR,Calendar.MONTH);

}

}

}

line++;

if(firstweek==1){ }

if(i>=firstweek-1){ }

rows[line][i%7]=(i-firstweek+2)+\rows[0][0]=\

public static void main(String[] args){ } }

5日期类的构造 定义一个日期类,属性有年、月、日;以及对应这些属性的构造函数以及set-get访问器方法。其它功能包括:

1)、按照中国日期输出格式,编写toString()函数。即形如2010年12月10日.

public String toString (){

… } 2)、定义日期相加的函数,可以加年,月,日。

public void addDate(int part,int nums){

CalendarTable cal=new CalendarTable(); cal.setBounds(40, 40, 400, 200); cal.setVisible(true);

cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);