用java编写一个简单的计算器import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame
{
private JPanel jp;
private JTextField tAdd1,tAdd2,tAdd3,tSub1,tSub2,tSub3,tMul1,tMul2,tMul3,tDiv1,tDiv2,tDiv3;
private JLabel tShow,label1,label2,label3,label4,label5,label6,label7,label8;
private JButton result,exit;
//构造器
public Calculator()
{
super("简易计算器");
Init();
}
//初始化
public void Init()
{ //显示区
tShow=new JLabel(" 感谢使用简易计算器!"); tShow.setBounds(0, 0, 300, 20);
//加法区
tAdd1=new JTextField(); tAdd2=new JTextField(); tAdd3=new JTextField();
label1=new JLabel("+"); label2=new JLabel("=");
tAdd1.setBounds(5, 25, 80, 20); label1.setBounds(85, 25, 15, 15);
tAdd2.setBounds(100,25,80,20); label2.setBounds(180,25,15,15);
tAdd3.setBounds(190,25,130,20);
//减法区
tSub1=new JTextField(); tSub2=new JTextField(); tSub3=new JTextField();
label3=new JLabel("-"); label4=new JLabel("=");
tSub1.setBounds(5, 50, 80, 20); label3.setBounds(85, 50, 15, 15);
tSub2.setBounds(100,50,80,20); label4.setBounds(180,50,15,15);
tSub3.setBounds(190,50,130,20);
//乘法区
tMul1=new JTextField(); tMul2=new JTextField(); tMul3=new JTextField();
label5=new JLabel("*"); label6=new JLabel("=");
tMul1.setBounds(5, 75, 80, 20); label5.setBounds(85,75, 15, 15);
tMul2.setBounds(100,75,80,20); label6.setBounds(180,75,15,15);
tMul3.setBounds(190,75,130,20);
//除法区
tDiv1=new JTextField(); tDiv2=new JTextField(); tDiv3=new JTextField();
label7=new JLabel("/"); label8=new JLabel("=");
tDiv1.setBounds(5, 100, 80, 20); label7.setBounds(85,100, 15, 15);
tDiv2.setBounds(100,100,80,20); label8.setBounds(180,100,15,15);
tDiv3.setBounds(190,100,130,20);
result=new JButton("开始计算");
result.setBounds(10,140,100,40);
exit=new JButton("退出");
exit.setBounds(180,140,100,40);
jp=(ContentPane();
jp.setLayout(null);
jp.add(tShow); jp.add(tAdd1); jp.add(label1); jp.add(tAdd2); jp.add(label2); jp.add(tAdd3);
jp.add(tSub1); jp.add(label3); jp.add(tSub2); jp.add(label4); jp.add(tSub3); jp.add(tMul1);
jp.add(tMul2); jp.add(label5); jp.add(tMul3); jp.add(label6); jp.add(tDiv1); jp.add(label7);
jp.add(tDiv2); jp.add(label8); jp.add(tDiv3); jp.add(result); jp.add(exit);
//动作事件
exit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
it(0);
}
});
result.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Double d = null;
String a=null,b=null;
Text(); Text();
if(!a.isEmpty()&&!b.isEmpty()) //if(a!=null&b!=null&&a!=""&b!="")不行
{
d=Double.parseDouble(a)+Double.parseDouble(b);
tAdd3.setText(d+""); //将d转换为字符串型
// tAdd3.setText(String.valueOf(d));
}
Text(); Text();
if(!a.isEmpty()&&!b.isEmpty())
{d=Double.parseDouble(a)-Double.parseDouble(b);
tSub3.setText(d+"");
}
Text(); Text();
if(!a.isEmpty()&&!b.isEmpty())
{d=Double.parseDouble(a)*Double.parseDouble(b);
tMul3.setText(d+"");
}
Text(); Text();
if(!a.isEmpty()&&!b.isEmpty())
{d=Double.parseDouble(a)/Double.parseDouble(b);
tDiv3.setText(String.valueOf(d));
}
}
});
}
//主函数
public static void main(String[] args)
{
Calculator cal=new Calculator();
cal.setVisible(true);
cal.setResizable(false);
cal.setSize(330,230);
cal.setLocationRelativeTo(null);
cal.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论