使用java swing的计算器程序 [英] calculator program using java swing

查看:64
本文介绍了使用java swing的计算器程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Awt implements ActionListener
{
	JFrame f;
	JButton b1,b2,b3,b4,b5;
	JTextField t1,t2,t3;
	JLabel l,l1;
	Awt()
	{
		f=new JFrame("Listener");
		t1=new JTextField("             ");
		t2=new JTextField("             ");
		t3=new JTextField("             ");
		b1=new JButton("Add");
		b2=new JButton("Sub");
		b3=new JButton("Mul");
		b4=new JButton("Div");
		l=new JLabel();
		l1=new JLabel();
	}
	public void awt1()
	{
		f.setLayout(new GridLayout(3,2));
		f.setVisible(true);
		f.add(t1);
		f.add(t2);
		f.add(t3);
		f.add(b1);
 		f.add(b2);
		f.add(b3);
		f.add(b4);
		f.add(l);
		f.add(l1);
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		f.pack();
	}
	public void actionPerformed(ActionEvent e)
	{
		String s=new String(e.getActionCommand());
		l.setText(s);
		if((s).equals("Add"))
		{
			int  a=Integer.parseInt(t1.getText());
			int b=Integer.parseInt(t2.getText());
			Integer c=a+b;
                        t3.setText(c.toString());

		}
		else if((s).equals("Sub"))
		{
			int a=Integer.parseInt(t1.getText());
			int b=Integer.parseInt(t2.getText());
			Integer c=a-b;
			t3.setText(c.toString());
	
		}
		else if((s).equals("Mul"))
		{
			int a=Integer.parseInt(t1.getText());
			int b=Integer.parseInt(t2.getText());
			Integer c=a*b;
			t3.setText(c.toString());
		}
	}
	
	public static void main(String args[])
	{
		Awt a=new Awt();
		a.awt1();
	}
}





嗨..

此代码成功运行但是我输入了输入并按下一个按钮,发生了数字格式异常。我不知道我在哪里犯了错误,请在此代码中发现该逻辑错误。

请帮助我。



Hi..
This code runs successfully but when I entered input and press a button a number format exception occurred. I don't know where I made a mistake, please spot that logical error in this code.
Please help me.

推荐答案

getText()返回的字符串包含 parseInt()对象的尾随空格。将代码更改为:

The strings returned by getText() contain trailing spaces which parseInt() objects to. Change your code to something like:
String stemp = t1.getText().trim();
int  a=Integer.parseInt(stemp);


getText()返回的字符串包含parseInt()对象的尾部空格。将代码更改为:



String stemp = t1.getText()。trim();

int a = Integer.parseInt (stemp);





还要对所有getText()方法进行验证,这意味着只有从getText返回的值才能继续进行()不为null或不为空。
The strings returned by getText() contain trailing spaces which parseInt() objects to. Change your code to something like:

String stemp = t1.getText().trim();
int a=Integer.parseInt(stemp);


Also put the validations for all the getText() methods, meaning proceed further only if the value returning from getText() is not null or not empty.


作为次要建议。为什么用这么多空格字符初始化文本字段?如果你想让文本字段为空,那么就像这样初始化它们:



As a minor advice. Why have you initialized your text fields with so many space characters? If you want the text fields to be empty, the just initialize them like this:

JTextField myTextField=new JTextField("");





因此编译器知道文本字段必须为空。通过这样做,在解析文本字段中的值时,您将不需要trim()方法。



并且在actionPerformed()方法中获取事件的名称,而不是我只会使用:



Thus the compiler knows that text fields must be empty. And by doing so, you won't be needing trim() method when parsing the value from the text fields.

And also in the actionPerformed() method with getting the name of the event, rather i would just use:

   if(e.getSource()==bt1){

     //do something -> add, mul etc.
}





除此之外,代码似乎还不错。



祝你好运。



Other than that, the code seems fine.

Best regards.


这篇关于使用java swing的计算器程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆