展开全部
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(); //它允许编辑单行文本
outputField.setText("0");
outputField.setEditable(false); //指示32313133353236313431303231363533e78988e69d8331333262353366此 TextComponent 是否应该为可编辑的
contentPane=this.getContentPane(); //返回此窗体的 contentPane 对象;
this.setSize(250,250);
p1.setLayout(new java.awt.GridLayout(4,5)); //创建具有4行5列的布局
for(int i=0;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",3,15));//设置字体Dialog字体名,样式,大小
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[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();
}
}
/******************************************
*****计算的模块****************************/
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是private的
{
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) //对backSpace设置
{
String reResult="0";
try
{
reResult=n.substring(0,n.length()-1); //从第一个开始取前n-1个
Double.parseDouble(reResult); //将其转化为Double型
}
catch(NumberFormatException e)
{
reResult="0";
}
return reResult;
}
}
你自己看看...可以回删
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起