Java 如何向同一对象添加不同的ActionListener

Java 如何向同一对象添加不同的ActionListener,java,applet,actionlistener,calculator,japplet,Java,Applet,Actionlistener,Calculator,Japplet,如何向p1对象添加不同的ActionListener。我希望程序能够设置文本栏到适当的数字时,按下适当的按钮。由于它们不是不同的变量,我不能简单地使用下面的代码(在我的actionPerformed函数中) if(例如getSource()==button1){ txtField.setText(“1”); } import java.awt.*; import java.applet.*; import java.awt.event.*; public class Telephone ext

如何向p1对象添加不同的ActionListener。我希望程序能够设置文本栏到适当的数字时,按下适当的按钮。由于它们不是不同的变量,我不能简单地使用下面的代码(在我的actionPerformed函数中)

if(例如getSource()==button1){
txtField.setText(“1”);
}

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Telephone extends Applet implements ActionListener
{
   TextField txtField;

   public void init() {
       setLayout(new BorderLayout());

       txtField = new TextField("");
       add(txtField, BorderLayout.NORTH);

       Panel p1 = new Panel();
       p1.setLayout(new GridLayout(4, 3));
       p1.add(new Button("1"));
       p1.add(new Button("2"));
       p1.add(new Button("3"));
       p1.add(new Button("4"));
       p1.add(new Button("5"));
       p1.add(new Button("6"));
       p1.add(new Button("7"));
       p1.add(new Button("8"));
       p1.add(new Button("9"));
       p1.add(new Button("*"));
       p1.add(new Button("0"));
       p1.add(new Button("#"));       
       add(p1, BorderLayout.CENTER);

   }

   public void actionPerformed(ActionEvent e) {


    }
}

不要让您的GUI类也成为您的侦听器类,因为这要求类做太多的事情。而是考虑使用匿名内部侦听器类或私有内部类。顺便说一句,我看不出您在哪里向按钮添加任何侦听器。另外,为了我的钱,我会创建一个Swing GUI,而不是AWT GUI,因为Swing更加健壮和灵活

还要注意,对于上面的例子,我实际上会给我所有的按钮对象一个相同的动作监听器。如果使用Swing,我可以简单地获取ActionEvent对象的actionCommand,这将是感兴趣的数字字符串。不需要10个if块或开关块

例如,这演示了在JTextField中显示数字的非常简单的逻辑,但没有计算逻辑:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class CalcEg {
   private static final float BTN_FONT_SIZE = 18f;
   private static final String[][] BTN_LABELS = {
      {"7", "8", "9", "-"},
      {"4", "5", "6", "+"},      
      {"1", "2", "3", "/"},
      {"0", ".", "=", "*"}
   };
   private JPanel mainPanel = new JPanel();
   private JTextField textField = new JTextField(10);

   public CalcEg() {
      int rows = BTN_LABELS.length;
      int cols = BTN_LABELS[0].length;
      int gap = 4;

      JPanel buttonPanel = new JPanel(new GridLayout(rows, cols, gap, gap));
      for (String[] btnLabelRow : BTN_LABELS) {
         for (String btnLabel : btnLabelRow) {
            JButton btn = createButton(btnLabel);
            if ("0123456789.".contains(btnLabel)) {
               btn.setAction(new NumberListener(btnLabel));
            } 
            buttonPanel.add(btn);
         }
      }

      textField.setFont(textField.getFont().deriveFont(BTN_FONT_SIZE));

      mainPanel.setLayout(new BorderLayout(gap, gap));
      mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
      mainPanel.add(textField, BorderLayout.PAGE_START);
      mainPanel.add(buttonPanel, BorderLayout.CENTER);
   }

   private JButton createButton(String btnLabel) {
      JButton button = new JButton(btnLabel);
      button.setFont(button.getFont().deriveFont(BTN_FONT_SIZE));
      return button;
   }

   public JComponent getMainComponent() {
      return mainPanel;
   }

   private class NumberListener extends AbstractAction {
      NumberListener(String actionCommand) {
         super(actionCommand);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         String actionCommand = e.getActionCommand();
         textField.setText(textField.getText() + actionCommand);
      }
   }

   private static void createAndShowGui() {
      CalcEg mainPanel = new CalcEg();

      JFrame frame = new JFrame("CalcEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel.getMainComponent());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

您可以将
e.getSource()
强制转换为
按钮
,并检查按钮上的文本。