Topic: 关于计算器显示的问题(菜菜的问题)

  Print this page

1.关于计算器显示的问题(菜菜的问题) Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-09-28 21:12

老师布置了一个计算器的作业,,但是我做出来的text的显示是在最左,,有什么办法可以使它从最右开始显示,,谢谢大虾们了!!!

2.Re:关于计算器显示的问题(菜菜的问题) [Re: xuxiang84] Copy to clipboard
Posted by: 三角梅
Posted on: 2004-09-29 15:13

把源程序贴出来

3.Re:关于计算器显示的问题(菜菜的问题) [Re: xuxiang84] Copy to clipboard
Posted by: eric_dasini
Posted on: 2004-09-29 16:14

text控件没有 右对齐的属性.
用label吧

4.Re:关于计算器显示的问题(菜菜的问题) [Re: xuxiang84] Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-10-03 18:53

不会吧,,应该有的啊!?老师说有办法的,一个办法是创建N个TEXT,,从右往左移,,还有种方法我就不晓得了,,希望知道的说一下

5.Re:关于计算器显示的问题(菜菜的问题) [Re: xuxiang84] Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-10-03 18:54


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.lang.String;
import java.lang.Integer;
public class jisuanqi extends Applet implements ActionListener
{
static String xianshi;
static float caozuoshu,jieguo;
int biaoji,yunsuanfu,zhengfuhao;

TextField text;
Button n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,point,minus,plus,subtract,multiply,divild,equal,back,ce;

public void init()
{
xianshi=new String("");
caozuoshu=0;
jieguo=0;
biaoji=-1;
yunsuanfu=0;
zhengfuhao=0;


text=new TextField(11);
text.setEditable(false);
text.setText("");

n1=new Button("1");
n2=new Button("2");
n3=new Button("3");
n4=new Button("4");
n5=new Button("5");
n6=new Button("6");
n7=new Button("7");
n8=new Button("8");
n9=new Button("9");
n0=new Button("0");
point=new Button(".");
minus=new Button("-/+");
plus=new Button("+");
subtract=new Button("-");
multiply=new Button("*");
divild=new Button("/");
equal=new Button("=");
back=new Button("back");
ce=new Button("ce");

add(text);
add(n1);
add(n2);
add(n3);
add(n4);
add(n5);
add(n6);
add(n7);
add(n8);
add(n9);
add(n0);
add(point);
add(minus);
add(plus);
add(subtract);
add(multiply);
add(divild);
add(equal);
add(back);
add(ce);

n1.addActionListener(this);
n2.addActionListener(this);
n3.addActionListener(this);
n4.addActionListener(this);
n5.addActionListener(this);
n6.addActionListener(this);
n7.addActionListener(this);
n8.addActionListener(this);
n9.addActionListener(this);
n0.addActionListener(this);
point.addActionListener(this);
minus.addActionListener(this);
plus.addActionListener(this);
subtract.addActionListener(this);
multiply.addActionListener(this);
divild.addActionListener(this);
equal.addActionListener(this);
back.addActionListener(this);
ce.addActionListener(this);

}


public void actionPerformed(ActionEvent e)
{
if(e.getSource()==n1)
{
if(biaoji==1){xianshi="";biaoji=0;}
xianshi=xianshi+"1";
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==n2)
{
if(biaoji==1){xianshi="";biaoji=0;}
xianshi=xianshi+"2";
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==n3)
{
if(biaoji==1){xianshi="";biaoji=0;}
xianshi=xianshi+"3";
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==n4)
{
if(biaoji==1){xianshi="";biaoji=0;}
xianshi=xianshi+"4";
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==n5)
{
if(biaoji==1){xianshi="";biaoji=0;}
xianshi=xianshi+"5";
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==n6)
{
if(biaoji==1){xianshi="";biaoji=0;}
xianshi=xianshi+"6";
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==n7)
{
if(biaoji==1){xianshi="";biaoji=0;}
xianshi=xianshi+"7";
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==n8)
{
if(biaoji==1){xianshi="";biaoji=0;}
xianshi=xianshi+"8";
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==n9)
{
if(biaoji==1){xianshi="";biaoji=0;}
xianshi=xianshi+"9";
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==n0)
{
if(biaoji==1){xianshi="";biaoji=0;}
xianshi=xianshi+"0";
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==point)
{if(biaoji==1){xianshi="";biaoji=0;}
if(xianshi.indexOf('.')==-1)
{xianshi=xianshi+".";}
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}
else if(e.getSource()==minus)
{if(biaoji==1){xianshi="";biaoji=0;}
if(xianshi.indexOf('-')==-1)
{xianshi="-"+xianshi;}
else
{xianshi=xianshi.substring(1,xianshi.length());}
caozuoshu=Float.parseFloat(xianshi);
if(biaoji==-1){jieguo=caozuoshu;biaoji=-1;}
text.setText(xianshi);
}

else if(e.getSource()==plus)
{caozuoshu=0;biaoji=1;yunsuanfu=1;
}
else if(e.getSource()==subtract)
{caozuoshu=0;biaoji=1;yunsuanfu=2;

}
else if(e.getSource()==multiply)
{
caozuoshu=0;biaoji=1;yunsuanfu=3;
}
else if(e.getSource()==divild)
{
caozuoshu=0;biaoji=1;yunsuanfu=4;
}
else if(e.getSource()==equal)
{if(yunsuanfu==1){jieguo=jieguo+caozuoshu;biaoji=1;text.setText(xianshi=String.valueOf(jieguo));}
if(yunsuanfu==2){jieguo=jieguo-caozuoshu;biaoji=1;text.setText(xianshi=String.valueOf(jieguo));}
if(yunsuanfu==3){jieguo=jieguo*caozuoshu;biaoji=1;text.setText(xianshi=String.valueOf(jieguo));}
if(yunsuanfu==4){jieguo=jieguo/caozuoshu;biaoji=1;text.setText(xianshi=String.valueOf(jieguo));}
}

else if(e.getSource()==back)
{
xianshi=xianshi.substring(0,xianshi.length()-1);
caozuoshu=Integer.parseFlaot(xianshi);

text.setText(xianshi);}
else if(e.getSource()==ce)
{
xianshi=new String("");
caozuoshu=0;
jieguo=0;
biaoji=-1;
yunsuanfu=0;
text.setText(xianshi);}

6.Re:关于计算器显示的问题(菜菜的问题) [Re: xuxiang84] Copy to clipboard
Posted by: Jason_zhu85
Posted on: 2004-10-03 22:41

好像是core java的一个例子~~~

7.Re:关于计算器显示的问题(菜菜的问题) [Re: xuxiang84] Copy to clipboard
Posted by: hvbird
Posted on: 2004-10-05 00:04

把输入数字的文本框设置成右对齐可以吗

8.Re:关于计算器显示的问题(菜菜的问题) [Re: xuxiang84] Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-10-05 03:38

“把输入数字的文本框设置成右对齐可以吗 ”
就是这个问题啊 ,,楼上的知道的话请说出来

9.Re:关于计算器显示的问题(菜菜的问题) [Re: xuxiang84] Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-10-09 16:49

找到答案了 ,,是一个函数:text.setHorizontalAlignment(JTextField.RIGHT);


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923