HTML自行做一個吧,這應該馬上就好了
---------------------------------------------------------------------
// A simple calculator applet.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
*/
public class Calc extends Applet
implements ActionListener {
TextField expText, resText;
Parser p;
public void init() {
Label heading = new
Label("ex
Label explab = new Label("ex
Label reslab = new Label("Result ", Label.CENTER);
expText = new TextField(24);
resText = new TextField(24);
resText.setEditable(false); // result field for display only
add(heading);
add(explab);
add(expText);
add(reslab);
add(resText);
/* Register ex
to receive action events. */
expText.addActionListener(this);
// create parser
p = new Parser();
}
// User pressed Enter.
public void actionPerformed(ActionEvent ae) {
repaint();
}
public void paint(Graphics g) {
double result = 0.0;
String expstr = expText.getText();
try {
if(expstr.length() != 0)
result = p.evaluate(expstr);
// To clear ex
// use the folloing line:
// expText.setText("");
resText.setText(Double.toString(result));
showStatus(""); // erase any previous error message
} catch (ParserException exc) {
showStatus(exc.toString());
resText.setText("");
}
}
}
---------------------------------------------------------------------
- May 13 Sun 2007 19:08
<Java-高階>關於解析器(Parser)-使用此Parser建立一個Applet的計算機
close
全站熱搜
留言列表