java计算器里的一个错误……

import java.awt.*;import java.awt.event.*;
import javax.swing.*;
public class calculator extends JPanel {
//定义组件
JLabel display;
JPanel panel;
double result;
String lastCommand;
boolean start;

public calculator(){
setLayout(new BorderLayout());
result=0;
lastCommand="=";
start =true;

display=new JLabel("0",SwingConstants.RIGHT);
display.setForeground(Color.black);

add(display,BorderLayout.NORTH);
ActionListener insert= new InsertAction();
ActionListener command= new CommandAction();

panel=new JPanel();
panel.setLayout(new GridLayout(4,4));

addButton("7",insert);
addButton("8",insert);
addButton("9",insert);
addButton("/",command);
addButton("4",insert);
addButton("5",insert);
addButton("6",insert);
addButton("*",command);
addButton("1",insert);
addButton("2",insert);
addButton("3",insert);
addButton("-",command);
addButton("0",insert);
addButton(".",insert);
addButton("=",command);
addButton("+",command);
add(panel,BorderLayout.CENTER);

}
//在面板容器中添加按钮组件
public void addButton(String label,ActionListener listener){
JButton button=new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
//处理单击数字按钮事件的监听器类
public class InsertAction implements ActionListener{ // InsertAction总是报错 ???
public void actionPerfirmed(ActionEvent event){
String input=event.getActionCommand();
if(start){
display.setText("");
start=false;
}
}
}
//处理单击命令按钮事件的监听器类
public class CommandAction implements ActionListener{//-----CommandAction 报错
public void actionPerfirmed(ActionEvent evt){
String input=evt.getActionCommand();
if(start){
lastCommand= command;//------------command???
}
else{
calculate(Double.parseDouble(display.getText()));
lastCommand= command;//-----------command??
start=true;
}
}
}
public void calculate(double x){
if(lastCommand.equals("-")){
result+=x;}
else if(lastCommand.equals("*")){
result*=x;}
else if(lastCommand.equals("+")){
result+=x;}
else if(lastCommand.equals("/")){
result/=x;}
else if(lastCommand.equals("=")){
result=x;}
display.setText(""+result);
}
}
import javax.swing.JFrame;
public class calculatetest { public static void main(String[] args) {
// TODO Auto-generated method stub
calculatejrame frame=new calculatejrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

import java.awt.*;import javax.swing.*;
public class calculatejrame extends JFrame{ public calculatejrame(){
setTitle("计算器");
Container contentPane=getContentPane();//创建一个容器对象
calculator panel =new calculator();
setSize(200,200);
setVisible(true);
setResizable(false);
}
}

// InsertAction总是报错 ???

和 CommandAction 报错 是因为你实现的接口里面的方法名是错的,正确的方法名是actionPerformed,你写错了,所以Eclipse会认为你没有实现接口的方法,关于这类错误,一定要学会运用@Override,要不然你以后代码一多出Bug了就根本就不知道在哪里出错的,打个比方说:你这是实现的接口,写借了IDE会认为你没有实现而给你报错,如果你是继承的适配器的话,IDE是不会给你报错的,你所有的运行也能通过,但是你监听器里的方法永远也不会执行,

至于后面的command,它应该是input参数,追问

可不可以告诉我代码怎么改呢在……有点没懂……体会不到

追答

Ctrl+F 用actionPerformed代替你的actionPerfirmed

在command报错的地方,把command换成input 就可以运行了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-22
有没有实现的方法追问

补充上去了……

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网