1  import java.awt.*;
  2  import java.awt.event.*;
  3  import javax.swing.*;
  4  import javax.swing.border.*;
  5  import java.util.*;
  6  
  7  public class CalendarApp extends JApplet {
  8    // Create a CalendarPanel for showing calendars
  9    private CalendarPanel calendarPanel = new CalendarPanel();
 10  
 11    // Combo box for selecting available locales
 12    private JComboBox jcboLocale = new JComboBox();
 13  
 14    // Declare locales to store available locales
 15    private Locale locales[] = Calendar.getAvailableLocales();
 16  
 17    // Buttons Prior and Next for displaying prior and next month
 18    private JButton jbtPrior = new JButton("Prior");
 19    private JButton jbtNext = new JButton("Next");
 20  
 21    /** Initialize the applet */
 22    public void init() {
 23      // Panel jpLocale to hold the combo box for selecting locales
 24      JPanel jpLocale = new JPanel(new FlowLayout());
 25      jpLocale.setBorder(new TitledBorder("Choose a locale"));
 26      jpLocale.add(jcboLocale);
 27  
 28      // Initialize the combo box to add locale names
 29      for (int i = 0; i < locales.length; i++)
 30        jcboLocale.addItem(locales[i].getDisplayName());
 31  
 32      // Panel jpButtons to hold buttons
 33      JPanel jpButtons = new JPanel(new FlowLayout());
 34      jpButtons.add(jbtPrior);
 35      jpButtons.add(jbtNext);
 36  
 37      // Panel jpCalendar to hold calendarPanel and buttons
 38      JPanel jpCalendar = new JPanel(new BorderLayout());
 39      jpCalendar.add(calendarPanel, BorderLayout.CENTER);
 40      jpCalendar.add(jpButtons, BorderLayout.SOUTH);
 41  
 42      // Place jpCalendar and jpLocale to the applet
 43      add(jpCalendar, BorderLayout.CENTER);
 44      add(jpLocale, BorderLayout.SOUTH);
 45  
 46      // Register listeners
 47      jcboLocale.addActionListener(new ActionListener() {
 48        @Override
 49        public void actionPerformed(ActionEvent e) {
 50          if (e.getSource() == jcboLocale)
 51            calendarPanel.changeLocale(
 52              locales[jcboLocale.getSelectedIndex()]);
 53        }
 54      });
 55  
 56      jbtPrior.addActionListener(new ActionListener() {
 57        @Override
 58        public void actionPerformed(ActionEvent e) {
 59          int currentMonth = calendarPanel.getMonth();
 60          if (currentMonth == 0) // The previous month is 11 for Dec
 61            calendarPanel.setYear(calendarPanel.getYear() - 1);
 62            calendarPanel.setMonth((currentMonth - 1) % 12);
 63        }});
 64  
 65      jbtNext.addActionListener(new ActionListener() {
 66        @Override
 67        public void actionPerformed(ActionEvent e) {
 68          int currentMonth = calendarPanel.getMonth();
 69          if (currentMonth == 11) // The next month is 0 for Jan
 70            calendarPanel.setYear(calendarPanel.getYear() + 1);
 71  
 72          calendarPanel.setMonth((currentMonth + 1) % 12);
 73        }});
 74  
 75      calendarPanel.changeLocale(
 76        locales[jcboLocale.getSelectedIndex()]);
 77    }
 78  
 79    /** Main method */
 80    public static void main(String[] args) {
 81      // Create a frame
 82      JFrame frame = new JFrame("CalendarApp");
 83  
 84      // Create an instance of the applet
 85      CalendarApp applet = new CalendarApp();
 86  
 87      // Add the applet instance to the frame
 88      frame.getContentPane().add(applet, BorderLayout.CENTER);
 89  
 90      // Invoke init() and start()
 91      applet.init();
 92      applet.start();
 93  
 94      // Display the frame
 95      frame.pack();
 96      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 97      frame.setVisible(true);
 98    }
 99  }