2012-04-13 6 views
2

私の年齢計算プログラムでJavaの計算に問題があります。私は生年月日、生年月日、生年月日の値を設定するとうまくいきますが、ユーザーが自分の生年月日をテキストフィールドに入力して、その値で作業しようとすると、デッドエンドになります。 Yahoo Answersについて質問したところ、ヒントは「getActionCommand()の戻り値は文字列で、結果はJLabelです。比較できますか?」私はそのヒントをどうするべきか正確には分かりません。以下のコード内にActionListenerを実装する

ここに私が持っているものと、私が "ユーザー入力"のアイデア全体を実装しようとした方法があります。私はコーディングが面倒で非効率であると確信しています。私は助けていただければ幸いです!

//Date: April 11, 2012 
    //Description: Calculates the age in terms of days depending on your birthdate. 
    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 

    public class AgeCalculator extends Frame implements ActionListener { 

JButton equal; 
JTextField year, month, day; 
JLabel result, first, second, third; 
JFrame frame; 
JPanel panel; 

static int totaldaysalive; 
static int daysaliveyr; 
static int daysalivem; 
static int birthyr; 
static int birthm; 
static int birthd; 
static int currentyr = 2012; 


public AgeCalculator(){ 
    gui(); 
} 

public void gui(){ 
    frame = new JFrame ("Age Calculator"); 
    panel = new JPanel(new GridBagLayout()); 
    panel.setBackground(Color.LIGHT_GRAY); 
    GridBagConstraints x = new GridBagConstraints(); 

    equal = new JButton ("Get Result"); 

    x.insets = new Insets(3,0,3,0); 

    first = new JLabel("Year "); 
    x.gridx = 0; 
    x.gridx = 0; 
    panel.add(first, x); 

    year = new JTextField(10); 
    x.gridx = 5; 
    x.gridy = 0; 
    x.gridwidth = 3; 
    panel.add(year, x); 

    second = new JLabel ("Month "); 
    x.gridx = 0; 
    x.gridy = 1; 
    panel.add(second,x); 

    month = new JTextField(10); 
    x.gridx = 5; 
    x.gridy = 1; 
    x.gridwidth = 3; 
    panel.add(month,x); 

    third = new JLabel ("Day  "); 
    x.gridx = 0; 
    x.gridy = 2; 
    panel.add(third,x); 

    day = new JTextField(10); 
    x.gridx = 5; 
    x.gridy = 2; 
    x.gridwidth = 3; 
    panel.add(day,x); 

    x.gridx = 6; 
    x.gridy = 3; 
    panel.add(equal,x); 

    result = new JLabel (""); 
    x.gridx = 5; 
    x.gridy = 5; 
    panel.add(result,x); 

    frame.add(panel); 
    frame.setVisible(true); 
    frame.setSize(350, 350); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Calc e = new Calc(); 

    equal.addActionListener(e); 
    year.addActionListener(e); 
    month.addActionListener(e); 
    day.addActionListener(e); 
} 

class Calc implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     try { 
      birthyr = Integer.parseInt(year.getText()); 

     } catch (NumberFormatException a) { 
      result.setText("Illegal data for first field."); 
      result.setForeground(Color.red); 
      return; 
     } 

     try { 

      birthm = Integer.parseInt(month.getText()); 

     } catch (NumberFormatException a) { 

      result.setText("Illegal data for second field."); 
      result.setForeground(Color.red); 
      return; 
     } 
     try { 

      birthd = Integer.parseInt(day.getText()); 

     } catch (NumberFormatException a) { 

      result.setText("Illegal data for third field."); 
      result.setForeground(Color.red); 
      return; 
     } 

     if (e.getActionCommand().equals (equal)){ 

      totaldaysalive = ageCalcYr() + ageCalcM() + birthd; 
      result.setText(Integer.toString(totaldaysalive)); 
     } 
    } 

    public int ageCalcYr(){ 
     for (int i = birthyr; i <= currentyr; i++){ 
      if ((i % 4 == 0) && (!(i % 100 == 0) || (i % 400 == 0))){ 
       daysaliveyr = daysaliveyr + 366; 
      } 
      else { 
       daysaliveyr = daysaliveyr + 365; 
      } 
     } 
     return daysaliveyr; 
    } 
    public int ageCalcM(){ 
     if (birthm == 1){ 
      daysalivem = daysalivem + 0; 
     } 
     else if (birthm == 2){ 
      daysalivem = daysalivem + 30; 
     } 
     else if (birthm == 3){ 
      daysalivem = daysalivem + 60; 
     } 
     else if (birthm == 4){ 
      daysalivem = daysalivem + 90; 
     } 
     else if (birthm == 5){ 
      daysalivem = daysalivem + 120; 
     } 
     else if (birthm == 6){ 
      daysalivem = daysalivem + 150; 
     } 
     else if (birthm == 7){ 
      daysalivem = daysalivem + 180; 
     } 
     else if (birthm == 8){ 
      daysalivem = daysalivem + 210; 
     } 
     else if (birthm == 9){ 
      daysalivem = daysalivem + 240; 
     } 
     else if (birthm == 10){ 
      daysalivem = daysalivem + 270; 
     } 
     else if (birthm == 11){ 
      daysalivem = daysalivem + 300; 
     } 
     else if (birthm == 12){ 
      daysalivem = daysalivem + 330; 
     } 
     return daysalivem; 
    } 
} 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     AgeCalculator gui = new AgeCalculator(); 
    } 
    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     // TODO Auto-generated method stub 
    } 
} 

答えて

0

equal.addMouseListener(e)と思っていますか?もちろんCalcを変更してMouseListenerを実装する必要があります。実際には、mouseClicked(MouseEvent)メソッドを記述するだけで済みます。他のすべてのものは、あなたが後にしているものよりもより具体的なもののためです。

これは、ボタンのクリックイベントに応答します。私はあなたが他のリスナーを望んでいるとは思わない。その場合は、KeyListenersまたはActionListeners以外の値にする必要があります。

あなたのインデントがオフになっているので、私には分かりませんが、あなたのintフィールドがstaticである理由が分かりません。私はそれがおそらく不要だと思う。

+0

ああ、ごめんなさい。私はこのサイトを初めて使用しました。コードを貼り付けても実際にはわかりません。私は思っていません。とにかく、私は教師がActionListenerだけを教えてくれるので、どういうふうに使うのかは分かりません。 – Noelle

0

ボタンを使用して計算を開始しているため、そのボタンにアクションリスナーを登録するだけです。実行されたアクションの中で、年齢を読み、解析し、計算する。

+0

アクションリスナーとしてボタンを登録するのではなく、私がすでにやっていたことだと思いました。 – Noelle

3

高速治癒:

if (e.getActionCommand().equals ("Get Result")) { // equal)) { 
     totaldaysalive = ageCalcYr() + ageCalcM() + birthd; 
     result.setText (Integer.toString (totaldaysalive)); 
    } 

あなたには、いくつかの時間を持っている場合、私はあなたに20点の改良を投稿することができます。あなたは、フレームを拡張

  1. IMPL。しかし、AgeCalculatorにはJFrame(SwingContextのほうが、AWTであるFrameよりも優れています)があり、使用される別々のActionListenerがあります。
  2. 宣言と最後に優先されるメソッドを削除します。そこ
 
    public class AgeCalculator // extends Frame implements ActionListener 

ビジュアル・コンポーネントおよびその他の属性のブロックに続く、後のものは、静的である - 同じJVM上の2 AgeCalculatorsの使用禁止しています。それは確かに意図による制限ではありません。

  1. コンパイラをシャットダウンするようなものを作成しないでください。
  2. 公開したくない場合は、すべてを非公開にしてください。
  3. 可能な場合は、属性を避けてください。
  4. いつラベルをレタッチしますか?

    daysaliveyr += 366; 
    
  5. は、月の日数をカルクbirthyrパラメータとしてbirthmを渡すために:適切な

 
    JButton equal; 
    JTextField year, month, day; 
    JLabel result, ... 

    ... 
    static int birthd; 
    static int currentyr = 2012; 
  1. 使用簡素化に加え int型totaldaysalive = ageCalcYr(birthyr) + ageCalcM(birthm)+ birthd; result.setText(Integer.toString(totaldaysive));

  2. 変数totaldaysaliveの有効期間を2行に減らすことができます。エラーがある場合は、エラーを検索するための非常に小さな範囲です。現在の状態で

    public int ageCalcM (int birthm) { 
        int daysalivem = 0;  
        if (birthm == 2) { 
         daysalivem += 30; 
        } 
        else if (birthm == 3) { 
         daysalivem += 60; 
        } 
    
  3. 、ageCalcMはprovisoriumあります。メーンでは

    public int ageCalcM (int birthm) { 
        int[] mdays = {0, 30, 60, 90, ...}; 
        return mdays [birthm]; 
    } 
    
  4. :しかし、愚かな繰り返しで、質量操作は単純な配列で解決することができ

    public int ageCalcM (int birthm) { 
        if (birthm == 2) { 
         return 30; 
        } 
        else if (birthm == 3) { 
         return 60; 
        } 
    
  5. :そうでないあなただけdaysalivem = (birthm - 1) * 30;

  6. 短いコードを言うことができます方法 'gui'インスタンスを作成します。これは決して使用されません。

    public static void main(String [] args){ new AgeCalculator();これは必要なものです。 }

  7. Gui、btw。あなたがすでにその名前のメソッドを持っていれば、悪い名前です。

  8. この方法は決して使用されないので、全体をctorに移動するだけです。
  9. 年/月/日付はActionListenerを必要としません。
  10. 他のレイアウトがはるかに適しています。
  11. プログラムを動作させるには、別のカレンダー改革が必要です。
  12. 入力はint、有効な月などである必要があります。
  13. 実際の日付は使用されません。

残っているものは何ですか?

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

public class AgeCalculator 
{ 
    JTextField year, month, day; 
    JLabel result; 

    public AgeCalculator() { 
     JFrame frame = new JFrame ("Age Calculator"); 
     JPanel panel = new JPanel (new GridBagLayout()); 
     panel.setBackground (Color.LIGHT_GRAY); 
     GridBagConstraints x = new GridBagConstraints(); 

     JButton equal = new JButton ("Get Result"); 
     x.insets = new Insets (3, 0, 3, 0); 

     JLabel first = new JLabel ("Year "); 
    // two times gridx = 0 here? 
     x.gridx = 0; 
     x.gridx = 0; 
     panel.add (first, x); 

     year = new JTextField (10); 
     x.gridx = 5; 
     x.gridy = 0; 
     x.gridwidth = 3; 
     panel.add (year, x); 

     JLabel second = new JLabel ("Month "); 
     x.gridx = 0; 
     x.gridy = 1; 
     panel.add (second, x); 

     month = new JTextField (10); 
     x.gridx = 5; 
     x.gridy = 1; 
     x.gridwidth = 3; 
     panel.add (month, x); 

     JLabel third = new JLabel ("Day  "); 
     x.gridx = 0; 
     x.gridy = 2; 
     panel.add (third, x); 

     day = new JTextField (10); 
     x.gridx = 5; 
     x.gridy = 2; 
     x.gridwidth = 3; 
     panel.add (day, x); 

     x.gridx = 6; 
     x.gridy = 3; 
     panel.add (equal, x); 

     result = new JLabel (""); 
     x.gridx = 5; 
     x.gridy = 5; 
     panel.add (result, x); 

     frame.add (panel); 
     frame.setVisible (true); 
     frame.setSize (350, 350); 
     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

     Calc e = new Calc(); 

     equal.addActionListener (e); 
    } 

    class Calc implements ActionListener { 
     public void actionPerformed (ActionEvent e) { 
      int birthyr; 
      int birthm; 
      int birthd; 

      try { 
       birthyr = Integer.parseInt (year.getText()); 
      } catch (NumberFormatException a) { 
       result.setText ("Illegal data for first field."); 
       result.setForeground (Color.red); 
       return; 
      } 
      try { 
       birthm = Integer.parseInt (month.getText()); 
      } catch (NumberFormatException a) { 
       result.setText ("Illegal data for second field."); 
       result.setForeground (Color.red); 
       return; 
      } 
      try { 
       birthd = Integer.parseInt (day.getText()); 
      } catch (NumberFormatException a) { 
       result.setText ("Illegal data for third field."); 
       result.setForeground (Color.red); 
       return; 
      } 
      if (e.getActionCommand().equals ("Get Result")) { // equal)) { 
       int totaldaysalive = ageCalcYr (birthyr) + ageCalcM (birthm) + birthd; 
       result.setText (Integer.toString (totaldaysalive)); 
      } 
     } 

     public int ageCalcYr (int birthyr) { 
      int currentyr = 2012; 
      int daysaliveyr = 0; 
      for (int i = birthyr; i <= currentyr; i++) { 
       if ((i % 4 == 0) && (! (i % 100 == 0) || (i % 400 == 0))) { 
        daysaliveyr += 366; 
       } 
       else { 
        daysaliveyr += 365; 
       } 
      } 
      return daysaliveyr; 
     } 

     public int ageCalcM (int birthm) { 
      int[] mdays = {0, 30, 60, 90, 120}; 
      return mdays [birthm]; 
     } 
    } 

    public static void main (String [] args) { 
     new AgeCalculator(); 
    } 
} 
+0

私のコードを修正する時間がたくさんあります。もしあなたがそうするのを手伝ってくれるのであれば。私は非常に感謝するだろう!しかし、あなたが提案したクイックフィックスは私にとってはうまくいかなかった。あなたはそれを試しましたか?編集:私は実際にgetActionCommand()。equalsの下にresult.setTextの何かが間違っていると思っています。 asdkjfsdなどの通常の文字列で置き換えると、まだ表示されません。誤解されていない限り、これは奇妙です。 – Noelle

+0

@ノーレ:まず第一に、 'System.out。それが確実に呼び出されるように、println( "debug");をactionListenerに挿入します。私は最初の変更の後に編集し、それは働いた。他の20の変更を実行した後も、まだ動作しています(ageCalcM配列のみを拡張する必要があります)。 –

+0

... whoa。説明をいただきありがとうございます!私は非常に感謝しています。私はそれを読んでそれを理解しようとします。正直なところ、コーディングは私には当然正確に来ていないので、これは大きな助けになっています! – Noelle