19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<br />

<br />

<br />

<br />

<br />

<br />

<br />

1 import java.awt.*;<br />

2 import java.awt.event.*;<br />

3 import javax.swing.*;<br />

4 import javax.swing.border.*;<br />

5 import java.util.*;<br />

6<br />

7 public class CalendarApp extends JApplet {<br />

8 // Create a CalendarPanel for showing calendars<br />

9 private CalendarPanel calendarPanel = new CalendarPanel();<br />

10<br />

11 // Combo box for selecting available locales<br />

12 private JComboBox jcboLocale = new JComboBox();<br />

13<br />

14 // Declare locales <strong>to</strong> s<strong>to</strong>re available locales<br />

15 private Locale locales[] = Calendar.getAvailableLocales();<br />

16<br />

17 // But<strong>to</strong>ns Prior and Next for displaying prior and next month<br />

18 private JBut<strong>to</strong>n jbtPrior = new JBut<strong>to</strong>n("Prior");<br />

19 private JBut<strong>to</strong>n jbtNext = new JBut<strong>to</strong>n("Next");<br />

20<br />

21 /** Initialize the applet */<br />

22 public void init() {<br />

23 // Panel jpLocale <strong>to</strong> hold the <strong>com</strong>bo box for selecting locales<br />

24 JPanel jpLocale = new JPanel(new FlowLayout());<br />

25 jpLocale.setBorder(new TitledBorder("Choose a locale"));<br />

26 jpLocale.add(jcboLocale);<br />

27<br />

28 // Initialize the <strong>com</strong>bo box <strong>to</strong> add locale names<br />

29 for (int i = 0; i < locales.length; i++)<br />

30 jcboLocale.addItem(locales[i].getDisplayName());<br />

31<br />

32 // Panel jpBut<strong>to</strong>ns <strong>to</strong> hold but<strong>to</strong>ns<br />

33 JPanel jpBut<strong>to</strong>ns = new JPanel(new FlowLayout());<br />

34 jpBut<strong>to</strong>ns.add(jbtPrior);<br />

35 jpBut<strong>to</strong>ns.add(jbtNext);<br />

36<br />

37 // Panel jpCalendar <strong>to</strong> hold calendarPanel and but<strong>to</strong>ns<br />

38 JPanel jpCalendar = new JPanel(new BorderLayout());<br />

39 jpCalendar.add(calendarPanel, BorderLayout.CENTER);<br />

40 jpCalendar.add(jpBut<strong>to</strong>ns, BorderLayout.SOUTH);<br />

41<br />

42 // Place jpCalendar and jpLocale <strong>to</strong> the applet<br />

43 add(jpCalendar, BorderLayout.CENTER);<br />

44 add(jpLocale, BorderLayout.SOUTH);<br />

45<br />

46 // Register listeners<br />

47 jcboLocale.addActionListener(new ActionListener() {<br />

48 @Override<br />

49 public void actionPerformed(ActionEvent e) {<br />

16

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!