// // WageTool.java // // Created by Edward on 2/15/06 // import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.text.*; // for DecimalFormat public class WageTool extends JApplet implements ActionListener { int hrs; int min; float rate; float pay = 0; JTextField hours; JTextField minutes; JTextField hrlyrate; JButton compute; Integer temp; Float tempf; Font myFont = new Font("monospaced",0,14); Color purple = new Color(148, 0, 211); DecimalFormat Currency = new DecimalFormat("0.00"); public void init() { // set the default look and feel String laf = UIManager.getSystemLookAndFeelClassName(); try { UIManager.setLookAndFeel(laf); } catch (UnsupportedLookAndFeelException exc) { System.err.println ("Warning: UnsupportedLookAndFeel: " + laf); } catch (Exception exc) { System.err.println ("Error loading " + laf + ": " + exc); } // set up text fields and buttons Label hrlb = new Label("Hours:",Label.RIGHT); hours = new JTextField("0", 5); Label minlb = new Label("Minutes:",Label.RIGHT); minutes = new JTextField("0", 5); Label rtlb = new Label("Hourly Rate:",Label.RIGHT); hrlyrate = new JTextField("45.00", 5); compute = new JButton("Compute"); compute.addActionListener(this); JPanel inPane = new JPanel(); inPane.setLayout(new GridLayout(3,2)); inPane.add(hrlb); inPane.add(hours); inPane.add(minlb); inPane.add(minutes); inPane.add(rtlb); inPane.add(hrlyrate); JPanel butPane = new JPanel(); butPane.add(compute); JPanel pane = new JPanel(); pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); pane.add(Box.createRigidArea(new Dimension(0,23))); pane.add(inPane); pane.add(butPane); setContentPane(pane); } public void actionPerformed(ActionEvent e) { Object src = e.getSource(); if (src == compute) { temp = Integer.valueOf(hours.getText()); hrs = temp.intValue(); temp = Integer.valueOf(minutes.getText()); min = temp.intValue(); tempf = Float.valueOf(hrlyrate.getText()); rate = tempf.floatValue(); while (min > 59) { // convert minutes over 60 to hours hrs++; min -= 60; } temp = new Integer(hrs); hours.setText(temp.toString()); temp = new Integer(min); minutes.setText(temp.toString()); pay = ((float)min / 60 + hrs) * rate ; // compute pay repaint(); } } public void paint (Graphics g) { super.paint(g); g.setColor(purple); g.setFont(myFont); g.drawString("Your pay is: $"+Currency.format(pay), 48, 18); } }