java课程设计模板(windows计算器)(2)

2018-11-22 20:54

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.


java课程设计模板(windows计算器)(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:医疗矿泉水项目可行性研究报告

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: