我回到了我的工资单计算器,我正在尝试创建一个从JTextField获取值的动作侦听器。当我调试它时,我无法弄清楚为什么没有传递这些值。非常感谢任何帮助。
package week4Calc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author bsmith
*/
public class PayrollDisplay extends JFrame {
public static JTextField hoursField, rateField;
public PayrollDisplay() {
JFrame frame = new JFrame ();
Panel panel1 = new Panel();
Panel panel2 = new Panel();
ExitButton exit = new ExitButton ();
JButton calc = new JButton ("Calculate");
calc.addActionListener(new calcEvent());
JLabel nameLbl = new JLabel ("Name:");
JTextField nameField = new JTextField (10);
JLabel deptLbl = new JLabel ("Department:");
EmpDeptBox box1 = new EmpDeptBox ();
JLabel hoursLbl = new JLabel ("Hours:");
hoursField = new JTextField (3);
JLabel rateLbl = new JLabel ("Rate:");
rateField = new JTextField (7);
panel1.add(nameLbl);
panel1.add(nameField);
panel1.add(deptLbl);
panel1.add(box1);
panel1.add(hoursLbl);
panel1.add(hoursField);
panel1.add(rateLbl);
panel1.add(rateField);
panel2.add(exit);
panel2.add(calc);
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(750,200);
frame.setVisible(true);
}
public class calcEvent implements ActionListener
{
@Override
public void actionPerformed(ActionEvent evt)
{
float payCheck,hoursWrkd,payRate;
hoursWrkd = Float.parseFloat(hoursField.toString());
payRate = Float.parseFloat(rateField.toString());
payCheck = hoursWrkd*payRate;
JOptionPane.showMessageDialog(null, "The paycheck should be: $"+payCheck);
}
}
}