如何由年份和星期而得到当年所有的日期(月日)在线等,急!!!
请写出实现的方法
------解决方案--------------------参考:http://community.csdn.net/Expert/TopicView.asp?id=5383638
/**
* Calendar test 根据年份,打印该年日历
* @author
*
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LeapYearTest extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel result;
TextArea txa;
JTextField t1;
JButton btn;
int year;
boolean leap;
int weekDay;
public LeapYearTest() {
super( "Calendar Test ");
setSize(280, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(dimension.width / 2 - 140, dimension.height / 2 - 350);
JPanel panel = new JPanel();
result = new JLabel( "year ");
t1 = new JTextField(5);
t1.setToolTipText( "input the year > 0 ");
btn = new JButton( "generate ");
t1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char input = e.getKeyChar();
if(input == '\n ')
generate();
else if (!Character.isDigit(input) && input != '\b ') {
e.consume();
}else{
btn.setEnabled(true);
}
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
});
btn.setToolTipText( "Generate the year " + t1.getText() + " Calendar ");
btn.setEnabled(false);
panel.add(result);
panel.add(t1);
panel.add(btn);
txa = new TextArea( " ", 12, 36, TextArea.SCROLLBARS_VERTICAL_ONLY);
txa.setEditable(false);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(panel, "North ");
this.getContentPane().add(txa, "Center ");
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
generate();
}
private void generate()
{
System.out.println(t1.getText());
if(t1.getText() == null || t1.getText().equals( " ")){
return;
}
year = Integer.parseInt(t1.getText());
t1.setText( " ");
if (year < 1) {
txa.setText( "year canot be less than 1! ");
return;
}
else
txa.setText( " Calendar Year: " + year + "\n ");
weekDay = (int) firstDayOfYear(year);
for (int i = 1; i <= 12; i++) {
printMonth(i);
// System.out.println( "\n ");
txa.append( "\n ");
}
t1.setText( " ");
btn.setEnabled(false);
}
private void printMonth(int m) {
printHead(m);
int days = daysOfMonth(m);
Calendar calendar = new GregorianCalendar();
for (int i = 1; i <= days; i++) {
if (i < 10) {
// System.out.print( " " + i);