天天看點

java萬年曆

最近一直在努力補因為ACM而耽誤的專業課,o(︶︿︶)o 唉 其實我也很喜歡C語言的。

可惜。。隻能一步一步來。

Mycalender:

import java.awt.*;
public class MycalCalender {
  public static void main(String[] args) {
    WindowActionEvent win=new WindowActionEvent();
    ReadListen listener=new ReadListen();//建立螢幕(listener可以是任意的)
    Container con=win.getContentPane();
    con.setBackground(Color.green);
    win.setMycommandListener(listener);//視窗組合螢幕.
    win.setTitle("——我的萬年曆———");
    win.setBounds(100,100,610,560);
  }
}      

WindowActionEvent:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class WindowActionEvent extends JFrame{
  JTextField textInput;
  JTextArea textShow;
  JButton  button;
  public WindowActionEvent(){
    init();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  void init(){//建構視窗
    setLayout(new FlowLayout());
    add(new JLabel("請輸入你要查詢的年份(1900年以後):"));
    textInput=new JTextField(15);
    add(textInput);
    button=new JButton("确定");
    add(button);
    add(new JLabel("             這一年的月曆為:"));
    textShow=new JTextArea(25,51);
    add(new JScrollPane(textShow));
  }
  void setMycommandListener(ReadListen listener){
      listener.setJTextField(textInput);
      listener.setJTextArea(textShow);
      button.addActionListener(listener);//button是事件源,listener是螢幕
      textInput.addActionListener(listener);//textInput是事件源,listener是螢幕
    }
}      

ReadListen:

import java.awt.event.*;
import javax.swing .*;
public class ReadListen implements ActionListener{
  JTextArea textShow;
  JTextField textInput;
  public void setJTextArea(JTextArea area){
    textShow=area;
  }
  public void setJTextField(JTextField text){
    textInput=text;
  }
  public void actionPerformed(ActionEvent e){
    double nowyears=Double.parseDouble(textInput.getText());
    print(nowyears);
    }
  void print(double nowyears){
      int day=0,mon[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
      for(int year=1990;year<nowyears;year++)
      {
        if(year%4==0&&year%100!=0||year%400==0)
          day+=366;
        else
          day+=365;
      }
      int week=(day+1)%7;
      textShow.setText("第1月:\n日\t一\t二\t三\t四\t五\t六\n");
      if(nowyears%4==0&&nowyears%100!=0||nowyears%400==0)
        mon[2]=29;
      else
        mon[2]=28;
      int monday=1;
      while(monday<13)
      {
        int t=1;
        if(week==7)
          week=0;
        for(int i=0;i<week;i++)
          textShow.append(" \t");
        while(t<=mon[monday])
        {
          if(week==7)
          {
            week=0;
            textShow.append("\n");
          }
          textShow.append(t+"\t");
          t++;week++;
        }
        textShow.append("\n");
        monday++;
        if(monday==13)
          break;
        textShow.append("第"+monday+"月:\n");
        textShow.append("日\t一\t二\t三\t四\t五\t六\n");
      }
  }
}