I wrote mine in java.. I also made a custom coded from scratch swing GUI and a JNLP file, so if you wanna learn any of that read through my code =).
Ageulator.javaCode:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Calendar;
// The main frame, all the gui elements go on here
class MainFrame extends JFrame implements ActionListener {
Calendar m_calendar = Calendar.getInstance();
JComboBox m_birthMonth = new JComboBox();
JComboBox m_birthDay = new JComboBox();
JComboBox m_birthYear = new JComboBox();
// Create and show the main frame
public MainFrame() {
// Close the application when we exit
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
this.setTitle("Ageulator");
// Create all the components on the main frame
this.createGUI();
// Remove any extra space between components
this.pack();
// Start application in the center of the screen
this.setLocationRelativeTo(null);
this.setVisible(true);
}
// Create all the components and put them on the main frame
private void createGUI() {
// Simple GUI layout manager
this.setLayout(new BorderLayout());
m_birthMonth.addItem("--");
// Allow the user to select months 1-12
for (int i = 1; i <= 12; i++) {
if (i < 10) {
m_birthMonth.addItem("0" + i);
} else {
m_birthMonth.addItem(i);
}
}
this.add(m_birthMonth, BorderLayout.WEST);
m_birthDay.addItem("--");
// Allow the user to select days 1-32
for (int i = 1; i <= 32; i++) {
if (i < 10) {
m_birthDay.addItem("0" + i);
} else {
m_birthDay.addItem(i);
}
}
this.add(m_birthDay, BorderLayout.CENTER);
m_birthYear.addItem("----");
// Allow the user to select years 1900-present
for (int i = m_calendar.get(Calendar.YEAR); i >= 1900; i--) {
m_birthYear.addItem(i);
}
this.add(m_birthYear, BorderLayout.EAST);
// Add a button and make it execute actionperformed when clicked
JButton calculateAge = new JButton("Calculate my age");
calculateAge.addActionListener(this);
this.add(calculateAge, BorderLayout.PAGE_END);
}
// Called when the calculate button is pushed
public void actionPerformed(ActionEvent e) {
int selDay;
int selMonth;
int selYear;
try {
selDay = Integer.parseInt(m_birthDay.getSelectedItem().toString());
selMonth = Integer.parseInt(m_birthMonth.getSelectedItem().toString());
selYear = Integer.parseInt(m_birthYear.getSelectedItem().toString());
// Sometimes the age is just current year - birth year
int age = m_calendar.get(Calendar.YEAR) - selYear;
// If currentmonth < birthmonth
if (m_calendar.get(Calendar.MONTH) + 1 < selMonth) {
age--;
}
// If currentmonth = birthmonth and currentday < birthday
if (m_calendar.get(Calendar.MONTH) + 1 == selMonth) {
if (m_calendar.get(Calendar.DAY_OF_MONTH) < selDay) {
age--;
}
}
// Display your age
JOptionPane.showMessageDialog(this,
"You are " + age + " years old!",
"Your age",
JOptionPane.INFORMATION_MESSAGE);
} catch (java.lang.Exception ex) {
// An error occured parsing (you didn't select a number)
JOptionPane.showMessageDialog(this,
"Please enter a valid age!",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
public class Ageulator {
private static void createAndShowGUI() {
MainFrame mainFrame = new MainFrame();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Agulator.jnlpCode:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp codebase="http://www.raymondcox.net/" spec="1.0+" href="Ageulator.jnlp">
<information>
<title>Agulator</title>
<vendor>Raymond Cox</vendor>
<homepage href="http://www.raymondcox.net/"/>
<description>A simple age calculator tool</description>
<description kind="short">Simple Age Calculator</description>
<offline-allowed/>
</information>
<resources>
<j2se version="1.5+"/>
<jar eager="true" href="Ageulator.jar" main="true"/>
</resources>
<application-desc main-class="Ageulator">
</application-desc>
</jnlp>
The code doesn't contain any 'real' error handling, but some basic stuff is in place.
Executables
Download the JNLPDownload the JAREDIT: the gui looks kinda ugly on windows.. I'm going to work on fixing that later.