gpt4 book ai didi

java - 将 ActionListener 添加到 JButton

转载 作者:行者123 更新时间:2023-12-01 21:40:03 24 4
gpt4 key购买 nike

当尝试实现 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

我不想制作EmployeesApplet abstract,因为它不需要。我的代码如下,请注意,我注释掉了 implements 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()
{
}


}

最佳答案

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

public void actionPerformed(ActionEvent e) {

}

否则你不会覆盖the method defined in ActionListener - 您将只是创建一个新方法。由于 ActionListener 是一个接口(interface),因此您需要实现该接口(interface)中定义的所有方法,因此会出现错误。

<小时/>

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

关于java - 将 ActionListener 添加到 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36606774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com