如何用java编写一个简单的计算器,只要能进行最基本的加减乘除运算就可以了,本人急用,简单易懂即可,大

2024-11-03 05:25:01
推荐回答(1个)
回答1:

简单的啊,我有个自己编的完美的,不过给你改成简单的吧。有注释。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.text.*;

public class Calculator extends JFrame implements ActionListener
{
int m=0,i,n=0;
float sum=0,s1,s2,equ;
String v="";
JPanel delete_main = new JPanel();
JPanel delete = new JPanel();
JPanel buttons = new JPanel();

JTextField text = new JTextField("0");

JButton backspace = new JButton("Backspace");
JButton c = new JButton("C");
JButton ce = new JButton("CE");
JButton bt1= new JButton("1");
JButton bt2= new JButton("2");
JButton bt3= new JButton("3");
JButton bt4= new JButton("+");
JButton bt5 = new JButton("4");
JButton bt6= new JButton("5");
JButton bt7= new JButton("6");
JButton bt8= new JButton("-");
JButton bt9= new JButton("7");
JButton bt10= new JButton("8");
JButton bt11= new JButton("9");
JButton bt12= new JButton("*");
JButton bt13= new JButton("0");
JButton bt14= new JButton(".");
JButton bt15= new JButton("=");
JButton bt16= new JButton("/");

public Calculator()
{
super("简易计算器--超人不会飞荣誉出品");
delete.setLayout(new GridLayout(1, 3, 15, 15));
delete.add(backspace);
delete.add(c);
delete.add(ce);
delete.setBorder(new LineBorder(delete.getBackground(), 5));//添加边框
c.addActionListener(this);
ce.addActionListener(this);
backspace.addActionListener(this);
text.setFont(new Font("宋体", Font.BOLD +Font.ITALIC, 20));
// 设置显示字体
text.setBackground(Color.getHSBColor(44, 3, 87));
text.setBorder(new LineBorder(Color.ORANGE, 1));
text.setHorizontalAlignment(SwingConstants.RIGHT); // 设置鼠标靠右
text.setEditable(false); // 屏蔽键盘输入,防止非法字符
delete_main.setLayout(new GridLayout(2, 1, 10, 10));
delete_main.add(text, BorderLayout.NORTH);
delete_main.add(delete, BorderLayout.SOUTH);
buttons.setLayout(new GridLayout(4, 5, 10, 20));

bt1.addActionListener(this); // 为各个按钮添加事件监听
buttons.add(bt1);
bt2.addActionListener(this);
buttons.add(bt2);
bt3.addActionListener(this);
buttons.add(bt3);
bt4.addActionListener(this);
buttons.add(bt4);
bt5.addActionListener(this);
buttons.add(bt5);
bt6.addActionListener(this);
buttons.add(bt6);
bt7.addActionListener(this);
buttons.add(bt7);
bt8.addActionListener(this);
buttons.add(bt8);
bt9.addActionListener(this);
buttons.add(bt9);
bt10.addActionListener(this);
buttons.add(bt10);
bt11.addActionListener(this);
buttons.add(bt11);
bt12.addActionListener(this);
buttons.add(bt12);
bt13.addActionListener(this);
buttons.add(bt13);
bt14.addActionListener(this);
buttons.add(bt14);
bt15.addActionListener(this);
buttons.add(bt15);
bt16.addActionListener(this);
buttons.add(bt16);

buttons.setBorder(new LineBorder(delete.getBackground(), 5));//边框
this.setLayout(new BorderLayout());
this.add(delete_main, BorderLayout.NORTH);
this.add(buttons, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //添加关闭窗口事件监听器
pack(); //自动设置窗体大小
setResizable(false); // 设置面板大小不可变
setVisible(true);
Toolkit tk = this.getToolkit();
Dimension de = tk.getScreenSize();
setBounds((de.width - this.getWidth()) / 2,(de.height - this.getHeight()) / 2, this.getWidth(), this.getHeight());
//使之居于屏幕正中央
}
public void actionPerformed(ActionEvent e)
{ //事件响应
//Object source = e.getSource();
if(e.getSource()==backspace) //退格键
{
v=v.substring(0,v.length() - 1);
text.setText(v);
}
if(e.getSource()==c||e.getSource()==ce) //清空键
{
m=0;
sum=0;
v="";s1=0;s2=0;i=0;
text.setText("0");
}
if(e.getSource()==bt1)
{
if(m==1)
v="";
v=v+"1";
text.setText(v);
m=0;n=1;

}
if(e.getSource()==bt2)
{
if(m==1)
v="";
v=v+"2";
text.setText(v);
m=0;n=1;
}
if(e.getSource()==bt3)
{
if(m==1)
v="";
v=v+"3";
text.setText(v);
m=0;n=1;
}
if(e.getSource()==bt4) // +按钮
{
if(n==1) //如果按之前按了0~9数字键,就运算
{
sum=Float.parseFloat(v);

}
else //如果按之前没按0~9数字键,直接按了运算符,不做任何操作
{
}
i=1;
m=1;n=0;
}
if(e.getSource()==bt5)
{
if(m==1)
v="";
v=v+"4";
text.setText(v);
m=0;n=1;
}
if(e.getSource()==bt6)
{
if(m==1)
v="";
v=v+"5";
text.setText(v);
m=0;n=1;
}
if(e.getSource()==bt7)
{
if(m==1)
v="";
v=v+"6";
text.setText(v);
m=0;n=1;
}
if(e.getSource()==bt8) // -按钮
{
if(n==1)
{
sum=Float.parseFloat(v);

}
else
{
}
i=2;
m=1;n=0;
}
if(e.getSource()==bt9)
{
if(m==1)
v="";
v=v+"7";
text.setText(v);
m=0;n=1;
}
if(e.getSource()==bt10)
{
if(m==1)
v="";
v=v+"8";
text.setText(v);
m=0;n=1;
}
if(e.getSource()==bt11)
{
if(m==1)
v="";
v=v+"9";
text.setText(v);
m=0;n=1;
}
if(e.getSource()==bt12) //*按钮
{
if(n==1)
{
sum=Float.parseFloat(v);

}
else
{
}
i=3;
m=1;n=0;

}
if(e.getSource()==bt13)
{
if(m==1)
v="";
v=v+"0";
text.setText(v);
m=0;n=1;
}
if(e.getSource()==bt14)
{

}
if(e.getSource()==bt15) //等于按钮
{
if(m==1) // 如果输入了+=等非法输入,不做任何操作
{ }
else
{
s1=sum;
s2=Float.parseFloat(v);
switch(i)
{
case 0: //如果按过数字键后直接按等号,直接输出
equ=Float.parseFloat(v);
break;
case 1:
equ=s1+s2;
break;
case 2:
equ=s1-s2;
break;
case 3:
equ=s1*s2;
break;
case 4:
equ=s1/s2;
break;
}

sum=0;v=""; //清空运算数
v=String.valueOf(equ);
text.setText(String.valueOf(equ));
i=0;
}
}
if(e.getSource()==bt16) // 除按钮
{
if(n==1)
{
sum=Float.parseFloat(v);

}
else
{
}
i=4;
m=1;n=0;
}

}
public static void main(String[] args)
{
Calculator bt=new Calculator();
}
}