用java程序 写简易计算器的设计,哪位高手写给我?

 我来答
春开高4266
2006-06-29 · TA获得超过1607个赞
知道小有建树答主
回答量:1446
采纳率:20%
帮助的人:0
展开全部
回答:拿破仑
新手
5月14日 13:16 1 文件名:Calculate_GUI.java

import javax.swing.*;
import java.awt.Container;
import java.awt.event.*;

public class Calculate_GUI extends JFrame{
private JPanel p1;
private JButton keyButton[]=new JButton[16],signButton,clearButton,deleteButton;
private JTextField outputField;
private Container contentPane;
private boolean firstInput=true;
private Calculate_Model model=new Calculate_Model();

public Calculate_GUI(){
p1=new JPanel();
keyButton[0]=new JButton("1");
keyButton[1]=new JButton("2");
keyButton[2]=new JButton("3");
keyButton[3]=new JButton("/");
keyButton[4]=new JButton("4");
keyButton[5]=new JButton("5");
keyButton[6]=new JButton("6");
keyButton[7]=new JButton("*");
keyButton[8]=new JButton("7");
keyButton[9]=new JButton("8");
keyButton[10]=new JButton("9");
keyButton[11]=new JButton("-");
keyButton[12]=new JButton("0");
keyButton[13]=new JButton(".");
keyButton[14]=new JButton("=");
keyButton[15]=new JButton("+");
clearButton=new JButton("c");
signButton=new JButton("+/-");
deleteButton=new JButton("←");
outputField=new JTextField(15);
outputField.setText("0");
outputField.setEditable(false);
contentPane=this.getContentPane();

this.setSize(250,250);
p1.setLayout(new java.awt.GridLayout(4,5));
for(int i=0;i<keyButton.length;i++){
if(i==4)p1.add(deleteButton);
if(i==8)p1.add(clearButton);
if(i==12)p1.add(signButton);
keyButton[i].setFont(new java.awt.Font("Dialog",1,16));
p1.add(keyButton[i]);
}
contentPane.add(outputField,java.awt.BorderLayout.NORTH);
contentPane.add(p1,java.awt.BorderLayout.CENTER);
this.show();
}
public void registEvent(){
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){System.exit(0);}
});
for(int i=0;i<keyButton.length;i++){
keyButton[i].addActionListener(new keyButtonHandler());
}
signButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
outputField.setText(String.valueOf(model.toSign(Double.parseDouble(outputField.getText()))));
}
});
deleteButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
outputField.setText(model.backSpace(outputField.getText()));
if(outputField.getText().equals("0"))firstInput=true;
}
});
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
firstInput=true;
outputField.setText("0");
model.reset();
}
});
}

class keyButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent ae){
char actionCommand=ae.getActionCommand().charAt(0);
switch(actionCommand){
case '1':case '2':case '3':case '4':case '5': case '6':case '7':case '8':case '9':case '0':case '.':
if(firstInput){outputField.setText(String.valueOf(actionCommand));firstInput=false;}
else outputField.setText(outputField.getText()+actionCommand);
break;
case '+':case '-':case '*':case '/':
if(firstInput)model.setOperate(actionCommand);
else{
model.setOperateNumber(Double.parseDouble(outputField.getText()));
model.calculating();
model.setOperate(actionCommand);
outputField.setText(String.valueOf(model.getResult()));
firstInput=true;
}
break;
case '=':
if(firstInput){
model.calculating();
outputField.setText(String.valueOf(model.getResult()));
}
else{
model.setOperateNumber(Double.parseDouble(outputField.getText()));
model.calculating();
outputField.setText(String.valueOf(model.getResult()));
firstInput=true;
}
} } }

public static void main(String args[]){
new Calculate_GUI().registEvent();
}
}

2 文件名 Calculate_Model.java

public class Calculate_Model{
private double result;
private double operateNumber;
private char operate;

public Calculate_Model(){
result=0;
operateNumber=0;
operate=' ';
}

public void setResult(double n){
result=n;
}
public void setOperate(char o){
operate=o;
}
public void setOperateNumber(double n){
operateNumber=n;
}
public double getResult(){
return result;
}
public void calculating(){
switch(operate){
case ' ':result=operateNumber;break;
case '+':result+=operateNumber;break;
case '-':result-=operateNumber;break;
case '*':result*=operateNumber;break;
case '/':result/=operateNumber;break;
}
}
public void reset(){
result=0;
operateNumber=0;
operate=' ';
}
public double toSign(double n){
return 0-(n-0);
}
public String backSpace(String n){
String reResult="0";
try{
reResult=n.substring(0,n.length()-1);
Double.parseDouble(reResult);
}
catch(NumberFormatException e){reResult="0";}
return reResult;
}
}

两个文件同一目录;

javac Calculate_GUI.java

javac Calculate_Model.java

java Calculate_GUI
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式