JAVA计算器——Quicl修改版

 由于特殊原因如果您看到的是不完全的代码 ,请您重新打开该网页,即可看到完整的代码。

001 import java.awt.*;
002 import java.awt.datatransfer.Clipboard;
003 import java.awt.datatransfer.StringSelection;
004 import java.awt.datatransfer.Transferable;
005 import java.awt.event.*;
006 import java.io.*;
007 import javax.swing.*;
008
009 public class cal implements ActionListener {
010 Clipboard clipboard = null;
011 JTextField jtf = new JTextField(20);
012 private boolean append = false;
013 private String op1 = “0”;
014 private String operator = “+”;

015
016 public void actionPerformed(ActionEvent e) {
017 String comn = e.getActionCommand();
018 if (“0123456789”.indexOf(comn) != 1) {
019 if (append) {// 追加
020 String temp = jtf.getText();
021 jtf.setText(temp + comn);
022 }
023 else {// 替换
024 jtf.setText(comn);
025 append = true;
026 }
027 } // 处理运算符
028 else if (“+-*/%”.indexOf(comn) != 1) {
029 op1 = jtf.getText();
030 operator = comn;
031 append = false;
032 }
033 else if (“=”.indexOf(comn) != 1) {
034 String op2 = jtf.getText();
035 double d1 = Double.parseDouble(op1);
036 double d2 = Double.parseDouble(op2);
037 if (“+”.equals(operator)) {
038 d1 = d1 + d2;
039 } else if (“-“.equals(operator)) {
040 d1 = d1 d2;
041 } else if (“*”.equals(operator)) {
042 d1 = d1 * d2;
043 } else if (“/”.equals(operator)) {
044 d1 = d1 / d2;
045 }else if (“%”.equals(operator)) {if(“d1>d2!=null”!=null)
046 d1 =d1 % d2;
047 }
048 jtf.setText(d1 + “”);
049 append = false;
050 }
051 else if (“.”.equals(comn)) {
052 String temp = jtf.getText();
053 if (temp.indexOf(“.”) == 1) {
054 jtf.setText(temp + “.”);
055 append = true;
056 }
057 }
058 else if (“Backspace”.equals(comn)) {
059 String temp = jtf.getText();
060 if (temp.length() > 0) {
061 jtf.setText(temp.substring(0, temp.length() 1));
062 }
063 }
064 else if (“CE”.equals(comn)) {
065 jtf.setText(“0”);
066 append = false;
067 }
068 else if (“作者博客”.equals(comn)||“About”.equals(comn)) {
069 String command = “rundll32 url.dll,FileProtocolHandler 20xue.com;
070 try {
071 Runtime.getRuntime().exec(command);
072 }
073 catch (IOException e1) {
074 e1.printStackTrace();
075 }
076 }
077 else if (“Quit”.equals(comn)) {
078 System.exit(0);
079 }
080 else if (“Copy”.equals(comn)) {
081 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
082 Transferable trandata = new StringSelection(op1);
083 clipboard.setContents(trandata, null);
084 }
085 }
086
087 public cal() {
088 JFrame jf = new JFrame(“Quicl’s  Ccalculator”);
089 JMenuBar menuBar = new JMenuBar();
090 jf.setJMenuBar(menuBar);
091 JMenu FileMenu = new JMenu(“File”);
092 menuBar.add(FileMenu);
093 JMenuItem quitItem = new JMenuItem(“Quit”);
094 FileMenu.add(quitItem);
095 JMenu EditMenu=new JMenu(“Edit”);
096 menuBar.add(EditMenu);
097 JMenuItem copyItem = new JMenuItem(“Copy”);
098 EditMenu.add(copyItem);
099 JMenu HelpMenu=new JMenu(“Help”);
100 menuBar.add(HelpMenu);
101 JMenuItem helpItem = new JMenuItem(“Help”);
102 HelpMenu.add(helpItem);
103 HelpMenu.addSeparator();
104 JMenuItem aboutItem = new JMenuItem(“About”);
105 HelpMenu.add(aboutItem);
106 helpItem.addActionListener(this);
107 aboutItem.addActionListener(this);
108 copyItem.addActionListener(this);
109 quitItem.addActionListener(this);
110 jf.add(jtf, BorderLayout.NORTH);
111 String[] s1 = { “Backspace”, “CE”, “作者博客”, “+”, “7”, “8”, “9”, “/”, “4”,“5”, “6”, “*”, “1”, “2”, “3”, “-“, “0”, “%”, “.”, “=” };
112 JPanel jp = new JPanel();
113 jf.add(jp, BorderLayout.CENTER);
114 GridLayout gl = new GridLayout(5, 4);
115 jp.setLayout(gl);
116 JButton[] jb = new JButton[s1.length];
117 for (int i = 0; i < s1.length; i++) {
118 jb[i] = new JButton(s1[i]);
119 jp.add(jb[i]);
120 jb[i].addActionListener(this);
121 }
122 jb[2].setBackground(new Color(204,238,102));
123 jf.add(jp);
124 jtf.setEditable(false);
125 jf.setLocation(400, 300);
126 jf.pack();
127 jf.setResizable(false);// 设置窗口的大小不可变
128 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
129 jf.setVisible(true);
130 }
131
132 public static void main(String[] args) {
133 new cal();
134 }
135 }