2017-07-15 2 views
0

私はJavaを使い慣れておらず、Java 1クラスを取っています。私は、if文でJComboBox選択を使用して、JTextFieldの選択に基づいて答えを表示しようとしています。それはすべて正しくコンパイルされましたが、答えは表示されず、私は何を変更する必要があるか分かりません。私は検索し、いくつかの異なることを試みたが、誰も動作するように見えませんでした。文章の場合、CoomboBoxを使って方程式の答えがJTextFieldに出力されない

import javax.swing.*; 
import java.awt.event.*; 

public class HayDyGuiTempConv extends JFrame 
{ 

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

private JButton buttonConvert; 
private JButton exitButton; 
private JTextField textAmount; 
private String fieldText; 
private JTextField field; 



public HayDyGuiTempConv() 
{ 

    this.setSize(440,150); 
    this.setLocation(350,420); 
    this.setTitle("Temperature Conversion"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    ButtonListener bl = new ButtonListener(); 
    JPanel panel1 = new JPanel(); 
    panel1.add(new JLabel("Enter temperature to convert: ")); 

    textAmount = new JTextField(6); 
    panel1.add(textAmount); 

    JComboBox<String> comboBox = new JComboBox<> (new String[] {"C to F", "F to C"}); 
    comboBox.addActionListener(bl); 
    panel1.add(comboBox); 

    buttonConvert = new JButton("Convert"); 
    buttonConvert.addActionListener(bl); 
    buttonConvert.setToolTipText("Convert the temperature."); 
    panel1.add(buttonConvert); 

    panel1.add(new JLabel("Temp: ")); 

    field = new JTextField(6); 
    field.setEditable(false); 
    panel1.add(field); 

    exitButton = new JButton("Exit"); 
    exitButton.addActionListener(bl); 
    exitButton.setToolTipText("Exit the program."); 
    panel1.add(exitButton); 

    this.add(panel1); 

    this.setVisible(true); 
} 

private class ButtonListener implements ActionListener 
{ 

    public void actionPerformed(ActionEvent e) 
    { 


     if(e.getSource() == buttonConvert) 
     { 

      if(e.getSource() == ("C to F")) 
      { 
       double tempEntered = Double.parseDouble(textAmount.getText()); 
       double tempConverted = tempEntered - 32 * (5/9); 
       String tempAmount = (Double.toString(tempConverted)); 
       field.setText(tempAmount); 
      } 
      else if(e.getSource() == ("F to C")) 
      { 
       double tempEntered = Double.parseDouble(textAmount.getText()); 
       double tempConverted = tempEntered * (9/5) + 32; 
       String tempAmount = (String.format("%.2f",(tempConverted))); 
       field.setText(tempAmount); 
      } 
     } 
     else if(e.getSource() == exitButton) 
     { 
      System.exit(0); 
     } 
    } 
} 
} 

編集:私は両方の提案を試してみましたが、それらの両方で、私は相互作用ペインにこれ​​を取得数で入力して変換しようとすると:スレッド「AWT-EventQueueの-0」java.langの例外を。 ClassCastExceptionが発生:javax.swing.JButtonのは、イベントの「ソース」はコンポーネントではなく、文字列であるjavax.swing.JComboBoxの

答えて

1
if(e.getSource() == ("C to F")) 

にキャストすることはできません。この場合、それはJComboBoxです。あなたは、コードのようなものでなければなりませんので、コンボボックスの選択値をテストしたい

:また

JComboBox comboBox = (JComboBox)e.getSource(); 
int index = comboBox.getSelectedIndex(); 

If (index == 0) 
    // do C to F conversion 
else 
    // do F to C conversion 

、文字列の値を比較する「==」は使用しないでください。将来、文字列の比較のために、文字列のequals(...)メソッドを使用します。

編集:

私はあなたがコンボボックスにActionListenerを追加したと思いました。コンボボックスから項目を選択すると、変換が自動的に実行されます。ボタンは必要ありません。

ボタンを保持したい場合は、コンボボックスをクラスのインスタンス変数として定義する必要があります。次に、コンボボックスに名前でアクセスするだけです。

+0

あなたの提案で自分のコードを編集しましたが、変換しようとするとたくさんの赤いテキストが表示され、JButtonをjavax.swing.JComboBoxにキャストすることはできません。 例外:スレッド「AWT-EventQueue-0」の例外java.lang.ClassCastException:javax.swing.JButtonをjavax.swing.JComboBoxにキャストできません –

+0

@DylanHayesは編集を参照してください。 – camickr

+0

ありがとうございました。私はインスタンス変数を追加するようにボタンを保持する必要があったし、それは働いた。 –

0

文字列を比較するときに==を使用しないでください。これは、値だけでなくインスタンスを比較するためです。

comboBoxの文字列値をcomboBox.getSelectedItem()。toString();で取得します。 と比較し、それらを.equals()メソッドと比較します。

ほとんどの場合、数値またはインスタンスを比較するために==が使用されます。

String selected = comboBox.getSelectedItem().toString(); 

if(selected.equals("C to F")){ 
// do C to F conversion 
}else{ 
// do F to C conversion 
} 
+0

あなたの提案で自分のコードを編集しましたが、変換しようとすると、JButtonをjavax.swing.JComboBoxにキャストすることができないという赤いテキストが多く表示されます。次の例外があります:スレッド「AWT-EventQueue-0」の例外java.lang.ClassCastException:javax.swing.JButtonをjavax.swing.JComboBoxにキャストできません - –

関連する問題