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
| package cn.cduestc.fire;
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.Collections;
public class Story { public static void main(String[] args) { MyFrame myFrame = new MyFrame(); myFrame.setTitle("简易计算器--单次计算完成请点击AC继续下一次运算"); } } class MyFrame extends Frame implements ActionListener {
TextField textField = new TextField();
Panel panel1 = new Panel(new GridLayout(1, 4)); Button button1 = new Button("1"); Button button2 = new Button("2"); Button button3 = new Button("3"); Button buttonadd = new Button("+");
Panel panel2 = new Panel(new GridLayout(1, 4)); Button button4 = new Button("4"); Button button5 = new Button("5"); Button button6 = new Button("6"); Button buttonsubtract = new Button("-");
Panel panel3 = new Panel(new GridLayout(1, 4)); Button button7 = new Button("7"); Button button8 = new Button("8"); Button button9 = new Button("9"); Button buttonmultiply = new Button("x");
Panel panel4 = new Panel(new GridLayout(1, 4));
Button button0 = new Button("0"); Button button_equal = new Button("="); Button buttondivide = new Button("÷"); Button buttonpoint = new Button("."); Panel panel5 = new Panel(new GridLayout(1, 4)); JButton button_eliminate = new JButton("AC"); Button buttonremainder = new Button("%"); Button buttonclose = new Button("close"); Button buttonsqure = new Button("²"); public MyFrame(){ JFrame jf=new JFrame("简易计算器"); setBounds(300, 0, 800, 800); button_eliminate.setFont(new java.awt.Font( "SimSun", 1, 100)); buttonadd.setFont(new java.awt.Font( "SimSun", 1, 50)); buttonsubtract.setFont(new java.awt.Font( "SimSun", 1, 50)); buttonmultiply.setFont(new java.awt.Font( "SimSun", 1, 50)); buttondivide.setFont(new java.awt.Font( "SimSun", 1, 50)); button_equal.setFont(new java.awt.Font( "SimSun", 1, 50)); buttonpoint.setFont(new java.awt.Font( "SimSun", 1, 50)); buttonremainder.setFont(new java.awt.Font( "SimSun", 1, 50)); buttonclose.setFont(new java.awt.Font( "SimSun", 1, 50)); buttonsqure.setFont(new java.awt.Font( "SimSun", 1, 50)); textField.setFont(new java.awt.Font( "SimSun", 1, 50)); setLayout(new GridLayout(6, 1));
setVisible(true);
add(textField); panel1.add(buttonadd); panel1.add(button1); panel1.add(button2); panel1.add(button3);
add(panel1); panel2.add(buttonsubtract); panel2.add(button4); panel2.add(button5); panel2.add(button6);
add(panel2); panel3.add(buttonmultiply); panel3.add(button7); panel3.add(button8); panel3.add(button9);
add(panel3); panel4.add(buttondivide); panel4.add(button_equal); panel4.add(button0);
panel4.add(buttonpoint); add(panel4); panel5.add(buttonremainder); panel5.add(buttonsqure); panel5.add(buttonclose); panel5.add(button_eliminate); add(panel5);
ArrayList<Button> arrayList=new ArrayList<>(); Collections.addAll(arrayList,button0,button1,button2,button3,button4,button5,button6,button7,button8,button9,button_equal,buttonadd,buttonsubtract,buttonmultiply,buttondivide,buttonpoint,buttonremainder,buttonclose,buttonsqure); button_eliminate.addActionListener(this); for(Button button:arrayList){ button.addActionListener(this); } addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } });
} String str1=""; @Override public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if(str.equals("=")){ String text=textField.getText(); String[] text1; float c; if ((text1 = text.split("\\+")).length == 2) { c = Float.parseFloat(text1[0]) + Float.parseFloat(text1[1]); textField.setText(String.valueOf(c));
} else if ((text1 = text.split("-")).length == 2) {
c = Float.parseFloat(text1[0]) - Float.parseFloat(text1[1]); textField.setText(String.valueOf(c));
} else if ((text1 = text.split("x")).length == 2) { c = Float.parseFloat(text1[0]) * Float.parseFloat(text1[1]); textField.setText(String.valueOf(c));
} else if ((text1 = text.split("÷")).length == 2) { c = Float.parseFloat(text1[0]) / Float.parseFloat(text1[1]); textField.setText(String.valueOf(c)); } else if ((text1 = text.split("%")).length == 2) { if(text1[0].equals(".")){ textField.setText("操作错误"); System.exit(0); } if(Float.parseFloat(text1[0]) >Float.parseFloat(text1[1])) { c = (Float.parseFloat(text1[0])%Float.parseFloat(text1[1])); textField.setText(String.valueOf(c)); } else{ c=Float.parseFloat(text1[0]); textField.setText(String.valueOf(c)); }
}else if ((text1 = text.split("²")).length == 1) { c = (float) Math.pow(Float.parseFloat(text1[0]),2); textField.setText(String.valueOf(c)); }
} else if(str.equals("AC")){ str1 = ""; textField.setText(""); } else if (str.equals("close")) { System.exit(0); }
else{ textField.setText(str1=str1+str); }
} }
|