Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将ActionListener添加到JButtons_Java_Actionlistener - Fatal编程技术网

Java 将ActionListener添加到JButtons

Java 将ActionListener添加到JButtons,java,actionlistener,Java,Actionlistener,尝试实现ActionListener时,我收到以下错误 我不想让EmployeesApplet抽象化,因为它不需要。 下面是我的代码,请注意,我注释掉了实现ActionListener,并将JButtons添加为ActionListener import javax.swing.*; import java.awt.*; import java.awt.event.*; public class EmployeesApplet extends JApplet //implements Acti

尝试实现ActionListener时,我收到以下错误

我不想让
EmployeesApplet
抽象化,因为它不需要。
下面是我的代码,请注意,我注释掉了
实现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()
     {
     }


}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类EmployeesApplet扩展了JApplet//实现了ActionListener
{
公共JButton sd=新JButton(“受薪”);
公共按钮hr=新按钮(“每小时”);
公共按钮cm=新按钮(“委托”);
公共按钮cl=新按钮(“清除”);
专用最终整型字段=8,
字段宽度=20;
私有字符串[]字符串=新字符串[字段];
私有TextFieldWithLabel[]tf=新TextFieldWithLabel[FIELDS];
私人JTextArea ta=新JTextArea(5,25);
公共void init()
{
字符串[]s={“名字”、“姓氏”、“员工ID”,“(a)领薪:周工资”,“b1)小时工资1:每小时费率”,
“(b2)每小时2:工作小时数”,“(c1)委托:费率”,“(c2)委托:总销售额”};
//----------------------
//建立结构
//----------------------
容器c=getContentPane();
JPanel f=newjpanel(newflowlayout());
JPanel b=新JPanel(新边界布局(2,0));
JPanel glb=新JPanel(新网格布局(8,1,0,2));
JPanel gtf=新JPanel(新网格布局(8,1,0,2));
JPanel flb=新的JPanel(新的FlowLayout());
//将FlowLayout添加到容器中
c、 添加(f);
//将BorderLayout添加到FlowLayout
f、 添加(b);
//---------------------------------------
//将JPanel添加到BorderLayout区域
//---------------------------------------
//将JLables添加到西部的GridLayout
b、 添加(glb,BorderLayout.WEST);
for(int i=0;i
您的
actionPerformed
方法需要一个
ActionEvent
作为参数:

public void actionPerformed(ActionEvent e) {

}
否则,您将不会被重写-您将只创建一个新方法。由于
ActionListener
是一个接口,因此需要实现接口中定义的所有方法,因此会出现错误



actionPerformed
方法是用
ActionEvent
参数声明的,用于传递有关事件的方法详细信息(哪个组件触发了事件,等等)。如果没有
ActionEvent
参数,就无法轻松收集此类信息。执行操作时,将创建一个
ActionEvent
对象,并填充事件信息,然后调用
actionPerformed
方法,将
ActionEvent
对象作为参数传入。

您的
actionPerformed
方法需要一个
ActionEvent
作为参数:

public void actionPerformed(ActionEvent e) {

}
否则,您将不会被重写-您将只创建一个新方法。由于
ActionListener
是一个接口,因此需要实现接口中定义的所有方法,因此会出现错误



actionPerformed
方法是用
ActionEvent
参数声明的,用于传递有关事件的方法详细信息(哪个组件触发了事件,等等)。如果没有
ActionEvent
参数,就无法轻松收集此类信息。执行操作时,将创建一个
ActionEvent
对象,并填充事件信息,然后调用
actionPerformed
方法,将
ActionEvent
对象作为参数传入。

您的类正在实现接口
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()
     {
     }


}
因此你有两个选择。。。使类抽象或实现方法
actionPerformed

第一个看起来像你需要的

所以试着做一些类似的事情:

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

您的类正在实现接口
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()
     {
     }


}
因此你有两个选择。。。使类抽象或实现方法
actionPerformed

第一个看起来像你需要的

所以试着做一些类似的事情:

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

那么为了实现ActionListener,有没有actionPerformed方法?是的,您建议添加该参数解决了问题,我现在只是想理解我的回答,解释了如何解决问题,以及为什么需要该参数。那么,为了实现ActionListener,是否需要actionPerformed方法?是的,您添加该参数的建议解决了问题,我现在只是想理解它。我编辑了我的答案,解释了如何解决问题,以及为什么需要该参数。@beeee没有
ActionEvent e
,如何