用Java写计算器

eclipse+java+GUI:

package Jsq;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JSQ extends JFrame implements
ActionListener {
boolean end,mul,sub,add,div;
double num1,num2,num3;
JTextField t=new JTextField("0");
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("+");
JButton b5=new JButton("4");
JButton b6=new JButton("5");
JButton b7=new JButton("6");
JButton b8=new JButton("-");
JButton b9=new JButton("7");
JButton b10=new JButton("8");
JButton b11=new JButton("9");
JButton b12=new JButton("*");
JButton b13=new JButton(".");
JButton b14=new JButton("0");
JButton b15=new JButton("=");
JButton b16=new JButton("/");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
public JSQ(){
 setTitle("计算器"); 
 setVisible(true);
 setLocation(400,300);
 setBounds(200,200,400,300);
 setResizable(false);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(p1,BorderLayout.NORTH);
t.setColumns(20);
t.setHorizontalAlignment(JTextField.RIGHT);
p1.add(t);
GridLayout l=new GridLayout(4,0);
l.setVgap(10);
l.setHgap(10);
p2.setLayout(l);
add(p2,BorderLayout.CENTER);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b10);
p2.add(b11);
p2.add(b12);
p2.add(b13);
p2.add(b14);
p2.add(b15);
p2.add(b16);
JLabel l1=new JLabel();
JLabel l2=new JLabel();
l1.setPreferredSize(new Dimension(10,0));
l2.setPreferredSize(new Dimension(10,0));
add(l1,"West");
add(l2,"East");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
}
public static void main(String[] args) {
	JSQ f=new JSQ();
}
public void num(int i){
String s=String.valueOf(i);
if(end){  
t.setText("0");
end=false;}
if((t.getText()).equals("0")){
t.setText(s);}
else{  
 s=t.getText()+s;
t.setText(s);}
}
	public void point(){
		String s;
		if(t.getText().indexOf('.')<0)
		{s=t.getText()+".";
		t.setText(s);}
		}
public void sign(int i){
		if(i==1){
		add=true;mul=false;sub=false;div=false;}
		if(i==2){
		add=false;mul=false;sub=true;div=false;}
		if(i==3){
		add=false;mul=true;sub=false;div=false;}
		if(i==4){
		add=false;mul=false;sub=false;div=true;}
		num1=Double.parseDouble(t.getText());
		end=true;
		}	
	
	public void eql(){
		num2=Double.parseDouble(t.getText());
		if(add)
		 num3=num1+num2;
		if(mul)
		num3=num1*num2;
		if(sub)
		num3=num1-num2;
		if(div)
		num3=num1/num2;
		String s=String.valueOf(num3);
		t.setText(s);
		end=true;
		}
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==b1)
			num(1);	
		if(e.getSource()==b2)
			num(2);
		if(e.getSource()==b3)
			num(3);
		if(e.getSource()==b5)
			num(4);
		if(e.getSource()==b6)
			num(5);
		if(e.getSource()==b7)
			num(6);
		if(e.getSource()==b9)
			num(7);
		if(e.getSource()==b10)
			num(8);
		if(e.getSource()==b11)
			num(9);
		if(e.getSource()==b14)
			num(0);
		if(e.getSource()==b8)
			sign(2);
		if(e.getSource()==b12)
			sign(3);
		if(e.getSource()==b16)
			sign(4);
		if(e.getSource()==b13)
			point();
		if(e.getSource()==b15)
			eql();
	}	
	}

效果图
在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值