1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| package code;
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.regex.Pattern;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener{ private JMenuBar jmb; private JMenu jm; private JMenuItem jmi1; private JTextField result; private JButton key[]; private JPanel jp_but,jp_cal; private Mainpad jp_main; private Container con; private double opernum; private String keytitle[]={"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/", "!", "^", "C", "-/+", "1/x", "sqrt", "四舍五入", "back"}, opers = ""; public Calculator() { this.setSize(400,450); this.setTitle("计算器"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.jmb=new JMenuBar(); this.jm=new JMenu("文件"); this.jmi1=new JMenuItem("退出"); this.jm.add(this.jmi1); this.result=new JTextField(); this.key=new JButton[24]; this.jp_but=new JPanel(); this.jp_cal=new JPanel(); this.jp_main=new Mainpad(); this.con=this.getContentPane(); this.jp_but.setLayout(new GridLayout(6,4,5,5)); this.jp_cal.setLayout(new BorderLayout()); this.jp_main.setLayout(new BorderLayout()); this.con.setLayout(new BorderLayout()); Font font = this.result.getFont(); this.result.setFont(new Font(font.getName(), font.getStyle(), 24)); for(int i=0;i<24;i++) { this.key[i]=new JButton(this.keytitle[i]); this.key[i].setSize(60, 60); this.key[i].addActionListener(this); this.jp_but.add(key[i]); } this.jmb.add(jm); this.jp_main.add(jmb,BorderLayout.NORTH); this.jp_main.add(jp_cal,BorderLayout.CENTER); this.jp_cal.add(result,BorderLayout.NORTH); this.jp_cal.add(this.jp_but,BorderLayout.CENTER); this.con.add(jp_main); this.jmi1.addActionListener(this); this.setBackground(Color.RED); this.setLocation(400, 100); this.setVisible(true); } public static void main(String[] args) { new Calculator(); } @Override public void actionPerformed(ActionEvent evt) { String oper=evt.getActionCommand(); switch(oper) { case "0": this.result.setText(this.result.getText()+oper); break; case "1": this.result.setText(this.result.getText()+oper); break; case "2": this.result.setText(this.result.getText()+oper); break; case "3": this.result.setText(this.result.getText()+oper); break; case "4": this.result.setText(this.result.getText()+oper); break; case "5": this.result.setText(this.result.getText()+oper); break; case "6": this.result.setText(this.result.getText()+oper); break; case "7": this.result.setText(this.result.getText()+oper); break; case "8": this.result.setText(this.result.getText()+oper); break; case "9": this.result.setText(this.result.getText()+oper); break; case "+": this.opernum = Double.valueOf(this.result.getText()); this.result.setText(""); this.opers = "+"; break; case "-": this.opernum = Double.valueOf(this.result.getText()); this.result.setText(""); this.opers = "-"; break; case "*": this.opernum = Double.valueOf(this.result.getText()); this.result.setText(""); this.opers = "*"; break; case "/": this.opernum = Double.valueOf(this.result.getText()); this.result.setText(""); this.opers = "/"; break; case ".": this.result.setText(this.result.getText()+oper); break; case "^": this.opernum=Double.valueOf(this.result.getText()); this.result.setText(this.result.getText()+"^"); this.opers="^"; break; case "-/+": this.result.setText(String.valueOf(-Double.valueOf(this.result.getText()))); break; case "1/x": this.result.setText(String.valueOf(1/(Double.valueOf(this.result.getText())))); break; case "sqrt": this.result.setText(String.valueOf(Math.sqrt(Double.valueOf(this.result.getText())))); break; case "四舍五入": this.result.setText(String.valueOf((int) (Double.valueOf(this.result.getText())+0.5))); break; case "back": if(this.result.getText().length()>0) this.result.setText(this.result.getText().substring(0, this.result.getText().length()-1)); break; case "C": this.result.setText("");break; case "!": this.result.setText(String.valueOf(this.JieCheng(Double.valueOf(this.result.getText())))); break; case "=": switch(this.opers) { case "+": this.result.setText (String.valueOf(this.opernum+Double.valueOf(this.result.getText()))); break; case "-": this.result.setText (String.valueOf(this.opernum-Double.valueOf(this.result.getText()))); break; case "*": this.result.setText (String.valueOf(this.opernum*Double.valueOf(this.result.getText()))); break; case "/": this.result.setText (String.valueOf(this.opernum/Double.valueOf(this.result.getText()))); break; case "^": this.result.setText( String.valueOf((Math.pow(this.opernum, Integer.valueOf(this.result.getText().toString().substring (this.result.getText().toString().indexOf('^')+1)))))); break; default: } break; case "退出": System.exit(0); } } public long JieCheng(double n3) { long n = (long)(n3 + 0.5); if(n==1 || n==0) return n; else if(n>1) { return n*JieCheng(n-1); } else { return 0; } } public boolean isDot(String num) { if(Pattern.matches("[0-9].*", num)) { return false; } else { return true; } } }
class Mainpad extends JPanel{ public Mainpad() { this.setBackground(Color.RED); } }
|