用java写一个简易的计算器

用java写一个简易计算机

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

package chapter11_3; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class Calculator extends JFrame implements ActionListener { private String[] str = { "7", "8", "9", "/", "sqrt", "4", "5", "6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" }; private JTextField tf_out; private JButton jb_bk, jb_ce, jb_c; private JButton[] jb_key; private char ch = '#'; private boolean can = false; private double num1; public void creatGUI() { tf_out = new JTextField(); tf_out.setHorizontalAlignment(JTextField.RIGHT); tf_out.setColumns(18); tf_out.setEditable(false); // 设置输出不可编辑 tf_out.setText("0"); this.add(tf_out, BorderLayout.NORTH); JPanel p = new JPanel(new BorderLayout(3, 8)); JPanel p1 = new JPanel(new GridLayout(1, 3, 3, 10)); p.add(p1, "North"); jb_bk = new JButton("Backspace"); jb_bk.setForeground(Color.RED); jb_bk.addActionListener(this); jb_ce = new JButton("CE"); jb_ce.setForeground(Color.RED); jb_ce.addActionListener(this); jb_c = new JButton("C"); jb_c.setForeground(Color.RED); jb_c.addActionListener(this); p1.add(jb_bk); p1.add(jb_ce); p1.add(jb_c); JPanel p2 = new JPanel(new GridLayout(4, 5, 3, 3)); p.add(p2, BorderLayout.CENTER); jb_key = new JButton[str.length]; for (int i = 0; i < str.length; i++) { jb_key[i] = new JButton(str[i]); jb_key[i].addActionListener(this); if (i == 3 || i == 8 || i == 13 || i == 18 || i == 19) { jb_key[i].setForeground(Color.RED); } else { jb_key[i].setForeground(Color.BLUE); } p2.add(jb_key[i]); } this.add(p, BorderLayout.CENTER); this.setTitle("计算器"); this.setIconImage(new ImageIcon("image/1.jpg").getImage()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值