Today I will introduce to you how to use Java swing to develop a simple small calendar. Let’s look at the code below:
First create a CalendarBean class for basic date calculation:
package other1; import java.util.Calendar; public class CalendarBean { String day[]; int year=2005,month=0; public void setYear(int year) { this.year=year; } public int getYear() { return year; } public void setMonth(int month) { this.month=month; } public int getMonth() { return month; } public String[] getCalendar() { String a[]=new String[42]; Calendar date=Calendar.getInstance(); date.set(year,month-1,1); int week=date.get(Calendar.DAY_OF_WEEK)-1; int day=0; //Judge large month if(month==1||month==3||month==5||month==7 ||month==8||month==10||month==12) { day=31; } //Judge small month if(month==4||month==6||month==9||month==11) { day=30; } //Judge normal year and leap year if(month==2) { if(((year%4==0)&&(year%100!=0))||(year%400==0)) { day=29; } else { day=28; } } for(int i=week,n=1;i<week+day;i++) { a[i]=String.valueOf(n) ; n++; } return a; } } Then create a main interface class for the interface implementation:
package other1; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CalendarFrame extends JFrame implements ActionListener { JLabel labelDay[]=new JLabel[42]; JTextField text=new JTextField(10); JButton titleName[]=new JButton[7]; JButton button = new JButton(); String name[]={"Day","one","two","three", "Four","five","six"}; JButton nextMonth, previousMonth; int year=1996,month=1; //Date information displayed by the startup program CalendarBean calendar; JLabel showMessage=new JLabel("",JLabel.CENTER); JLabel lbl1 = new JLabel("Please enter year: "); JLabel lbl2=new JLabel(" "); public CalendarFrame() { setBackground(new Color(0, 128, 128)); JPanel pCenter=new JPanel(); pCenter.setBackground(new Color(0, 139, 139)); //Set the layout of pCenter to the GridLayout layout with 7 rows and 7 columns. pCenter.setLayout(new GridLayout(7,7)); //pCenter adds component titleName[i] for(int i=0;i<7;i++) { titleName[i]=new JButton(name[i]); pCenter.add(titleName[i]); } //pCenter adds component labelDay[i] for(int i=0;i<42;i++) { labelDay[i]=new JLabel("",JLabel.CENTER); pCenter.add(labelDay[i]); } text.addActionListener(this); calendar=new CalendarBean(); calendar.setYear(year); calendar.setMonth(month); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) { labelDay[i].setText(day[i]); } nextMonth=new JButton("Next month"); previousMonth=new JButton("Last month"); button=new JButton("Of course"); //Register listener nextMonth.addActionListener(this); previousMonth.addActionListener(this); button.addActionListener(this); JPanel pNorth=new JPanel(), pSouth=new JPanel(); pNorth.add(showMessage); pNorth.add(lbl2); pNorth.add(previousMonth); pNorth.add(nextMonth); pSouth.add(lbl1); pSouth.add(text); pSouth.add(button); showMessage.setText("Calendar: "+calendar.getYear()+"Year"+calendar.getMonth()+"month"); ScrollPane scrollPane=new ScrollPane(); scrollPane.add(pCenter); getContentPane().add(scrollPane,BorderLayout.CENTER);// Add scrollPane in the center area getContentPane().add(pNorth,BorderLayout.NORTH);// Add pNorth in the north area getContentPane().add(pSouth,BorderLayout.SOUTH);// Add pSouth in the south area. } public void actionPerformed(ActionEvent e) { if(e.getSource()==nextMonth) { month=month+1; if(month>12) month=1; calendar.setMonth(month); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) { labelDay[i].setText(day[i]); } } else if(e.getSource()==previousMonth) { month=month-1; if(month<1) month=12; calendar.setMonth(month); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) { labelDay[i].setText(day[i]); } } else if(e.getSource()==button) { month=month+1; if(month>12) month=1; calendar.setYear(Integer.parseInt(text.getText())); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) { labelDay[i].setText(day[i]); } } showMessage.setText("Calendar: "+calendar.getYear()+"Year"+calendar.getMonth()+"month"); } } Finally, use a class to call it:
package other1; import javax.swing.JFrame; import javax.swing.UIManager; public class CalendarMainClass { public static void main(String args[]) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows interface style}catch (Exception e) { e.printStackTrace(); } CalendarFrame frame=new CalendarFrame(); frame.setBounds(100,100,360,300); frame.setTitle("Calendar applet"); frame.setLocationRelativeTo(null);//The form is centered to display frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }The operation results are as follows:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.