用java做一个简单的计算器

huangnengwu123 2011-11-10 07:06:27
我是java初学者,尝试做一个简单的计算器,但运行会产生异常,而且得不到运算结果,请各位高手指点...程序如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
class 布局 extends Frame implements ActionListener
{ TextField text;
Button button1,button2,button3,button4;
Button button5,button6,button7,button8;
Button button9,button10,button11,button12;
Button button13,button14,button15,button16;
Panel panel1,panel2;
Box boxv1,boxv2,boxv3,boxv4;
Box boxh1,boxh2,boxh3,boxh4;
Font font=new Font("黑体",Font.BOLD,15);
布局()
{ setTitle("计算器");
text=new TextField("0",10);
Button button1=new Button("7");
button2=new Button("4");
button3=new Button("1");
button4=new Button("0");
button5=new Button("8");
button6=new Button("5");
button7=new Button("2");
button8=new Button("c");
button9=new Button("9");
button10=new Button("6");
button11=new Button("3");
button12=new Button("=");
button13=new Button("+");
button14=new Button("-");
button15=new Button("*");
button16=new Button("/");
button1.setForeground(Color.blue);
button2.setForeground(Color.blue);
button3.setForeground(Color.blue);
button4.setForeground(Color.blue);
button5.setForeground(Color.blue);
button6.setForeground(Color.blue);
button7.setForeground(Color.blue);
button8.setForeground(Color.red);
button9.setForeground(Color.blue);
button10.setForeground(Color.blue);
button11.setForeground(Color.blue);
button12.setForeground(Color.red);
button13.setForeground(Color.red);
button14.setForeground(Color.red);
button15.setForeground(Color.red);
button16.setForeground(Color.red);
button1.setFont(font);
button2.setFont(font);
button3.setFont(font);
button4.setFont(font);
button5.setFont(font);
button6.setFont(font);
button7.setFont(font);
button8.setFont(font);
button9.setFont(font);
button10.setFont(font);
button11.setFont(font);
button12.setFont(font);
button13.setFont(font);
button14.setFont(font);
button15.setFont(font);
button16.setFont(font);
Panel panel1=new Panel();
Panel panel2=new Panel();
panel1.setBackground(Color.blue);
panel2.setBackground(Color.blue);
boxv1=Box.createVerticalBox();
boxv2=Box.createVerticalBox();
boxv3=Box.createVerticalBox();
boxv4=Box.createVerticalBox();
boxv1.add(button1);
boxv1.add(Box.createVerticalStrut(8));
boxv1.add(button2);
boxv1.add(Box.createVerticalStrut(8));
boxv1.add(button3);
boxv1.add(Box.createVerticalStrut(8));
boxv1.add(button4);
boxv2.add(button5);
boxv2.add(Box.createVerticalStrut(8));
boxv2.add(button6);
boxv2.add(Box.createVerticalStrut(8));
boxv2.add(button7);
boxv2.add(Box.createVerticalStrut(8));
boxv2.add(button8);
boxv3.add(button9);
boxv3.add(Box.createVerticalStrut(8));
boxv3.add(button10);
boxv3.add(Box.createVerticalStrut(8));
boxv3.add(button11);
boxv3.add(Box.createVerticalStrut(8));
boxv3.add(button12);
boxv4.add(button13);
boxv4.add(Box.createVerticalStrut(8));
boxv4.add(button14);
boxv4.add(Box.createVerticalStrut(8));
boxv4.add(button15);
boxv4.add(Box.createVerticalStrut(8));
boxv4.add(button16);
boxh1=Box.createVerticalBox();
boxh2=Box.createVerticalBox();
boxh3=Box.createVerticalBox();
boxh4=Box.createVerticalBox();
boxh1.add(boxv1);
boxh2.add(boxv2);
boxh3.add(boxv3);
boxh4.add(boxv4);
panel2.add(boxh1);
panel2.add(boxh2);
panel2.add(boxh3);
panel2.add(boxh4);
panel1.add(text);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button10.addActionListener(this);
button11.addActionListener(this);
button12.addActionListener(this);
button13.addActionListener(this);
button14.addActionListener(this);
button15.addActionListener(this);
button16.addActionListener(this);
add(panel1,BorderLayout.NORTH);
add(panel2,BorderLayout.CENTER);
setBounds(100,100,250,250);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{ int s=0,R=0;//s为文本框中的整型数据,R为运算后的整型结果
int op1=0,op2=0;//op1为第一个操作数,op2为第二个操作数
char op=' ';//op为操作符
String m=text.getText();//向文本框中取字符
String str1=String.valueOf(R);
if(e.getSource()==button1)
{ int n=Integer.parseInt(m);
s=n*10+7;
String str=String.valueOf(s);
text.setText(str);
}
if(e.getSource()==button2)
{ int n=Integer.parseInt(m);
s=n*10+4;
String str=String.valueOf(s);
text.setText(str);
}
if(e.getSource()==button3)
{
int n=Integer.parseInt(m);
s=n*10+1;
String str=String.valueOf(s);
text.setText(str);
}
if(e.getSource()==button4)
{
int n=Integer.parseInt(m);
s=n*10+0;
String str=String.valueOf(s);
text.setText(str);
}
if(e.getSource()==button5)
{
int n=Integer.parseInt(m);
s=n*10+8;
String str=String.valueOf(s);
text.setText(str);
}
if(e.getSource()==button6)
{
int n=Integer.parseInt(m);
s=n*10+5;
String str=String.valueOf(s);
text.setText(str);
}
if(e.getSource()==button7)
{
int n=Integer.parseInt(m);
s=n*10+2;
String str=String.valueOf(s);
text.setText(str);
}
if(e.getSource()==button8)
{ text.setText("0");
}
if(e.getSource()==button9)
{
int n=Integer.parseInt(m);
s=n*10+9;
String str=String.valueOf(s);
text.setText(str);
}
if(e.getSource()==button10)
{
int n=Integer.parseInt(m);
s=n*10+6;
String str=String.valueOf(s);
text.setText(str);
}
if(e.getSource()==button11)
{
int n=Integer.parseInt(m);
s=n*10+3;
String str=String.valueOf(s);
text.setText(str);
}
if(e.getSource()==button12)
{ op2=Integer.parseInt(m);
String r;
switch(op)
{ case '+':
R=op1+op2;
r=String.valueOf(R);
text.setText(r); break;
case '-':
R=op1-op2;
r=String.valueOf(R);
text.setText(r); break;
case '*':
R=op1*op2;
r=String.valueOf(R);
text.setText(r); break;
case '/':
R=op1/op2;
r=String.valueOf(R);
text.setText(r); break;
}
}
if(e.getSource()==button13)
{ op1=Integer.parseInt(m);
text.setText(null);
op='+';
}
if(e.getSource()==button14)
{ op1=Integer.parseInt(m);
text.setText(null);
op='-';
}
if(e.getSource()==button15)
{ op1=Integer.parseInt(m);
text.setText(null);
op='*';
}
if(e.getSource()==button16)
{ op1=Integer.parseInt(m);
text.setText(null);
op='/';
}
}
}
public class 计算器
{ public static void main(String args[])
{ 布局 win=new 布局();
}
}


...全文
714 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
非凡90 2011-11-11
  • 打赏
  • 举报
回复
首先要说的是,你没有考虑异常处理机制,try{}catch(),你有没有想到,当用进行除法操作时,当用户输入的分母为0时,那输出的结果会是怎样呀?我看了一下你的代码,好像没有一处进行过异常处理吧,建议你好好看看异常处理这章吧,希望我的回答能给你带来些帮助,祝你好运!
_啊Y 2011-11-11
  • 打赏
  • 举报
回复
我晕了这代码.....
难道你就不能用数组吗?
Edward1688 2011-11-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 michaelbest1 的回复:]
去debug吧。此乃神器也
[/Quote]
请问什么是debug啊?谢谢!
debug就是单步吗?
huangnengwu123 2011-11-10
  • 打赏
  • 举报
回复
空指针异常怎么解决: public void actionPerformed(ActionEvent e)
{ int s=0,R=0;//s为文本框中的整型数据,R为运算后的整型结果
int op1=0,op2=0;//op1为第一个操作数,op2为第二个操作数
char op=' ';//op为操作符
String m=text.getText();//向文本框中取字符
String str1=String.valueOf(R);
我用op1保存了第一个数据,又在按下‘=’之前保存第二个数据op2,这样有什么问题吗,改怎么改进
liushuaibaicai 2011-11-10
  • 打赏
  • 举报
回复
现在都流行用汉语了么。。。。。
public class 计算器
孟祥月 2011-11-10
  • 打赏
  • 举报
回复
单步调试吧
Michaelbest1 2011-11-10
  • 打赏
  • 举报
回复
去debug吧。此乃神器也
24K純帥 2011-11-10
  • 打赏
  • 举报
回复
程序是可以执行的,我建议用个变量把每次操作的结果记录下来,按=的时候再处理之前的情况,现在LZ的情况只能按一次,再按第二次就报空指针异常
dracularking 2011-11-10
  • 打赏
  • 举报
回复
异常是什么 只要你会调试,这种问题容易解决
  • 打赏
  • 举报
回复
重贴一遍,嘿嘿,罚你重贴代码,这样的代码写的怎么样不说,贴的都不怎么样。
huangnengwu123 2011-11-10
  • 打赏
  • 举报
回复
本人程序可能有点欠简洁,请高手提出建议,不胜感激...

62,614

社区成员

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

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