问题描述
import java.awt.*;导入 java.awt.event.*;公共类 QuadraticSolver 扩展 Frame 实现 ActionListener、WindowListener{私人文本字段 tfX2;私人文本字段 tfX;私人文本字段 tfNum;私人文本字段 tfVal1;私人文本字段 tfVal2;私人文本字段 tfRoots;私人标签 lblX2;私人标签 lblX;私人标签 lblNum;私有标签 lblVal1;私人标签 lblVal2;私人标签 lblRoots;私人按钮 btnCheckRoots;私人按钮 btnCalc;私人按钮 btnClear;双 a = 0, b = 0, c = 0;双Val1 = 0,Val2 = 0,判别式= 0;字符串 StrVal1, StrVal2;公共 QuadraticSolver(){Panel panelX2Comp = new Panel(new FlowLayout());{lblX2 = new Label("输入X^2的系数:");panelX2Comp.add (lblX2);tfX2 = new TextField("", 20);tfX2.setEditable(true);panelX2Comp.add(tfX2);}Panel panelXComp = new Panel(new FlowLayout());{lblX = new Label("输入X的系数:");panelXComp.add(lblX);tfX = new TextField("", 20);tfX.setEditable(true);panelXComp.add(tfX);}Panel panelNumComp = new Panel(new FlowLayout());{lblNum = new Label("请输入数字:");panelNumComp.add(lblNum);tfNum = new TextField("", 20);tfNum.setEditable(true);panelNumComp.add(tfNum);}Panel panelButtons = new Panel(new FlowLayout());{btnCalc = new Button("计算");btnCalc.setEnabled(false);panelButtons.add(btnCalc);{btnCalc.addActionListener(new ActionListener(){@覆盖public void actionPerformed(ActionEvent e){a = Double.parseDouble(tfX2.getText());b = Double.parseDouble(tfX.getText());c = Double.parseDouble(tfNum.getText());Val1 = (-b + Math.sqrt(判别式))/(2 * a);Val2 = (-b - Math.sqrt(判别式))/(2 * a);StrVal1 = String.valueOf(Val1);StrVal2 = String.valueOf(Val2);tfVal1.setText(StrVal1);tfVal2.setText(StrVal2);tfX2.setText("");tfX.setText("");tfNum.setText("");btnCalc.setEnabled(false);}});}btnCheckRoots = new Button("根的性质");panelButtons.add(btnCheckRoots);{btnCheckRoots.addActionListener(new ActionListener(){@覆盖public void actionPerformed(ActionEvent e){a = Double.parseDouble(tfX2.getText());b = Double.parseDouble(tfX.getText());c = Double.parseDouble(tfNum.getText());判别式 = (b*b) - (4*(a*c));如果(判别式== 0){tfRoots.setText("相等");btnCalc.setEnabled(true);}否则如果(判别式
这是我的代码.它给我一个错误说QuadraticSolver.java:4: error: QuadraticSolver
不是 abstract
并且不会覆盖抽象方法 actionPerformed(ActionEvent)
在 ActionListener
public class QuadraticSolver extends Frame 实现 ActionListener, WindowListener
"
我不知道该怎么办.我尝试在所有 ActionListener 事件之前添加 @Override,仍然不起作用.
既然 QuadraticSolver
实现了 ActionListener
,它应该实现 actionPerformed
.>
您在匿名类中实现了该方法.
要解决此问题,请将 actionPerformed
的实现添加到 QuadraticSolver
或不需要 QuadraticSolver
来实现该接口.
import java.awt.*;
import java.awt.event.*;
public class QuadraticSolver extends Frame implements ActionListener, WindowListener
{
private TextField tfX2;
private TextField tfX;
private TextField tfNum;
private TextField tfVal1;
private TextField tfVal2;
private TextField tfRoots;
private Label lblX2;
private Label lblX;
private Label lblNum;
private Label lblVal1;
private Label lblVal2;
private Label lblRoots;
private Button btnCheckRoots;
private Button btnCalc;
private Button btnClear;
double a = 0, b = 0, c = 0;
double Val1 = 0, Val2 = 0, Discriminant = 0;
String StrVal1, StrVal2;
public QuadraticSolver()
{
Panel panelX2Comp = new Panel(new FlowLayout());
{
lblX2 = new Label("Enter Co-Efficient Of X^2:");
panelX2Comp.add (lblX2);
tfX2 = new TextField("", 20);
tfX2.setEditable(true);
panelX2Comp.add(tfX2);
}
Panel panelXComp = new Panel(new FlowLayout());
{
lblX = new Label("Enter Co-Efficient Of X:");
panelXComp.add(lblX);
tfX = new TextField("", 20);
tfX.setEditable(true);
panelXComp.add(tfX);
}
Panel panelNumComp = new Panel(new FlowLayout());
{
lblNum = new Label("Enter Number:");
panelNumComp.add(lblNum);
tfNum = new TextField("", 20);
tfNum.setEditable(true);
panelNumComp.add(tfNum);
}
Panel panelButtons = new Panel(new FlowLayout());
{
btnCalc = new Button("Calculate");
btnCalc.setEnabled(false);
panelButtons.add(btnCalc);
{
btnCalc.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
a = Double.parseDouble(tfX2.getText());
b = Double.parseDouble(tfX.getText());
c = Double.parseDouble(tfNum.getText());
Val1 = (-b + Math.sqrt(Discriminant)) / (2 * a);
Val2 = (-b - Math.sqrt(Discriminant)) / (2 * a);
StrVal1 = String.valueOf(Val1);
StrVal2 = String.valueOf(Val2);
tfVal1.setText(StrVal1);
tfVal2.setText(StrVal2);
tfX2.setText("");
tfX.setText("");
tfNum.setText("");
btnCalc.setEnabled(false);
}
}
);
}
btnCheckRoots = new Button("Nature Of Roots");
panelButtons.add(btnCheckRoots);
{
btnCheckRoots.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
a = Double.parseDouble(tfX2.getText());
b = Double.parseDouble(tfX.getText());
c = Double.parseDouble(tfNum.getText());
Discriminant = (b*b) - (4*(a*c));
if (Discriminant == 0)
{
tfRoots.setText("Equal");
btnCalc.setEnabled(true);
}
else if (Discriminant < 0)
{
tfRoots.setText("Imaginary");
}
else
{
tfRoots.setText("Real, Distinct");
btnCalc.setEnabled(true);
}
}
}
);
}
btnClear = new Button("Clear");
panelButtons.add(btnClear);
{
btnClear.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
a = 0; b = 0; c = 0;
Val1 = 0; Val2 = 0; Discriminant = 0;
tfX2.setText("");
tfX.setText("");
tfNum.setText("");
tfVal1.setText("");
tfVal2.setText("");
tfRoots.setText("");
}
}
);
}
}
Panel panelRoots = new Panel(new FlowLayout());
{
lblRoots = new Label ("Nature Of Roots:");
panelRoots.add(lblRoots);
tfRoots = new TextField("", 20);
tfRoots.setEditable(false);
panelRoots.add(tfRoots);
}
Panel panelValues = new Panel(new FlowLayout());
{
lblVal1 = new Label("First Value:");
panelValues.add(lblVal1);
tfVal1 = new TextField("", 10);
tfVal1.setEditable(false);
panelValues.add(tfVal1);
lblVal2 = new Label("Second Value:");
panelValues.add(lblVal2);
tfVal2 = new TextField("", 10);
tfVal2.setEditable(false);
panelValues.add(tfVal2);
}
setLayout(new FlowLayout()); // "this" Frame sets to BorderLayout
add(panelX2Comp);
add(panelXComp);
add(panelNumComp);
add(panelButtons);
add(panelRoots);
add(panelValues);
setTitle("Matrix Multiplier"); // "this" Frame sets title
setSize(400, 200); // "this" Frame sets initial size
setVisible(true);
addWindowListener(this);
}
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0); // Terminate the program
}
@Override
public void windowOpened(WindowEvent e) { }
@Override
public void windowClosed(WindowEvent e) { }
@Override
public void windowIconified(WindowEvent e) { }
@Override
public void windowDeiconified(WindowEvent e) { }
@Override
public void windowActivated(WindowEvent e) { }
@Override
public void windowDeactivated(WindowEvent e) { }
public static void main(String args[])
{
new QuadraticSolver();
}
}
So this is my code. it give me an error saying "QuadraticSolver.java:4: error: QuadraticSolver
is not abstract
and does not override abstract method actionPerformed(ActionEvent)
in ActionListener
public class QuadraticSolver extends Frame implements ActionListener, WindowListener
"
I have no idea what to do. I tried adding @Override before all ActionListener events, Still doesn't work.
Since QuadraticSolver
implements ActionListener
, it should implement actionPerformed
.
You implemented that method in an anonymous class.
To solve it, either add an implementation of actionPerformed
to QuadraticSolver
or don't require QuadraticSolver
to implement that interface.
这篇关于类不是抽象的,不会覆盖抽象方法 AWT 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!