2017-05-11 6 views
-1

プロジェクトのリスナーで「* Operator Undefined for JTextField、Double」というエラーが表示されます。これはJava GUIプロジェクトで、2つのテキストフィールドが必要です.1つは「時間」、もう1つは「レート」です。また、従業員のタイプと計算ボタンを列挙したコンボボックスが必要です。リスナーの下でスイッチのケース2(pay = hours * payRate;)にエラーが表示されます。 payRateで時間を掛けることができるように、テキストをどのように変換すればよいですか?JTextField、DoubleのJava *演算子が未定義

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); 
    } 

    } 
    } 
+2

'pay = hours * payRate' - ' hours'は 'JTextField'ですか、どのように倍で複数倍できますか? – MadProgrammer

+0

'文字列strHours = Double.toString(時);'はまたsimularの理由のために問題を与えるつもりです – MadProgrammer

+0

@MadProgrammer私たちはテキストフィールドを使用する必要があります、payrateは10進数を持つ必要がありますので私は信じられません整数を使用します。どのようなタイプを使用しますか? – lorac1969

答えて

3

JTextFieldをマルチプレックスしようとしています。意味がありません。

修正するには、hours.getText()でテキストフィールドの値を取得し、それをintに解析してInteger.parseInt(hours.getText())にします。したがって、それは次のようになります:

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

こんにちはOllaw、ありがとう先端。テキストフィールドから支払いを受ける必要がありますが、どのように書きますか? – lorac1969

+0

同じように、.getText()を使ってtextFieldのテキストを取得しますが、このメソッドはStringを返します。たとえば、Integer.parseInt(value)を実行する必要がある場合など、必要な型にキャストします。 double Double.parseDouble(value)など – Ollaw

関連する問題