JAVA编写的计算器源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package jsq;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class Test implements ActionListener {
/**
*
*/
JFrame f = new JFrame();
JTextField tf = new JTextField("0.");
JMenuBar bar=new JMenuBar();
JMenu menuEdit=new JMenu("编辑(E)");
JMenu menuView=new JMenu("查看(V)");
JMenu menuHelp=new JMenu("帮助(H)");
JMenuItem menuEditCopy=new JMenuItem("复制(C)");
JMenuItem menuEditPaste=new JMenuItem("粘贴(V)");
JMenuItem menuHelpAbout = new JMenuItem("关于计算器(A)");
JPanel panel0 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JButton btBk = new JButton(" Backspace"); JButton btCe = new JButton("Ce");
JButton btCl = new JButton("C");
JButton bt7 = new JButton("7");
JButton bt8 = new JButton("8");
JButton bt9 = new JButton("9");
JButton btDiv = new JButton("/");
JButton btSqrt = new JButton("sqrt");
JButton bt4 = new JButton("4");
JButton bt5 = new JButton("5");
JButton bt6 = new JButton("6");
JButton btMult = new JButton("*"); JButton btMol = new JButton(" % ");
JButton bt1 = new JButton("1");
JButton bt2 = new JButton("2");
JButton bt3 = new JButton("3");
JButton btMinu = new JButton("-"); JButton btCoun = new JButton(" 1/x");
JButton bt0 = new JButton("0");
JButton btPoint = new JButton("."); JButton btEqual = new JButton("="); JButton btAdd = new JButton("+");
JButton btSign = new JButton(" +/-");
/**
*@param args
*/
public void init(){
f.add(panel0);
f.add(tf,"North");
f.setJMenuBar(bar);
bar.add(menuEdit);
bar.add(menuView);
bar.add(menuHelp);
menuEdit.add(menuEditCopy);
menuEdit.add(menuEditPaste);
menuHelp.add(menuHelpAbout);
tf.setEditable(false);//不可编辑
tf.setHorizontalAlignment(JTextField.RIGHT);//从右侧输入数据panel0.add(panel2);
panel0.add(panel);
panel0.add(panel3);
panel0.add(panel4);
panel0.add(panel5);
panel2.setLayout(new GridLayout(1,4));
panel2.add(btBk);
panel2.add(btCe);
panel2.add(btCl);
panel.setLayout(new GridLayout(1,5));
panel.add(bt7);
panel.add(bt8);
panel.add(bt9);
panel.add(btDiv);
panel.add(btSqrt);//根号
panel3.setLayout(new GridLayout(1,5));
panel3.add(bt4);
panel3.add(bt5);
panel3.add(bt6);
panel3.add(btMult);
panel3.add(btMol);//百分号
panel4.setLayout(new GridLayout(1,5));
panel4.add(bt1);
panel4.add(bt2);
panel4.add(bt3);
panel4.add(btMinu);
panel4.add(btCoun);//倒数
panel5.setLayout(new GridLayout(1,5));
panel5.add(bt0);
panel5.add(btPoint);
panel5.add(btAdd);
panel5.add(btEqual);
panel5.add(btSign);//正负符号