用Java写日历(万年历)程序要用到一个类文件,里面有很多

用Java写日历(万年历)程序要用到一个类文件,里面有很多
自定义方法以及对时间的操作是值得借鉴的。
代码作者:张波。
在本代码中实现了实例化文本组件并定位、点击今天按钮后显
示这个月的日历、选择年份后显示的日历、窗口打开时就显示
今天的日历等功能,还有更多功能请参考以下代码吧:

view sourceprint001 package calendars; 

002 import java.awt.Color; 

003 import java.awt.event.ActionEvent; 

004 import java.awt.event.ActionListener; 

005 import java.awt.event.WindowAdapter; 

006 import java.awt.event.WindowEvent; 

007 import java.text.ParseException; 

008 import javax.swing.JButton; 

009 import javax.swing.JComboBox; 

010 import javax.swing.JFrame; 

011 import javax.swing.JTextField; 

012 public class Inter { 

013     private JTextField[] textField = new JTextField[49]; 

014     private ShowDate showdate=new ShowDate(); 

015     private JComboBox yearBox = new JComboBox(); 

016     private JComboBox monthBox = new JComboBox(); 

017     private Clean clean = new Clean(); 

018     public void homepag() throws ParseException{ 

019         窗口名字大小 

020         JFrame frame = new JFrame(万年历                 制作者:fan); 

021         frame.setResizable(false); 

022         frame.setLayout(null); 

023         frame.setBackground(Color.RED); 

024         frame.setSize(310, 430); 

025         年月的下拉菜单定位 

026         yearBox.setBounds(20, 20, 60, 20); 

027         monthBox.setBounds(90, 20, 40, 20); 

028         for(int i = 1900;i  2051;i++){ 

029             yearBox.addItem(i); 

030         } 

031         for(int i=1;i13;i++){ 

032             monthBox.addItem(i); 

033         } 

034         显示日期的窗口 

035         实例化文本组件并定位 

036             for(int i = 0;i  textField.length;i++){ 

037                 textField[i]=new JTextField(); 

038                 textField[i].setHorizontalAlignment(JTextField.CENTER); 

039                 textField[i].setEditable(false); 

040                 int j=i7+1; 

041                 int m=i-(i7)7; 

042                 textField[i].setBounds(10+40m, 40+40j, 40, 40); 

043             } 

044         星期一到星期日 

045         textField[0].setText(一); 

046         textField[1].setText(二); 

047         textField[2].setText(三); 

048         textField[3].setText(四); 

049         textField[4].setText(五); 

050         textField[5].setText(六); 

051         textField[6].setText(日); 

052         显示当前日期的按钮 

053         JButton todayButton = new JButton(今天); 

054         todayButton.setBounds(200, 20, 60, 20); 

055         点击今天按钮后显示这个月的日历 

056         todayButton.addActionListener(new ActionListener(){ 

057             public void actionPerformed(ActionEvent e) { 

058                 clean.clean(textField); 

059                 try { 

060                     showdate.showToday(textField,yearBox,monthBox); 

061                 } catch (ParseException e1) { 

062                     e1.printStackTrace(); 

063                 } 

064             } 

065               

066         }); 

067         选择年份后显示的日历 

068         yearBox.addActionListener(new ActionListener() { 

069             public void actionPerformed(ActionEvent e) { 

070                 clean.clean(textField); 

071                 try { 

072                     showdate.showOtherDay(textField, yearBox, monthBox); 

073                 } catch (ParseException e1) { 

074                     e1.printStackTrace(); 

075                 } 

076             } 

077         }); 

078         选择月份后显示的日历 

079         monthBox.addActionListener(new ActionListener() { 

080             public void actionPerformed(ActionEvent e) { 

081                 clean.clean(textField); 

082                 try { 

083                     showdate.showOtherDay(textField, yearBox, monthBox); 

084                 } catch (ParseException e1) { 

085                     e1.printStackTrace(); 

086                 } 

087             } 

088         }); 

089         frame.addWindowListener(new WindowAdapter() { 

090             public void windowClosing(WindowEvent arg0){ 

091                 System.exit(1); 

092             } 

093         }); 

094         窗口打开时就显示今天的日历 

095         showdate.showToday(textField,yearBox,monthBox); 

096         将组件加入窗口 

097         for(int i = 0;i  textField.length;i++){ 

098             frame.add(textField[i]); 

099         } 

100         frame.add(todayButton); 

101         frame.add(yearBox); 

102         frame.add(monthBox); 

103         frame.setVisible(true); 

 

你可能感兴趣的:(java研究)