import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CalendarGUI extends JFrame implements ActionListener, ItemListener, WindowListener { private Calendar myCalendar; private JLabel lblMonth; private JLabel lblYear; private JComboBox comboMonth; private JTextField txtYear; private JPanel pnlData; private JButton btnCalc; private JTextArea txtAreaMonth; private String names [] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; /** * Answers a new GUI for use with the Calendar object * * @param Calendar cal The model calendar object * **/ public CalendarGUI(Calendar cal) { super("Calendar"); setSize(450, 275); setLocation(200,200); myCalendar = cal; myCalendar.setMonth("January"); lblMonth = new JLabel("Month:"); lblYear = new JLabel("Year:"); comboMonth = new JComboBox(names); txtYear = new JTextField(6); btnCalc = new JButton("Generate"); pnlData = new JPanel(); txtAreaMonth = new JTextArea(); txtAreaMonth.setFont(new Font("Courier", Font.PLAIN, 16)); comboMonth.addItemListener(this); pnlData.add(lblMonth); pnlData.add(comboMonth); pnlData.add(lblYear); pnlData.add(txtYear); pnlData.add(btnCalc); btnCalc.addActionListener(this); Container c = getContentPane(); c.add(pnlData, BorderLayout.NORTH); c.add(txtAreaMonth, BorderLayout.CENTER); addWindowListener(this); setVisible(true); } /** * Listen and handle action events * * @param ActionEvent e The event listened for * **/ public void actionPerformed(ActionEvent e) { if (e.getSource() == btnCalc) { int textfieldyear = Integer.parseInt(txtYear.getText()); myCalendar.setYear(textfieldyear); System.out.println(comboMonth.getSelectedItem()); txtAreaMonth.setText(myCalendar.monthToString()); } } /** * Listen and handle item event * * @param ItemEvent e item (selected from month combo ) **/ public void itemStateChanged(ItemEvent e) { myCalendar.setMonth((String)e.getItem()); } /** * Listen and handle window events * Exits when window is closing. * * @param WindowEvent e the window event * **/ public void windowClosing(WindowEvent e) { System.exit(0); } public void windowClosed(WindowEvent e) { }; public void windowActivated(WindowEvent e) { }; public void windowDeactivated(WindowEvent e) { }; public void windowOpened(WindowEvent e) { }; public void windowIconified(WindowEvent e) { }; public void windowDeiconified(WindowEvent e) { }; public static void main(String args[]) { CalendarGUI cg = new CalendarGUI(new Calendar()); } }