简体   繁体   English

Java中的ActionListener

[英]ActionListener in Java

/* I am trying to make a basic calculator in JAVA using swings. However, after adding
 * actionlisteners to each of my buttons, the actionPerformed method is never called. I can't
 *seem to figure out why .
 */

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

public class calculator extends JApplet implements ActionListener
{
         JLabel num1_label,num2_label,result_label;
         JTextField t1,t2,t3;
         JButton sum,sub,mul,div;     
         float num1,num2,res;

    public calculator ()
    {
        Container contentPanel = getContentPane();
        contentPanel.setLayout(new GridLayout(6,2));

        JLabel num1_label = new JLabel("Number 1");
        contentPanel.add(num1_label);

        JTextField t1 = new JTextField(10);
        contentPanel.add(t1);

        JLabel num2_label = new JLabel("Number 2");
        contentPanel.add(num2_label);

        JTextField t2 = new JTextField(10);
        contentPanel.add(t2);

        JLabel result_label = new JLabel("Result");
        contentPanel.add(result_label);

        JTextField t3 = new JTextField(10);
        contentPanel.add(t3);

        JButton sum = new JButton("Add");      
        sum.addActionListener(this);
        contentPanel.add(sum);

        JButton sub = new JButton("Subtract");
        sub.addActionListener(this);
        contentPanel.add(sub);

        JButton mul = new JButton("Multiply");
        mul.addActionListener(this);
        contentPanel.add(mul);

        JButton div = new JButton("Divide");
        div.addActionListener(this);
        contentPanel.add(div);

        }

        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource() == sum)
              {
                  addition();
              }

            if(ae.getSource() == sub)
            {
                subtraction();
            }

            if(ae.getSource() == mul)
            {
                multiplication();
            }

            if(ae.getSource() == div)
            {
                division();
            }

        }

        public void addition()
        {
            try {
                  num1 = Float.parseFloat(t1.getText());
                  num2 = Float.parseFloat(t2.getText());
                  res  = num1 + num2;                
                  t3.setText(" " + res);
                }
                catch (NumberFormatException nfe) 
                {
                    t1.setText("0");
                    t2.setText("0");
                    t3.setText("0");
                }

        }

         public void subtraction()
        {
            try {
                  num1 = Float.parseFloat(t1.getText());
                  num2 = Float.parseFloat(t2.getText());
                  res  = num1 - num2;                
                  t3.setText(" " + res);
                }
                catch (NumberFormatException nfe) 
                {
                    t1.setText("0");
                    t2.setText("0");
                    t3.setText("0");
                }

        } 

        public void multiplication()
        {
            try {
                  num1 = Float.parseFloat(t1.getText());
                  num2 = Float.parseFloat(t2.getText());
                  res  = num1 * num2;                
                  t3.setText(" " + res);
                }
                catch (NumberFormatException nfe) 
                {
                    t1.setText("0");
                    t2.setText("0");
                    t3.setText("0");
                }

        }

        public void division()
        {
            try {
                  num1 = Float.parseFloat(t1.getText());
                  num2 = Float.parseFloat(t2.getText());
                  res  = num1 / num2;                
                  t3.setText(" " + res);
                }
                catch (NumberFormatException nfe) 
                {
                    t1.setText("0");
                    t2.setText("0");
                    t3.setText("0");
                }

        }

        public static void main(String argsp[])
        {
            calculator c = new calculator();
        }

}

You first declare the buttons as member variables and then you use (declare again and initialize) the same variable names inside the constructor. 首先,将按钮声明为成员变量,然后在构造函数中使用(再次声明并初始化)相同的变量名。 This means that the member variables remain all equal to null . 这意味着成员变量保持全部等于null To fix this, just drop the leading JButton from the buttons initialization in the constructor. 要解决此问题,只需从构造函数中的按钮初始化中删除领先的JButton

JButton mul = new JButton("Multiply");

becomes 变成

mul = new JButton("Multiply");

The issue related to variable names that you had here is known as shadowing. 您在此处遇到的与变量名称有关的问题称为阴影。

And the full solution. 以及完整的解决方案。 Please note the changes to the class name, the variable declaration, formatting,.... 请注意对类名,变量声明,格式等的更改。

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Calculator extends JApplet implements ActionListener {
    private JLabel num1_label;
    private JLabel num2_label;
    private JLabel result_label;
    private JTextField t1;
    private JTextField t2;
    private JTextField t3;
    private JButton sum;
    private JButton sub;
    private JButton mul;
    private JButton div;
    private float num1;
    private float num2;
    private float res;

    public Calculator() {
        Container contentPanel = getContentPane();
        contentPanel.setLayout(new GridLayout(6, 2));

        num1_label = new JLabel("Number 1");
        contentPanel.add(num1_label);

        t1 = new JTextField(10);
        contentPanel.add(t1);

        num2_label = new JLabel("Number 2");
        contentPanel.add(num2_label);

        t2 = new JTextField(10);
        contentPanel.add(t2);

        result_label = new JLabel("Result");
        contentPanel.add(result_label);

        t3 = new JTextField(10);
        contentPanel.add(t3);

        sum = new JButton("Add");
        sum.addActionListener(this);
        contentPanel.add(sum);

        sub = new JButton("Subtract");
        sub.addActionListener(this);
        contentPanel.add(sub);

        mul = new JButton("Multiply");
        mul.addActionListener(this);
        contentPanel.add(mul);

        div = new JButton("Divide");
        div.addActionListener(this);
        contentPanel.add(div);
    }

    public void actionPerformed(ActionEvent ae) {
        System.out.println("hhhhh");
        if (ae.getSource() == sum) {
            addition();
        }

        if (ae.getSource() == sub) {
            subtraction();
        }

        if (ae.getSource() == mul) {
            multiplication();
        }

        if (ae.getSource() == div) {
            division();
        }
    }

    public void addition() {
        try {
            num1 = Float.parseFloat(t1.getText());
            num2 = Float.parseFloat(t2.getText());
            res = num1 + num2;
            t3.setText(" " + res);
        } catch (NumberFormatException nfe) {
            t1.setText("0");
            t2.setText("0");
            t3.setText("0");
        }

    }

    public void subtraction() {
        try {
            num1 = Float.parseFloat(t1.getText());
            num2 = Float.parseFloat(t2.getText());
            res = num1 - num2;
            t3.setText(" " + res);
        } catch (NumberFormatException nfe) {
            t1.setText("0");
            t2.setText("0");
            t3.setText("0");
        }
    }

    public void multiplication() {
        try {
            num1 = Float.parseFloat(t1.getText());
            num2 = Float.parseFloat(t2.getText());
            res = num1 * num2;
            t3.setText(" " + res);
        } catch (NumberFormatException nfe) {
            t1.setText("0");
            t2.setText("0");
            t3.setText("0");
        }
    }

    public void division() {
        try {
            num1 = Float.parseFloat(t1.getText());
            num2 = Float.parseFloat(t2.getText());
            res = num1 / num2;
            t3.setText(" " + res);
        } catch (NumberFormatException nfe) {
            t1.setText("0");
            t2.setText("0");
            t3.setText("0");
        }

    }

    public static void main(String argsp[]) {
        Calculator c = new Calculator();
    }

}

Try this in you constructor: 在构造函数中尝试以下操作:

sum.setActionCommand ("sum");

And in your actionPerformed ask: 并在您执行的动作中询问:

if ( ae.getActionCommand().equals("sum")) {
    ...
}

And the same for all your buttons. 所有按钮都一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM