简体   繁体   English

向JButton添加ActionListener

[英]Adding ActionListener to JButtons

When attempting to implement ActionListener I receive the following error 尝试实现ActionListener时收到以下错误

 EmployeesApplet.java:5: error: EmployeesApplet is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener public class EmployeesApplet extends JApplet implements ActionListener ^ 1 error 

I do not want to make EmployeesApplet abstract since it doesn't need to be. 我不想使EmployeesApplet abstract因为它不必是abstract My code is below, note I commented out the implements ActionListener and adding the JButtons as ActionListener 我的代码如下,请注意,我注释掉了implements ActionListenerimplements ActionListener ,并将JButtons添加为ActionListener

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

public class EmployeesApplet extends JApplet //implements ActionListener 
{
  public JButton              sd   = new JButton ("Salaried");
  public JButton              hr   = new JButton ("Hourly");
  public JButton              cm   = new JButton ("Commissioned");
  public JButton              cl   = new JButton ("Clear"); 

  private final int    FIELDS      =  8,   
                       FIELD_WIDTH = 20;   

  private String[]     strings     = new String[FIELDS];
  private TextFieldWithLabel[] tf  = new TextFieldWithLabel[FIELDS];
  private JTextArea    ta          = new JTextArea(5,25); 

     public void init()
     {
      String[]  s = {"First Name", "Last Name", "Employee ID", "(a) Salaried: Weekly Salary", "(b1) Hourly 1: Rate Per Hour", 
                   "(b2) Hourly 2: Hours Worked" , "(c1) Commissioned: Rate", "(c2) Commissioned: Gross Sales" };

       //----------------------
       //  Set up the Structure
       //----------------------

       Container c = getContentPane();
       JPanel f   = new JPanel(new FlowLayout());
       JPanel b   = new JPanel(new BorderLayout(2,0));

       JPanel glb = new JPanel(new GridLayout(8,1,0,2));
       JPanel gtf = new JPanel(new GridLayout(8,1,0,2));
       JPanel flb = new JPanel(new FlowLayout());


       // Add FlowLayout to the container
       c.add(f);
       // Add BorderLayout to the FlowLayout
       f.add(b);

       //---------------------------------------
       //Add JPanels to the BorderLayout regions
       //---------------------------------------

       // Add JLables to GridLayout in West
       b.add(glb, BorderLayout.WEST);
       for (int i = 0; i < tf.length; i++)
       {
        tf[i] = new TextFieldWithLabel(s[i], FIELD_WIDTH);
        glb.add(tf[i].getLabel());
       }

       // Add JTextFeilds to GridLayout in East
       b.add(gtf, BorderLayout.EAST);
       for (int i = 0; i < tf.length; i++)
       {
        tf[i] = new TextFieldWithLabel(s[i], FIELD_WIDTH);
        tf[i].getTextField();
        gtf.add(tf[i].getTextField());
       }

       // Add JButtons to FlowLayout in South
       b.add(flb, BorderLayout.SOUTH);

       flb.add(sd);
       flb.add(hr);
       flb.add(cm);
       flb.add(cl);

       //sd.addActionListener(this);
       //hr.addActionListener(this);
       //cm.addActionListener(this);
       //cl.addActionListener(this);

       // Add JTextArea and make it not editable   
       f.add(ta);
       ta.setEditable(false);

     }

     public void readFields()
     {
     }

     public void fieldsExist()
     {
     }

     public void fieldsEmpty()
     {
     }

     public void actionPerformed()
     {
     }


}

Your actionPerformed method needs an ActionEvent as it's parameter: 您的actionPerformed方法需要一个ActionEvent作为其参数:

public void actionPerformed(ActionEvent e) {

}

Otherwise you won't be overriding the method defined in ActionListener - you'll just be creating a new method. 否则,您将不会覆盖ActionListener定义的方法 -您只会创建一个新方法。 Since ActionListener is an interface, you are required to implement all the methods defined in the interface, hence the error. 由于ActionListener是接口,所以您需要实现接口中定义的所有方法,因此会出现错误。


The actionPerformed method is declared with the ActionEvent parameter to pass the method details about the event (which component triggered the event, the action command , etc..). 使用ActionEvent参数声明actionPerformed方法,以传递有关事件的方法详细信息(哪个组件触发了事件, 操作命令等)。 Without the ActionEvent parameter, there is no easy way to gather such information. 没有ActionEvent参数,就没有简单的方法来收集此类信息。 When an action is performed, an ActionEvent object is created, filled with the event information, then your actionPerformed method is invoked, which the ActionEvent object is passed in as an argument. 执行动作时,将创建一个ActionEvent对象,并填充事件信息,然后将调用您的actionPerformed方法,该ActionEvent方法将作为参数传入。

Your class is implementing the interface ActionListener 您的课程正在实现ActionListener接口

hence you have 2 options... make the class abstract or implement the method actionPerformed 因此,您有2个选项...使类抽象化或实现actionPerformed方法

the first one looks like what you need.... 第一个看起来像您所需要的...。

so try doing something like: 因此,请尝试执行以下操作:

public class EmployeesApplet extends JApplet implements ActionListener {
     ....
     ....
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
    }

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

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