社区
Eclipse
帖子详情
计算器程序的输入文本框的小数点重复输入的问题
jayhai
2010-08-21 08:49:25
本人在做一个计算器的程序 我想要在文本框中输入小数点“.”一次以后 下次再按小数点 文本框中不再显示点
也就是小数点在你输到文本框的一个数时 只出现一次
我现在做不出来 按几次就在后面加几次 一按运算按钮就报错了
忘高手指教
...全文
371
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(".");
}
});
简单
计算器
(有
小数点
功能)
在IT领域,创建一个“简单
计算器
(有
小数点
功能)”涉及到的编程技术主要包括用户界面设计、事件处理、算术运算以及浮点数处理。下面将详细解释这些知识点: 1. **用户界面设计**:
计算器
通常有一个直观的图形用户...
用JAVA编写
计算器
程序
(模拟Windows
计算器
)
### 使用Java编写的
计算器
程序
(模拟Windows
计算器
) #### 一、项目概述 本文档介绍了一个使用Java语言编写的简易
计算器
程序
,该
程序
旨在模仿Windows系统的内置
计算器
功能。通过本项目,用户可以执行基本的数学...
Java版简易
计算器
程序
设计
### Java版简易
计算器
程序
设计知识点解析 #### 一、项目概述 本项目为一个简易
计算器
的设计与实现,采用Java语言开发。该
计算器
具备基本的算术运算功能,并且集成了记忆存储功能,能够满足日常计算的基本需求。项目...
简单的java
计算器
程序
【Java简单
计算器
程序
详解】 Java是一种广泛使用的编程语言,它以其跨平台的特性以及丰富的类库而备受青睐。在这个“简单的java
计算器
程序
”中,我们可以看到一个基础的命令行
计算器
的实现,它提供了基本的四则运算...
简易
计算器
程序
(C#代码编写)
总的来说,【简易
计算器
程序
(C#代码编写)】是一个很好的学习C#基础和GUI编程的实践项目,它涉及到用户
输入
处理、事件驱动编程、错误处理等多个关键概念,是初学者和进阶者提升技能的好途径。通过这样的项目,开发者...
Eclipse
58,448
社区成员
49,461
社区内容
发帖
与我相关
我的任务
Eclipse
Java Eclipse
复制链接
扫一扫
分享
社区描述
Java Eclipse
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章