java最代码大全 java代码大全网( 三 )


container.add(button);
}
private class InsertAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String input=event.getActionCommand();
if (start) {
displayField.setText("");
start=false;
if(input.equals("+/-"))
displayField.setText(displayField.getText()+"-");
}
if(!input.equals("+/-")) {
if(input.equals("Backspace")) {
String str=displayField.getText();
if(str.length()0)
displayField.setText(str.substring(0,str.length()-1));
}else if(input.equals("CE")||input.equals("C")) {
displayField.setText("0");
start=true;
}else
displayField.setText(displayField.getText()+input);
}
}
}
private class CommandAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String command=evt.getActionCommand();
if(start) {
lastCommand=command;
}else {
calculate(Double.parseDouble(displayField.getText()));
lastCommand=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;
displayField.setText(""+ result);
}
public static void main(String []args) {
Calculator calculator=new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
关于java最代码大全和java代码大全网的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读