我一直在为我的学校的一个项目制作一个小程序,但我偶然发现了一个严重的错误。我的问题可能很常见,但我似乎找不到答案。我收到错误:
The type Pyramid must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
但我已经在那里实现了该方法,并且它的拼写是正确的。我检查了很多次,我的头很痛。我的项目在这个过程中已经卡住了大约 3 个小时,我不知道如何让它工作。哦,你还认为使用 3 个多边形绘制金字塔是在 java 中最好的方法吗?
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Pyramid extends Applet implements ActionListener {
int n = 3; // vurhove
double a, k;
double Sok, S, V;
public void init()
{
setLayout(null);
TextField aT; // textbox za osn. rub
aT = new TextField("Osnoven rub", 20);
aT.setBounds(20, 410, 125, 20);
TextField lT; // textbox za ok. rub
lT = new TextField("Okolen rub", 20);
lT.setBounds(20, 460, 125, 20);
TextField SokT; // textbox S ok.
SokT = new TextField("Okolna povurhnina", 20);
SokT.setBounds(20, 510, 125, 20);
TextField ST; // textbox S1
ST = new TextField("Osnovna povurhnina", 20);
ST.setBounds(20, 560, 125, 20);
TextField VT; // textbox za obema
VT = new TextField("Obem", 20);
VT.setBounds(20, 610, 125, 20);
Button ochButton; // izchertava piramidata
ochButton = new Button("Izchertai");
ochButton.setBounds(700, 435, 100, 30);
ochButton.addActionListener(ActionOchButton);
Button oznButton; // ozna4ava vurhovete, izpisva velichinite
oznButton = new Button("Oznachi cherteja");
oznButton.setBounds(700, 510, 100, 30);
oznButton.addActionListener(ActionOznButton);
Button iButton; // iz4islqva veli4inite
iButton = new Button("Izchisli");
iButton.setBounds(700, 585, 100, 30);
iButton.addActionListener(ActioniButton);
add(aT);
add(lT);
add(SokT);
add(ST);
add(VT);
add(ochButton);
add(oznButton);
add(iButton);
Color bg;
bg = new Color(168,168,168);
setBackground(bg);
}
ActionListener ActionOchButton = new ActionListener() {
public void actionPerformed(ActionEvent e){
Graphics g = getGraphics();
int px[] = {340, 440, 490};
int py[] = {235, 335, 235};
g.drawPolygon(px, py, 3);
int px1[] = {340, 415, 490};
int py1[] = {235, 60, 235};
g.drawPolygon(px1, py1, 3);
int px2[] = {440, 415, 490};
int py2[] = {335, 60, 235};
g.drawPolygon(px2, py2, 3);
}
};
ActionListener ActionOznButton = new ActionListener() {
public void actionPerformed(ActionEvent e){
}
};
ActionListener ActioniButton = new ActionListener() {
public void actionPerformed(ActionEvent e){
}
};
public void paint(Graphics g)
{
g.setColor(Color.white);
g.drawRect(20, 20, 780, 370);
}
}
最佳答案
您还没有实现该方法,简单明了。
您需要在您的类中定义该方法。您添加了一堆其他操作监听器,但您的类中没有任何地方实现所需的方法。
为了进一步澄清,在与 init()
方法相同的级别上,您需要一个 public void actionPerformed(ActionEvent e) {/* ... */}
方法。
关于java - ActionListener问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24106168/