rendered paste body//This file was creatd on the 04/11/2K+6 at 14:20 by Christopher Beale for Year 1 Computer Science Coursework//Its purpose is to define the CMillitaryTime class and implement itimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class CMillitaryTime implements ActionListener { JFrame mTFrame; JPanel mTPanelInput, mTPanelOutput; //split out program in half, one for input and one for output JTextField mTTimeIn; JLabel mTResult, mTPicture; JComboBox mTAmPm; JButton mTConvert; String[] amPm = { "AM", "PM" }; final int NONE = 0; final int AM = 1; final int PM = 2; public CMillitaryTime() { mTFrame = new JFrame("Millitary Time Converter"); mTFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mTFrame.setSize(400,400); mTPanelInput = new JPanel(new GridLayout(1,2)); mTPanelOutput = new JPanel(new GridLayout(2,2)); addComponents(); mTFrame.getContentPane().add(mTPanelInput, BorderLayout.NORTH); mTFrame.getContentPane().add(mTPanelOutput, BorderLayout.SOUTH); mTFrame.pack(); mTFrame.setVisible(true); setPicture(NONE); } private void addComponents() { mTTimeIn = new JTextField(8); mTTimeIn.addActionListener(this); mTAmPm = new JComboBox(amPm); mTAmPm.addActionListener(this); mTPanelInput.add(mTTimeIn); mTPanelInput.add(mTAmPm); mTConvert = new JButton("Convert"); mTConvert.addActionListener(this); mTResult = new JLabel("00:00:00"); mTPicture = new JLabel(); mTPanelOutput.add(mTConvert); mTPanelOutput.add(mTResult); mTPanelOutput.add(mTPicture); } public void actionPerformed(ActionEvent e) { switch(mTAmPm.getSelectedIndex()){ default: case 0: setAM(); break; case 1: setPM(); break; } } private void setAM() { if(validTime(mTTimeIn.getText())) { mTResult.setText(mTTimeIn.getText()); setPicture(AM); } } private void setPM() { if(validTime(mTTimeIn.getText())) { String[] splitTime = mTTimeIn.getText().split(":"); int splitTimeTemp = Integer.parseInt(splitTime[0]) + 12; mTResult.setText(Integer.toString(splitTimeTemp) + ":" + splitTime[1] + ":" + splitTime[2]); setPicture(PM); } } private boolean validTime(String time) { if(!validStringTime(time)) { mTResult.setText("Invalid"); setPicture(NONE); return false; } return true; } private boolean validStringTime(String time){ String[] splitTime = time.split(":"); if(splitTime.length != 3 || Double.parseDouble(splitTime[0]) > 11.00 || Double.parseDouble(splitTime[1]) > 60.00 || Double.parseDouble(splitTime[2]) > 60.00) { return false; } return true; } private void setPicture(int image) { ImageIcon icon; switch(image) { default: case NONE: icon = createImageIcon("none.gif"); break; case AM: icon = createImageIcon("am.gif"); break; case PM: icon = createImageIcon("pm.gif"); break; } if (icon != null) { mTPicture.setText(null); mTPicture.setIcon(icon); } else { mTPicture.setText("Image broken"); } } private ImageIcon createImageIcon(String path) { java.net.URL imgURL = CMillitaryTime.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } private static void createAndShowGUI(){ LookAndFeel lf = UIManager.getLookAndFeel(); // attempt to set the windows look and feel. // if it doesnt work, no matter, we dont do anything with the exceptions // anyway, and it will stick with the default java look and feel try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (InstantiationException e) { } catch (ClassNotFoundException e) { } catch (UnsupportedLookAndFeelException e) { } catch (IllegalAccessException e) { } CMillitaryTime gui = new CMillitaryTime(); //create a new object of ourselves, which runs all the gui code } public static void main(String[] args){ //use the swing invokeLater function to start a new thread to display the gui //much neater and safer javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }