java的计算器页面代码 java实现计算器界面( 四 )


s="";
}
else if(command.equals("-")){
sum1=Integer.parseInt(t.getText());
b=t.getText();
s="";
}
else if(command.equals("*")){
sum2=Integer.parseInt(t.getText())*sum2;
t.setText(String.valueOf(sum2));
c=t.getText();
s="";
}
else if(command.equals("/")){
sum3=Integer.parseInt(t.getText());
d=t.getText();
s="";
}
else if(command.equals("=")){
if(a!=null){
t.setText(String.valueOf(sum+tmp));
}
if(b!=null){
t.setText(String.valueOf(sum1-tmp));
}
if(c!=null){
t.setText(String.valueOf(sum2*tmp));
}
try{
if(d!=null){
t.setText(String.valueOf(sum3/tmp));
}
}
catch(ArithmeticException a){
t.setText("除数不能为零");
}
}
else if(command.equals("空格")){
sum=0;
a=null;
b=null;
c=null;
d=null;
sum1=0;
sum2=1;
sum3=1;
tmp=0;
t.setText("0");
s="";
}
else{
s=s+command;
t.setText(s);
tmp=Integer.parseInt(s);
}
}
}
}
//按钮可以自己美化一下希望可以帮到你
高分求 用JAVA编写计算器界面(只要界面)代码!package importantpg;
import java.awt.*;
import java.awt.event.*;
public class jisuanqi
{
TextArea tf=new TextArea(3,50 );
Button lb=new Button("clear");
Panel p1=new Panel();
Panel p2=new Panel();
public jisuanqi()
{Frame f=new Frame("计算器");
Panel p=new Panel();
f.setBackground(Color.white);
f.setSize(500, 1500);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e){System.exit(1);}
});
Button[] b=new Button[18];b[10]=new Button("-");b[15]=new Button("(");
b[11]=new Button("+");b[12]=new Button("*");b[16]=new Button(")");
b[13]=new Button("/");b[14]=new Button("=");b[17]=new Button("退格");
for(int i=0;i=9;++i)
{
b[i]=new Button(""+i);
}
p.setLayout(new GridLayout(6,3));
avent aa=new avent();
lb.addActionListener(aa);
for(int i=0;i18;++i)
{
p.add(b[i]);b[i].addActionListener(aa);
}
p1.setLayout(new BorderLayout());
p1.add(lb,"North");p1.add(tf,"South");f.add(p1, "South");
f.add(p,"Center"); f.pack();
}//构造方法
public static void main(String args[])
{
new jisuanqi();
}
classavent implements ActionListener//事件体
{
public void actionPerformed(ActionEvent e)
{
String s=null; String in="";
s=e.getActionCommand();
if (s!="="s!=nulls!="clear"s!="退格")
{
tf.append(s);
in+=s; s=null;
}
else if(s=="clear")
{
tf.replaceRange("",0,1000);
}else if(s=="=")
{
tf.append("="+jisuan(tf.getText()));
}
else if(s=="退格") {}
}///////////////////////////////////////////////事件处理方法
}
///////////////////////////////////////计算过程//////////////
static String jisuan(String s)
{
String hs= "";
char[] ch = new char[s.length()];
int top = -1;
for (int i = 0; is.length(); i++)
{
char rsc = s.charAt(i);
switch(rsc)
{
case ' ' : break;
case '+' : //操作符
case '-' :
while (top = 0) //栈不为空时
{
char pc = ch[top--]; //pop Operator
if (pc == '(')
{
ch[++top] = pc; //push Operator
break;
}
else{hs = hs + pc;}
}
ch[++top] = rsc; //push Operator
hs += " ";break;
case '*' : //操作符
case '/' :
while (top = 0) //栈不为空时
{
char c = ch[top--]; //pop Operator
if (c== '(')

推荐阅读