lisliefor
发贴: 287
积分: 7
|
于 2006-05-28 19:15
写得不错~ 修改的地方我都加了注释,自己看吧!
package help;
import java.awt.*; import java.awt.event.*; import java.applet.*;
/** * <p> * Title: * </p> * <p> * Description: * </p> * <p> * Copyright: Copyright 2006 * </p> * <p> * Company: * </p> * * @author unascribed * @version 1.0 */
public class Frame1 extends Frame implements ActionListener { TextField t = new TextField("", 15); Panel p1 = new Panel(); Button[] b = new Button[10]; Button bAdd = new Button("+"); Button bSub = new Button("-"); Button bMul = new Button("*"); Button bDiv = new Button("/"); Button bPoint = new Button("."); Button bEqual = new Button("="); Button bNull = new Button("清空"); //菜单条、菜单、菜单项 MenuBar bar = new MenuBar(); Menu menu = new Menu("Mmenu"); MenuItem abourt = new MenuItem("Abourt"); MenuItem help = new MenuItem("Help"); String str1 = ""; String str2 = ""; String operator = null; boolean first = true; int countOper = 0; double result = 0.0; double num1 = 0.0, num2 = 0.0; boolean error = false;
public Frame1() { t.setEditable(false); for (int i = 0; i < b.length; i++) { b[i] = new Button((i+1)+""); //这里我小改动了一下 p1.add(b[i]); b[i].setActionCommand("number"); b[i].addActionListener(this); } p1.add(bPoint); bPoint.setActionCommand("number"); p1.add(bAdd); p1.add(bSub); p1.add(bMul); p1.add(bDiv); p1.add(bEqual); p1.add(bNull); bAdd.setActionCommand("oper"); bSub.setActionCommand("oper"); bMul.setActionCommand("oper"); bDiv.setActionCommand("oper"); bAdd.addActionListener(this); bSub.addActionListener(this); bMul.addActionListener(this); bDiv.addActionListener(this); bPoint.addActionListener(this); bEqual.addActionListener(this); bNull.addActionListener(this); p1.setLayout(new GridLayout(4, 4, 5, 5)); add(t, "North"); add(p1, "Center"); setLocation(400, 200); setSize(200, 200); setBackground(Color.lightGray); setVisible(true); //自己给abourt、help添加监听器,实现你想的功能 setMenuBar(bar); bar.add(menu); menu.add(abourt); menu.add(help);
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }
public void actionPerformed(ActionEvent e) { Button temp = (Button) e.getSource(); if (e.getActionCommand().equals("number")) { if (first) { str1 = str1 + temp.getLabel(); t.setText(str1); } else { str2 = str2 + temp.getLabel(); t.setText(str2); } } else if (e.getActionCommand().equals("oper")) { if (str1 == "") { countOper = 0; first = true; } else { countOper++; if (countOper > 1) { getResult(); } operator = temp.getLabel(); first = false; } } else if (e.getActionCommand().equals("清空")) { str1 = ""; str2 = ""; t.setText(""); countOper = 0; first = true; error = false; } else if (e.getActionCommand().equals("=")) { if ((str1 == "") || (str2 == "")) { countOper = 0; first = true; } else { getResult(); countOper = 0; } } }
public void getResult() { num1 = Double.parseDouble(str1); num2 = Double.parseDouble(str2); if (operator.equals("+")) { result = num1 + num2; } else if (operator.equals("-")) { result = num1 - num2; } else if (operator.equals("*")) { result = num1 * num2; } else if (operator.equals("/")) { if (num2 == 0.0) { error = true; } else { result = num1 / num2; } } if (error) { t.setText("error"); } else { t.setText(String.valueOf(result)); str1 = String.valueOf(result); str2 = ""; } }
public static void main(String[] args) { new Frame1(); }
}
|