社区
Eclipse
帖子详情
计算器程序的输入文本框的小数点重复输入的问题
jayhai
2010-08-21 08:49:25
本人在做一个计算器的程序 我想要在文本框中输入小数点“.”一次以后 下次再按小数点 文本框中不再显示点
也就是小数点在你输到文本框的一个数时 只出现一次
我现在做不出来 按几次就在后面加几次 一按运算按钮就报错了
忘高手指教
...全文
366
8
打赏
收藏
计算器程序的输入文本框的小数点重复输入的问题
本人在做一个计算器的程序 我想要在文本框中输入小数点“.”一次以后 下次再按小数点 文本框中不再显示点 也就是小数点在你输到文本框的一个数时 只出现一次 我现在做不出来 按几次就在后面加几次 一按运算按钮就报错了 忘高手指教
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
8 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
jayhai
2010-08-22
打赏
举报
回复
5楼的方法早就试过 对于我写的程序是行不通的
luyun2011
2010-08-22
打赏
举报
回复
给你一个完整的,自己做的,不是很完美
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame {
String x="",y="",z="";
JButton bt1; JButton bt2;
JButton bt3; JButton bt4;
JButton bt5; JButton bt6;
JButton bt7; JButton bt8;
JButton bt9; JButton bt0;
JButton add; JButton sub;
JButton mul; JButton div;
JButton reset; JButton ok;
JButton cancel; JButton dot;
JTextField tf;
JPanel p1; JPanel p2; JPanel p;
public Calculator(String title){
super(title);
this.init();
//this.setLayout(new GridLayout(2,1));
this.setBounds(300, 300, 300, 300);
this.setVisible(true);
}
private void init(){
bt1=new JButton("1");
bt2=new JButton("2");
bt3=new JButton("3");
bt4=new JButton("4");
bt5=new JButton("5");
bt6=new JButton("6");
bt7=new JButton("7");
bt8=new JButton("8");
bt9=new JButton("9");
bt0=new JButton("0");
dot=new JButton(".");
add=new JButton("+");
sub=new JButton("-");
mul=new JButton("*");
div=new JButton("/");
reset=new JButton("清除");
ok=new JButton("=");
cancel=new JButton("撤消");
tf=new JTextField(25);
p1=new JPanel();
p2=new JPanel();
p=new JPanel();
p1.add(tf);
//p1.setLayout(new GridLayout(2,1));
this.getContentPane().add("North",p1);
p.add(cancel);
p.add(reset);
this.getContentPane().add("Center",p);
p2.add(dot); p2.add(bt0); p2.add(ok); p2.add(add);
p2.add(bt1); p2.add(bt2); p2.add(bt3); p2.add(sub);
p2.add(bt4); p2.add(bt5); p2.add(bt6); p2.add(mul);
p2.add(bt7); p2.add(bt8); p2.add(bt9); p2.add(div);
p2.setLayout(new GridLayout(4,4));
this.getContentPane().add("South",p2);
bt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
String s=tf.getText().trim();
int index=s.indexOf(y);
int index2=s.lastIndexOf(y);
String zz=s.substring(index+1);
z=zz+"1";
tf.setText(s.substring(0, index+1)+z);
}else{
x=tf.getText().trim()+"1";
tf.setText(x);
}
}
});
bt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
String s=tf.getText().trim();
int index=s.indexOf(y);
String zz=s.substring(index+1);
z=zz+"2";
tf.setText(s.substring(0, index+1)+z);
}else{
x=tf.getText().trim()+"2";
tf.setText(x);
}
}
});
bt3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
String s=tf.getText().trim();
int index=s.indexOf(y);
String zz=s.substring(index+1);
z=zz+"3";
tf.setText(s.substring(0, index+1)+z);
}else{
x=tf.getText().trim()+"3";
tf.setText(x);
}
}
});
bt4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
// z= "4";
// tf.setText(tf.getText()+z);//z只能获得一位,2位时不行
String s=tf.getText().trim();
int index=s.indexOf(y);
String zz=s.substring(index+1);
z=zz+"4";
tf.setText(s.substring(0, index+1)+z);
}else{
x=tf.getText().trim()+"4";
tf.setText(x);
}
}
});
bt5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
// z= "5";
// tf.setText(tf.getText()+z);
String s=tf.getText().trim();
int index=s.indexOf(y);
String zz=s.substring(index+1);
z=zz+"5";
tf.setText(s.substring(0, index+1)+z);
}else{
x=tf.getText().trim()+"5";
tf.setText(x);
}
}
});
bt6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
String s=tf.getText().trim();
int index=s.indexOf(y);
String zz=s.substring(index+1);
z=zz+"6";
tf.setText(s.substring(0, index+1)+z);
}else{
x=tf.getText().trim()+"6";
tf.setText(x);
}
}
});
bt7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
String s=tf.getText().trim();
int index=s.indexOf(y);
String zz=s.substring(index+1);
z=zz+"7";
tf.setText(s.substring(0, index+1)+z);
}else{
x=tf.getText().trim()+"7";
tf.setText(x);
}
}
});
bt8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
String s=tf.getText().trim();
int index=s.indexOf(y);
String zz=s.substring(index+1);
z=zz+"8";
tf.setText(s.substring(0, index+1)+z);
}else{
x=tf.getText().trim()+"8";
tf.setText(x);
}
}
});
bt9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
String s=tf.getText().trim();
int index=s.indexOf(y);
String zz=s.substring(index+1);
z=zz+"9";
tf.setText(s.substring(0, index+1)+z);
}else{
x=tf.getText().trim()+"9";
tf.setText(x);
}
}
});
bt0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
String s=tf.getText().trim();
int index=s.indexOf(y);
String zz=s.substring(index+1);
z=zz+"0";
tf.setText(s.substring(0, index+1)+z);
}else{
x=tf.getText().trim()+"0";
tf.setText(x);
}
}
});
dot.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String s=tf.getText().trim();
if(y.equals("+")||y.equals("-")||y.equals("*")||y.equals("/")){
int index=s.indexOf(y);
String zz=s.substring(index+1);
if(zz.indexOf(".")==1) z=zz+".";
tf.setText(s.substring(0, index+1)+z);
}else{
if(s.indexOf(".")==-1)
x=s+".";
tf.setText(x);
}
}
});
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
y="+";
tf.setText(tf.getText()+y);
}
});
sub.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
y="-";
tf.setText(tf.getText()+y);
}
});
mul.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
y="*";
tf.setText(tf.getText()+y);
}
});
div.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
y="/";
tf.setText(tf.getText()+y);
}
});
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
tf.setText("");
}
});
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String s=tf.getText().trim();
tf.setText(s.substring(0, s.length()-1));
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
// int result=Integer.parseInt(z);
// tf.setText(x+z);
if(y.equals("+")){
float result=Float.parseFloat(x)+Float.parseFloat(z);
tf.setText(""+result);
}
else if(y.equals("-")){
float result=Float.parseFloat(x)-Float.parseFloat(z);
tf.setText(String.valueOf(result) );
}
else if(y.equals("*")){
float result=Float.parseFloat(x)*Float.parseFloat(z);
tf.setText(String.valueOf(result) );
}
else if(y.equals("/")){
if(z.equals("0")){
tf.setText("除数不能是0");
//throw new ArithmeticException("除数不能是0");
}
float result=Float.parseFloat(x)/Float.parseFloat(z);
tf.setText(String.valueOf(result) );
}
x="";y="";z="";
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculator cal=new Calculator("计算器");
cal.show();
}
}
luyun2011
2010-08-22
打赏
举报
回复
先用indexOf(".")判断文本框中有无小数点,有就加上
JTextArea tf=new JTextArea(25);
JButton dot=new JButton(".");
String inputstr="";
dot.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String s=tf.getText().trim();
if(s.indexOf(".")==-1){
inputstr=s+".";
}
tf.setText(inputstr);
}
});
huntor
2010-08-22
打赏
举报
回复
[Quote=引用 4 楼 jayhai 的回复:]
回去试了2楼的 但还是不行 文本框加点和加数字 我用的是按钮监听事件actionevent
[/Quote]
InputVerifier的作用是验证失败,无法使用Tab进行焦点转移。
你通过点击JButton输入的话,可以在输入“.”后禁用掉,在输入运算符后再启用输入“."的JButton。
jayhai
2010-08-22
打赏
举报
回复
回去试了2楼的 但还是不行 文本框加点和加数字 我用的是按钮监听事件actionevent
huntor
2010-08-21
打赏
举报
回复
[Quote=引用 2 楼 dr_lou 的回复:]
试试楼上的吧,或者增加key事件
[/Quote]
监听KeyEvent不如监听 DocumentEvent。
另外,可以使用一个定制的PlainDocument
dr_lou
2010-08-21
打赏
举报
回复
试试楼上的吧,或者增加key事件
huntor
2010-08-21
打赏
举报
回复
JTextField inputField = new JTextField(20);
inputField.setInputVerifier(new InputVerifier(){
public boolean verify(JComponent c){
JTextField inputTF = (JTextField)c;
String input = inputTF.getText();
return input.indexOf(".") == input.lastIndexOf(".");
}
});
简单
计算器
(有
小数点
功能)
能够进行简单的加减乘除运算,还有
小数点
功能
用JAVA编写
计算器
程序
(模拟Windows
计算器
)
用JAVA编写
计算器
程序
(模拟Windows
计算器
)
Java版简易
计算器
程序
设计
这个是我课设时候弄的,希望对大家有帮助~~~
简单的java
计算器
程序
用java写的简单
计算器
的小
程序
,是和寻xp里面的界面是一样的!
简易
计算器
程序
(C#代码编写)
简易
计算器
程序
(C#代码编写) 和勾月
计算器
相同,但采用的算法是不一样的,现在实现了加、减、乘、除功能。 QQ223857666勾月
Eclipse
58,451
社区成员
49,460
社区内容
发帖
与我相关
我的任务
Eclipse
Java Eclipse
复制链接
扫一扫
分享
社区描述
Java Eclipse
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章