实验目的熟悉JAVA GUI程序开发的一般步骤,掌握事件驱动,并能掌握一定的Java编程能力,在实践中能感受Java的奥秘!同时检验学生的学生的学习成果。
实验目标具有加减乘除的计算器,可以延伸到开方等其他的一些计算。
开发工具与平台jdk1.5+Eclipse 3.2设计思路:1.界面设计2.逻辑设计首先明确该实验设计的要求实现的基本功能,其次设计布局,按照GridLayout的布局,将空间划分为由行和列组成的网格单元,每个单元放一个组件,网格单元大小可以不同,尽量将相同属性的按钮放在一个区域,方便操作。
3.软件调试与测试经过调试之后,能实现基本的四则运算,并能实现开方和消除错误的数。
4.软件设计优化该软件的界面应该更加流畅,同时我相信随着自己只是的日益丰富,开发软件的速度更快,效率更高,功能更加全面!5.总结不足没有实现Mc,MR,MS,M+四个功能,有待进一步完善。
这样才能够完成自己的第一个应用软件设计!源程序如下:import java.awt.BorderLayout;import java.awt.Container;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JPanel;import javax.swing.JTextField;public class Calculator extends JFrame implements ActionListener { boolean init = true;boolean isMath = false;boolean clear = true;boolean clickable = true;double qian;String fuhao;int all = 0;JTextField text = new JTextField(25);JButton bM = new JButton();JButton bMC = new JButton("MC");JButton bMR = new JButton("MR");JButton bMS = new JButton("MS");JButton bMADD = new JButton("M+");JButton b0 = new JButton("0");JButton b1 = new JButton("1");JButton b2 = new JButton("2");JButton b3 = new JButton("3");JButton b4 = new JButton("4");JButton b5 = new JButton("5");JButton b6 = new JButton("6");JButton b7 = new JButton("7");JButton b8 = new JButton("8");JButton b9 = new JButton("9");JButton bNOP = new JButton("+/-");JButton bDot = new JButton(".");JButton bDiv = new JButton("/");JButton bMul = new JButton("*");JButton bSub = new JButton("-");JButton bAdd = new JButton("+");JButton bSprt = new JButton("sprt");JButton bMod = new JButton("%");JButton bDao = new JButton("1/x");JButton bEqual = new JButton("=");JButton bBackspace = new JButton("Backspace"); JButton bCE = new JButton("CE");JButton bC = new JButton("C");public Calculator() {this.setTitle("计算器");JMenuBar mainMenu = new JMenuBar();setJMenuBar(mainMenu);JMenu editMenu = new JMenu("编辑");JMenu viewMenu = new JMenu("查看");JMenu helpMenu = new JMenu("帮助");mainMenu.add(editMenu);mainMenu.add(viewMenu);mainMenu.add(helpMenu);JPanel jpDisplay = new JPanel();JPanel jpInput = new JPanel();JPanel jpLeft = new JPanel();JPanel jpRight = new JPanel();text.setText("0.");text.setHorizontalAlignment(JTextField.RIGHT);jpDisplay.add(text);bM.addActionListener(this);bMC.addActionListener(this);bMS.addActionListener(this);bMR.addActionListener(this);bMADD.addActionListener(this);jpLeft.setLayout(new GridLayout(5, 1)); jpLeft.add(bM);jpLeft.add(bMC);jpLeft.add(bMR);jpLeft.add(bMS);jpLeft.add(bMADD);JPanel jpInnerN = new JPanel();JPanel jpInnerS = new JPanel(); bBackspace.addActionListener(this); bCE.addActionListener(this);bC.addActionListener(this);jpInnerN.setLayout(new GridLayout(1, 3)); jpInnerN.add(bBackspace);jpInnerN.add(bCE);jpInnerN.add(bC);b0.addActionListener(this);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);b4.addActionListener(this);b5.addActionListener(this);b6.addActionListener(this);b7.addActionListener(this);b8.addActionListener(this);b9.addActionListener(this);bNOP.addActionListener(this);bDot.addActionListener(this);bDiv.addActionListener(this);bMul.addActionListener(this);bSub.addActionListener(this);bAdd.addActionListener(this);bSprt.addActionListener(this);bMod.addActionListener(this);bDao.addActionListener(this);bEqual.addActionListener(this); jpInnerS.setLayout(new GridLayout(4, 5)); jpInnerS.add(b7);jpInnerS.add(b8);jpInnerS.add(b9);jpInnerS.add(bDiv);jpInnerS.add(bSprt);jpInnerS.add(b4);jpInnerS.add(b5);jpInnerS.add(b6);jpInnerS.add(bMul);jpInnerS.add(bMod);jpInnerS.add(b1);jpInnerS.add(b2);jpInnerS.add(b3);jpInnerS.add(bSub);jpInnerS.add(bDao);jpInnerS.add(b0);jpInnerS.add(bNOP);jpInnerS.add(bDot);jpInnerS.add(bAdd);jpInnerS.add(bEqual);jpRight.setLayout(new BorderLayout());jpRight.add(jpInnerN, BorderLayout.NORTH);jpRight.add(jpInnerS, BorderLayout.CENTER);jpInput.setLayout(new BorderLayout());jpInput.add(jpLeft, BorderLayout.WEST);jpInput.add(jpRight, BorderLayout.CENTER);Container pane = this.getContentPane();pane.setSize(333, 208);this.setLocation(300, 200);this.setLayout(new BorderLayout());pane.add(jpDisplay, BorderLayout.CENTER);pane.add(jpInput, BorderLayout.SOUTH);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.pack();this.setVisible(true);}public void actionPerformed(ActionEvent e) { if (init)this.text.setText("0.");init = false;Object source = e.getSource();if (source == bBackspace) {String s = text.getText();text.setText("");for (int i = 0; i < s.length() - 1; i++) { char a = s.charAt(i);text.setText(text.getText() + a);}System.out.println("backspace");}if (source == bCE) {text.setText("0.");clear = true;init = true;System.out.println("CE");}if (source == bC) {text.setText("0.");clear = true;init = true;System.out.println("C");}if (source == bM) {System.out.println("M");}if (source == bMC) {System.out.println("MC,功能末实现");}if (source == bMR) {System.out.println("MR,功能末实现");}if (source == bMS) {System.out.println("MS,功能末实现");}if (source == bMADD) {System.out.println("M+,功能末实现");}if (source == b0) {System.out.println("0");if (clear == false)// 判断是否点击了符号位text.setText("");text.setText(text.getText() + "0");}if (source == b1) {if (clear == false)text.setText("");text.setText(text.getText() + "1");clear = true;// 第二次不在清空(前二句)}if (source == b2) {if (clear == false)text.setText("");text.setText(text.getText() + "2");clear = true;}if (source == b3) {System.out.println("3");if (clear == false)text.setText("");text.setText(text.getText() + "3");clear = true;}if (source == b4) {System.out.println("4");if (clear == false)text.setText("");text.setText(text.getText() + "4");clear = true;}if (source == b5) {System.out.println("5");if (clear == false)text.setText("");text.setText(text.getText() + "5");clear = true;}if (source == b6) {System.out.println("6");if (clear == false)text.setText("");text.setText(text.getText() + "6");clear = true;}if (source == b7) {System.out.println("7");if (clear == false)text.setText("");text.setText(text.getText() + "7");clear = true;}if (source == b8) {System.out.println("8");if (clear == false)text.setText("");text.setText(text.getText() + "8");clear = true;}if (source == b9) {if (clear == false)text.setText("");text.setText(text.getText() + "9");clear = true;}try{if (source == bNOP) {System.out.println("+/-");boolean isNumber = true;String s = text.getText();for (int i = 0; i < s.length(); i++)if (!(s.charAt(i) >= '0' && s.charAt(i) <= '9'|| s.charAt(i) == '.' || s.charAt(i) == '-')) {isNumber = false;break;}if (isNumber == true) {// 如果当前字符串首字母有'-'号,代表现在是个负数,再按下时,则将首符号去掉if (s.charAt(0) == '-') {text.setText("");for (int i = 1; i < s.length(); i++) {char a = s.charAt(i);text.setText(text.getText() + a);}}// 如果当前字符串第一个字符不是符号,则添加一个符号在首字母处elsetext.setText('-' + s);}}if (source == bDot) {System.out.println(".");clickable = true;for (int i = 0; i < text.getText().length(); i++)if ('.' == text.getText().charAt(i)) {clickable = false;break;} // 第一层判断是否里面含有小数点;if (clickable == true)// 第二层判断text.setText(text.getText() + ".");}if (source == bDiv) {System.out.println("/");qian = Double.parseDouble(text.getText());fuhao = "/";clear = false;}if (source == bMul) {System.out.println("*");qian = Double.parseDouble(text.getText());fuhao = "*";clear = false;}if (source == bSub) {System.out.println("-");qian = Double.parseDouble(text.getText());fuhao = "-";clear = false;}if (source == bAdd) {System.out.println("+");qian = Double.parseDouble(text.getText());fuhao = "+";clear = false;}if (source == bSprt) {System.out.println("sprt");String s = text.getText();if (s.charAt(0) == '-') {text.setText("负数不能开根号");} elsetext.setText(Double.toString(ng.Math.sqrt(Double.parseDouble(text.getText()))));clear = false;}if (source == bMod) {System.out.println("%,功能末实现");}if (source == bDao) {System.out.println("1/x");if (text.getText().charAt(0) == '0'&& text.getText().length() == 1) {text.setText("除数不能为零");} else {boolean isDec = true;int i, j, k;String s= Double.toString(1 / Double.parseDouble(text.getText()));for (i = 0; i < s.length(); i++)if (s.charAt(i) == '.')break;for (j = i + 1; j < s.length(); j++)if (s.charAt(j) != '0') {isDec = false;break;}if (isDec == true) {String stemp = "";for (k = 0; k < i; k++)stemp += s.charAt(k);text.setText(stemp);} elsetext.setText(s);}clear = false;}if (source == bEqual) {System.out.println("=");Double ss=Double.parseDouble(text.getText());text.setText("");if (fuhao == "+")text.setText(qian + ss + "");if (fuhao == "-")text.setText(qian - ss + "");if (fuhao == "*")text.setText(qian * ss + "");if (fuhao == "/")text.setText(qian / ss + "");clear = false;// 要清空前一次的数据;}}catch (Exception ee) {System.out.println("请正确输入");text.setText("运算出错,给您带来不便,sorry");clear = false;}}public static void main(String[] args) {new Calculator();}}。