在下面的代码里面实现ActionListener

时间:2012-04-13 00:29:08

标签: java swing user-interface actionlistener

我在Java上的年龄计算程序遇到了一些麻烦。当我预设出生年份,出生月份和出生日期值时它工作正常,但当我尝试让用户将自己的生日输入到文本字段中然后尝试使用这些值时,我只是达到了死胡同。我尝试在Yahoo Answers上询问,我得到的提示是“getActionCommand()的返回值是一个字符串,而结果是JLabel。你能比较它们吗?”我不确定如何处理这个提示。

这就是我所拥有的以及我尝试实现整个“用户输入”理念的方式。我很确定我的编码是凌乱和低效的,所以请耐心等待。我很感激任何帮助!

   //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
    }
}

3 个答案:

答案 0 :(得分:3)

快速治愈:

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

如果你有时间,我可以给你发20条改进。

  1. 你扩展了框架,
  2. 参数impl。 ActionListener,但同时AgeCalculator有一个JFrame(在SwingContext中比Frame更好,这是AWT)并且使用了一个单独的Actionlistener。
  3. 最后删除声明和覆盖方法。
  4.     public class AgeCalculator // extends Frame implements ActionListener 
    

    接下来是一组可视组件和其他属性,后者是静态的 - 它禁止在同一个JVM上使用2个AgeCalculator。这肯定不是意图的限制。

    1. 不要让某些东西静态来关闭编译器。
    2. 如果您不确定要公开它,请将所有内容设为私有。
    3. 尽可能避免使用属性。
    4. 您何时会修饰标签?
    5.     JButton equal;
          JTextField year, month, day;
          JLabel result, ...
      
          ...
          static int birthd;
          static int currentyr = 2012; 
      
      1. 在适当情况下使用简化添加:

        daysaliveyr += 366;
        
      2. 要计算月份的天数,请将出生日和出生日作为参数:             int totaldaysalive = ageCalcYr(birthyr)+ ageCalcM(birthm)+ birthd;             result.setText(Integer.toString(totaldaysalive));

      3. 变量totaldaysalive的实时时间可以减少到2行 - 一个非常小的搜索错误的范围,如果有的话。

        public int ageCalcM (int birthm) {
            int daysalivem = 0;     
            if (birthm == 2) {
                daysalivem += 30;
            }
            else if (birthm == 3) {
                daysalivem += 60;
            }
        
      4. 在当前状态下,ageCalcM是一个附带条件。否则你可以说daysalivem = (birthm - 1) * 30;

      5. 更短的代码:

        public int ageCalcM (int birthm) {
            if (birthm == 2) {
                return 30;
            }
            else if (birthm == 3) {
                return 60;
            }
        
      6. 然而,愚蠢重复中的这种大规模操作可以用一个简单的数组来解决:

        public int ageCalcM (int birthm) {
            int[] mdays = {0, 30, 60, 90, ...};
            return mdays [birthm];
        }
        
      7. 在main-Method中创建一个从未使用的实例'gui'。这就是你所需要的:

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

      8. 桂,顺便说一句。如果你已经有了这个名字的方法,那就是一个坏名字。

      9. 由于从未使用过该方法,只需将整个内容移入ctor。
      10. 年/月/日不需要ActionListener。
      11. 其他布局更适合。
      12. 我们需要另一项日历改革才能使您的计划有效。
      13. 不仅要检查输入是int,还要检查有效月份等。
      14. 不使用实际日期。
      15. 还剩下什么?

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

答案 1 :(得分:0)

我想你想做equal.addMouseListener(e)。当然,您必须更改Calc才能实施MouseListener。您可能只需要实际编写mouseClicked(MouseEvent)方法。所有其他的都是比你所追求的更具体的东西。

这将响应按钮上的点击事件。我认为你不想要任何其他听众。如果是,则应为KeyListenersActionListeners以外的其他内容。

另一方面,我很难看到因为你的缩进已经关闭,但我无法确切说明为什么int字段为static。我认为这可能是不必要的。

答案 2 :(得分:0)

由于您使用按钮开始计算,因此只在该按钮上注册一个动作侦听器。在动作执行方法内部读取,解析并计算以天为单位的年龄。