JAVA程序编的简单计算器程序??
1 文件名:Calculate_GUI。java
import javax。swing。*;
import t。Container;
import t。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;
priva...全部
1 文件名:Calculate_GUI。java
import javax。swing。*;
import t。Container;
import t。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);
tText("0");
tEditable(false);
contentPane= tContentPane();
tSize(250,250);
tLayout(new t。
GridLayout(4,5));
for(int i=0;i setFont(new t。Font("Dialog",1,16));
d(keyButton[i]);
}
d(outputField, t。BorderLayout。
NORTH);
d(p1, t。BorderLayout。CENTER);
ow();
}
public void registEvent(){
dWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){System。
exit(0);}
});
for(int i=0;i equals("0"))firstInput=true;
}
});
dActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
firstInput=true;
tText("0");
set();
}
});
}
class keyButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent ae){
char actionCommand= tActionCommand()。
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){ tText( lueOf(actionCommand));firstInput=false;}
else tText( tText()+actionCommand);
break;
case '+':case '-':case '*':case '/':
if(firstInput) tOperate(actionCommand);
else{
tOperateNumber( rseDouble( tText()));
lculating();
tOperate(actionCommand);
tText( lueOf( tResult()));
firstInput=true;
}
break;
case '=':
if(firstInput){
lculating();
tText( lueOf( tResult()));
}
else{
tOperateNumber( rseDouble( tText()));
lculating();
tText( lueOf( tResult()));
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= bstring(0,n。
length()-1);
rseDouble(reResult);
}
catch(NumberFormatException e){reResult="0";}
return reResult;
}
}
两个文件同一目录;
javac Calculate_GUI。
java
javac Calculate_Model。java
java Calculate_GUI。收起