Presentation is loading. Please wait.

Presentation is loading. Please wait.

PayCalculator. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PayCalculator extends JFrame implements ActionListener.

Similar presentations


Presentation on theme: "PayCalculator. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PayCalculator extends JFrame implements ActionListener."— Presentation transcript:

1 PayCalculator

2

3 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PayCalculator extends JFrame implements ActionListener { private JTextField wageRate; private JLabel wageRateTitle; private JTextField hours; private JLabel hoursTitle; private JLabel regularPay; private JLabel regularPayTitle; private JButton calculate;

4 PayCalculator public PayCalculator() { super("Pay Calculator"); setBounds(100, 200, 800, 600); getContentPane().setLayout(null); setVisible(true); wageRate = new JTextField(); wageRate.setBounds(20, 50, 200, 40); wageRate.setBackground(Color.white); add(wageRate); wageRateTitle = new JLabel("Hourly Pay"); wageRateTitle.setBounds(40, 90, 200, 40); wageRateTitle.setForeground(Color.black); add(wageRateTitle);

5 PayCalculator hours = new JTextField(); hours.setBounds(250, 50, 200, 40); hours.setBackground(Color.white); add(hours); hoursTitle = new JLabel("Hours Worked"); hoursTitle.setBounds(270, 90, 200, 40); hoursTitle.setForeground(Color.black); add(hoursTitle); calculate = new JButton("Calculate Pay"); calculate.setBounds(20, 300, 200, 40); calculate.addActionListener(this); add(calculate); repaint(); }

6 PayCalculator public void actionPerformed(ActionEvent e) { float wages; float pay; int hoursWorked; wages = Helper.getFloat(wageRate); hoursWorked = Helper.getInt(hours); pay = wages * hoursWorked; Helper.remove(this,regularPay); Helper.remove(this, regularPayTitle);

7 PayCalculator regularPay = new JLabel(Helper.makeFloatLabel(pay)); regularPay.setBounds(500, 50, 200, 40); regularPay.setForeground(Color.black); add(regularPay); regularPayTitle = new JLabel("Regular Pay"); regularPayTitle.setBounds(500, 90, 200, 40); regularPayTitle.setForeground(Color.black); add(regularPayTitle); repaint(); }

8 PayCalculator public static void main(String args[]) { new PayCalculator(); }

9 Helper import javax.swing.*; import java.text.*; public class Helper { public static int getInt(JTextField t) { return new Integer(t.getText()); } public static float getFloat(JTextField t) { return new Float(t.getText()); } public static String makeIntLabel(int i) { return Integer.toString(i); } public static String makeFloatLabel(float f) { DecimalFormat format = new DecimalFormat("#.00"); return format.format(f); } public static void remove(JFrame f, JLabel k) { if (k!= null) f.remove(k); }

10 PayCalculator

11


Download ppt "PayCalculator. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PayCalculator extends JFrame implements ActionListener."

Similar presentations


Ads by Google