Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JTextField的Java*运算符未定义,双精度_Java_Undefined_Operator Keyword - Fatal编程技术网

JTextField的Java*运算符未定义,双精度

JTextField的Java*运算符未定义,双精度,java,undefined,operator-keyword,Java,Undefined,Operator Keyword,我在项目的侦听器中得到错误“*运算符未定义为JTextField,Double”。这是一个JavaGUI项目,我们需要有两个文本字段,一个用于“小时”,另一个用于“速率”。我们还需要有一个列出员工类型的组合框和一个计算按钮。我在监听器下面的开关案例2中得到了错误(pay=hours*payRate;)。如何转换文本,以便将小时数乘以工资率 public class EmployeeControls extends JPanel { private JLabel cost, inputLabel1

我在项目的侦听器中得到错误“*运算符未定义为JTextField,Double”。这是一个JavaGUI项目,我们需要有两个文本字段,一个用于“小时”,另一个用于“速率”。我们还需要有一个列出员工类型的组合框和一个计算按钮。我在监听器下面的开关案例2中得到了错误(pay=hours*payRate;)。如何转换文本,以便将小时数乘以工资率

public class EmployeeControls extends JPanel
{
private JLabel cost, inputLabel1, inputLabel2, outputLabel, resultLabel;
private JTextField hours, rate;
private JComboBox employeeCombo;
private JButton calculateButton;
private double pay, bonus, payRate;

public EmployeeControls()
{   
    inputLabel1 = new JLabel("Hours:");
    inputLabel2 = new JLabel("Rate:");
    outputLabel = new JLabel("Pay:");
    resultLabel = new JLabel("------");
    hours = new JTextField(2);
    rate = new JTextField(5);
    hours.addActionListener(new CalcListener());
    rate.addActionListener(new CalcListener());

    String[] employeeTypes = {"Select an Employee 
    Type","Salaried","Hourly","Volunteer"};


    employeeCombo = new JComboBox(employeeTypes);

    calculateButton = new JButton("Calculate Pay");

    cost = new JLabel("Pay: " + pay);

    setPreferredSize(new Dimension (400, 100));
    setBackground(Color.cyan);

    add(employeeCombo);
    add(calculateButton);

    calculateButton.addActionListener(new CalcListener());

   }

    private class CalcListener implements ActionListener
   {
    public void actionPerformed (ActionEvent event)
    {

        String text = rate.getText();
        String strRate = Double.toString(payRate);

        String text2 = hours.getText();
        String strHours = Double.toString(hours);


        int employeeType = employeeCombo.getSelectedIndex();

        switch(employeeType)
        {
            case 0:
                JOptionPane.showMessageDialog(null, "Select an Employee 
       Type");
                break;
            case 1:
                pay = (2000.00 + bonus);
                JOptionPane.showMessageDialog(null, "Enter bonus amount");
                break;
            case 2:
                pay = hours * payRate;
                break;
            case 3:
                pay = 0.00;
                break;

        }
        cost.setText("Cost = " + pay);
     }

     }
     }

您正在尝试多应用JTextField:这没有意义

要修复此问题,请使用
hours.getText()
获取文本字段的值,然后使用
Integer.parseInt(hours.getText())
将其解析为
int
。因此,它变成:

pay = Integer.parseInt(hours.getText()) * payRate

pay=hours*payRate
-
hours
是一个
JTextField
,如何将其乘以一个双精度?
String strHours=double.toString(小时)
还将为simular提供问题reasons@MadProgrammer我们需要使用文本字段,工资率需要有一个小数点,所以我不相信我可以使用整数。您将使用什么类型?
Double.toString(小时)
需要
Double
值。我认为您的所有转换都是向后的,您需要将
字符串
解析为
双精度
,因此这意味着您需要首先从文本字段中获取
字符串
文本Hi Ollaw,感谢您的提示。我们需要从文本字段中获取payrate,您将如何编写它?同样,使用.getText()获取文本字段的文本,但此方法返回字符串,因此您可以将其转换为所需的类型,例如使用int,您需要使用Integer.parseInt(value),使用double double.parseDouble(value)等等