panel.add(panel1,BorderLayout.NORTH); panel.add(panel2,BorderLayout.WEST); panel.add(panel3); this.add(panel); this.setResizable(false); this.setLocationRelativeTo(null); } //设置侦听器
public ActionListener getActionListener() {
return new ActionListener() {
public void actionPerformed(final ActionEvent e) {
final String cmd = e.getActionCommand(); String result = null; try {
result = service.callMethod(cmd,textField1.getText()); }
catch(final Exception ex) {
System.out.println(ex.getMessage()); }
if(opString.indexOf(cmd) != -1) {
textField2.setText(textField2.getText()+textField1.getText()+cmd);
}
else if(cmd.equals(\ {
textField2.setText(\ }
else if(cmd.indexOf(\ button.setText(\ else
if(cmd.indexOf(\ button.setText(\ if(result != null)
textField1.setText(result); } }; }
//初始化输入框
private JTextField getTextField() {
textField1 = new JTextField();
textField1.setBorder(new EmptyBorder(0,0,0,0)); textField1.setText(\
textField1.setHorizontalAlignment(JTextField.RIGHT); textField1.setEditable(false); textField1.setOpaque(false);
textField2 = new JTextField();
textField2.setBorder(new EmptyBorder(0,0,0,0)); textField2.setHorizontalAlignment(JTextField.RIGHT); textField2.setEditable(false); textField2.setOpaque(false); return null; }
//获得计算器的存储显示等按键 private JButton[] getMButton() {
final JButton[] result = new JButton[this.mOp.length+1]; result[0] = this.button;
for(int i = 0;i < this.mOp.length;i++)
{
final JButton b = new JButton(this.mOp[i]); b.addActionListener(getActionListener()); b.setForeground(Color.black); result[i+1] = b; }
return result; }
//获得计算器的退格清零等按键 private JButton[] getRButton() {
final JButton[] result = new JButton[this.rOp.length]; for(int i = 0;i < this.rOp.length;i++) {
final JButton b = new JButton(this.rOp[i]); b.addActionListener(getActionListener()); b.setForeground(Color.red); result[i] = b; }
return result; }
//获得计算器的加减乘除等其他按键 private JButton[] getNButton() {
final JButton[] result = new JButton[this.nOp.length]; for(int i = 0;i < this.nOp.length;i++) {
final JButton b = new JButton(this.nOp[i]);
b.addActionListener(getActionListener()); b.setForeground(Color.blue); result[i] = b; }
return result;
} }
//计算方法 class CalService {
private double store;
private boolean isSecondNum = false; private String numString = \ private String opString = \ private String lastOp; private String firstNum = \ private String secondNum; private boolean flag = true; public double getStore() {
return store; }
//调用该方法并返回计算结果
public String callMethod(String cmd,String text) {
if(cmd.equals(\ return clearAll(); else if(cmd.equals(\
//设置文本区域 { }
//获得文本区域 { }
return textArea; this.textArea = textArea;
public void setTextArea(JTextArea textArea)
public JTextArea getTextArea()
return clear(text); else if(cmd.equals(\ return backSpace(text); else if(numString.indexOf(cmd) != -1) return catNum(cmd,text); else if(opString.indexOf(cmd) != -1) return setOp(cmd,text); else if(cmd.equals(\ {
flag = false; return cal(text,false); }
else if(cmd.equals(\ return setNegative(text); else if(cmd.equals(\ return setReciprocal(text); else if(cmd.equals(\ return sqrt(text); else if(cmd.equals(\ return cal(text,true); else
return mCmd(cmd,text); }
public String cal(String text,boolean isPercent)//用来结算加、减、乘、除法,并返回封装成String类型的结果,参数text是显示框中的内容,boolean代表是否有“%”运算
{
double
secondResult
=
secondNum
==
null
?
Double.valueOf(text).doubleValue():Double.valueOf(secondNum).doubleValue();
if(secondResult == 0 && this.lastOp.equals(\
return \
if(isPercent)
firstNum=String.valueOf(MyMath.multiply(Double.valueOf(firstNum),MyMath.