swing小计算器练习

这个计算器程序是对Swing的一个练习,逻辑比较简单

 

它只能进行单次运算(不能进行多项式运算),考虑了除数不能为0的情况

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame implements ActionListener{ JTextField jtf = new JTextField(); JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0; JButton badd,bsub,bmul,bdiv,bequal,bdot; int select = 0; JPanel jp1 = new JPanel(); //分别代表0~9数字和加减乘除等于和点符号 double temp = 0.0; double num = 0.0; public Calculator(){ //生成两个区域,上面区域为一个文本域,下面为计算器的按钮 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = this.getContentPane(); jtf.setHorizontalAlignment(JTextField.RIGHT); c.add(jtf,BorderLayout.NORTH); c.add(jp1,BorderLayout.CENTER); jp1.setLayout(new GridLayout(4,4)); b1 = new JButton("1"); b1.addActionListener(this); jp1.add(b1); b2 = new JButton("2"); b2.addActionListener(this); jp1.add(b2); b3 = new JButton("3"); b3.addActionListener(this); jp1.add(b3); badd = new JButton("+"); badd.addActionListener(this); jp1.add(badd); b4 = new JButton("4"); b4.addActionListener(this); jp1.add(b4); b5 = new JButton("5"); b5.addActionListener(this); jp1.add(b5); b6 = new JButton("6"); b6.addActionListener(this); jp1.add(b6); bsub = new JButton("-"); bsub.addActionListener(this); jp1.add(bsub); b7 = new JButton("7"); b7.addActionListener(this); jp1.add(b7); b8 = new JButton("8"); b8.addActionListener(this); jp1.add(b8); b9 = new JButton("9"); b9.addActionListener(this); jp1.add(b9); bmul = new JButton("*"); bmul.addActionListener(this); jp1.add(bmul); b0 = new JButton("0"); b0.addActionListener(this); jp1.add(b0); bdot = new JButton("."); bdot.addActionListener(this); jp1.add(bdot); bequal = new JButton("="); bequal.addActionListener(this); jp1.add(bequal); bdiv = new JButton("//"); bdiv.addActionListener(this); jp1.add(bdiv); } public void actionPerformed(ActionEvent e){ //jtf.setText(jtf.getText()+e.getActionCommand()); if(e.getSource() == b1){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b2){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b3){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b4){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b5){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b6){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b7){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b8){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b9){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b0){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == badd){ temp = Double.parseDouble(jtf.getText()); jtf.setText(""); select = 1; } if(e.getSource() == bsub){ temp = Double.parseDouble(jtf.getText()); jtf.setText(""); select = 2; } if(e.getSource() == bmul){ temp = Double.parseDouble(jtf.getText()); jtf.setText(""); select = 3; } if(e.getSource() == bdiv){ temp = Double.parseDouble(jtf.getText()); jtf.setText(""); select = 4; } if(e.getSource() == bequal){ if(select == 1){ num = Double.parseDouble(jtf.getText()); jtf.setText((num = temp+num)+""); } else if(select == 2){ num = Double.parseDouble(jtf.getText()); jtf.setText((num = temp-num)+""); } else if(select == 3){ num = Double.parseDouble(jtf.getText()); jtf.setText((num = temp*num)+""); } else if(select == 4){ num = Double.parseDouble(jtf.getText()); if(num == 0){ JOptionPane.showMessageDialog(jp1, "除数不能为0"); } jtf.setText((num = temp/num)+""); } } } public static void main(String[] args) { Calculator mainFrame = new Calculator(); mainFrame.setSize(400,400); mainFrame.setTitle("calculator by jk"); mainFrame.setVisible(true); } }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值