最近一直在努力补因为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); } }
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是监视器 } }
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"); } } }这是运行结果: