计算器程序的输入文本框的小数点重复输入的问题

jayhai 2010-08-21 08:49:25
本人在做一个计算器的程序 我想要在文本框中输入小数点“.”一次以后 下次再按小数点 文本框中不再显示点
也就是小数点在你输到文本框的一个数时 只出现一次
我现在做不出来 按几次就在后面加几次 一按运算按钮就报错了
忘高手指教
...全文
345 8 打赏 收藏 转发到动态 举报
写回复
用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(".");
}
});
目 录 一、课设任务及要求 1 二、需求分析 2 三、设计思路 3 四、详细设计 5 五、运行调试与分析讨论 9 六、设计体会与小结 14 七、参考文献 15 附录 16 中文摘要 Java是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台的总称。用Java实现 的HotJava浏览器,显示了Java的魅力:跨平台、动感的Web、Internet计算。从此,Ja va被广泛接受并推动了Web的迅速发展,常用的浏览器现在均支持Java applet。另一方面,Java技术也不断更新。Java平台由Java虚拟机和Java 应用编程接口构成。Java 应用编程接口为Java应用提供了一个独立于操作系统的标准接口,可分为基本部分和扩 展部分。在硬件或操作系统平台上安装一个Java平台之后,Java应用程序就可运行。现 在Java平台已经嵌入了几乎所有的操作系统。这样Java程序可以只编译一次,就可以在 各种系统中运行。 Java分为三个体系J2SE,J2EE,J2ME。 说起计算器,值得我们骄傲的是,最早的计算工具的诞生地是中国。 在17世纪初,西方国家的计算工具才有了较大的发展,英国数学家纳皮尔发明的"纳 皮尔算筹",英国牧师奥却德发明了圆柱型对数计算尺,这种计算尺不仅能做加减乘除、 乘方、开方运算,甚至可以计算三角函数,指数函数和对数函数,这些计算工具不仅带 动了计算器的发展,也为现代计算器发展奠定了良好的基础,进而成为了现代社会应用 广泛的计算工具。 关键词:java Java平台 计算器 课设任务及要求 1.课设任务 这次课程设计选择的题目为设计一个图形界面(GUI)的计算器应用程序,完成简单 的算术运算。 这次课程设计的基本要求为设计的计算器应用程序可以完成加法、减法、乘法、除 法和取余运算,且有小数点、正负号、求倒数、退格和清零功能。拓展功能根据自己的 能力添加。 这次课程设计的我选择添加的拓展功能为开平方根,平方,立方,判断素数,求lo g的功能。 本程序主要练习使用布局管理器设计一个计算器的界面,并练习使用事件监听器处 理数据的输入,并完成相关的计算。数据和运算符号的存储采用动态链表这种数据结构 实现。 这次课程设计选择的Java运行环境为: Windows XP sp3 +Eclipse+JDK 1.6 二、需求分析 1.系统功能需求分析 计算器是现在一个普遍应用的工具,能够解决许多人所无法计算的数据,节省大量 宝贵的时间。 2.系统功能分析 为了实现计算器系统的功能.主要有二个功能模块:输入、输出。 3.系统设计原则 基于计算器系统要具有适用性广、操作简便等特点.本系统预计要达到以下几个目标 : (1)、满足以上的功能要求; (2)、能够运行在常见的计算机及其配置上; 三、设计思路 1.关于布局问题 本次课程设计程序继承来自框架类(Frame),总体布局上选用布局管理器BorderLayout: (1)将单行文本框加入到"North"区域 (2)将面板panel加入到"Center"区域,同时panel包含了各种数字按钮和符号按钮。 面板panel采用Girdlayout布局,选用5行*5列,将各种按钮添加到面板panel,并增加按 钮监听事件。 布局完成后的效果图如下: 2.关于数据存储问题 计算器完成的是一个数学表达式,本次课程设计我选用的是使用链表(Linkedlist类 )来存储数字和运算符号。程序运行后,输入的所有数字及运算符号都全部存储在链表中 ,待最后运算时,再一一求出来进行计算。 3.关于事件监听的处理问题 计算器的各种按钮都需要一个对象来进行监视,以便对发生的事件做出处理。计算器 的各种按钮通过调用相应的方法将某个对象作为自己的监视器。 例如计算器中的数字按钮,其方法为: AddActionListener(监视器); 对于获取了监视器的数字按钮,通过相应的操作就会导致事件的发生,并通知监视器 ,监视器就会做出相应的处理。 四、详细设计 1.计算器系统主要功能模块 (1)、系统主要模块实现的功能 系统输入模块实现数字以及计算符号输入的功能,输出模块的结果在文本框中实现显 示。 (2)、系统输入窗体实现的效果 系统输入窗体设计效果如图所示: 上图为按数字键1234567890后,在文本框中的显示 (3)、系统主要模块功能描述 功能描述: 菜单项"计算器"主要服务于使用者.它包含了"输入"、"输出"、 两个功能。 输入功能:当使用者将数字输入后,会出现数字的显示;当使用者将计算符号输入时 候会有计算符号的录入。 输出功能:点击"输出"选项后.可实现计算的结果。 2.系统的实现 (1) 系统源文件类之间的关系 计算器系统共有3个java源文件:Calcul

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧