import java.awt.*;
import java.awt.event.*;public class jsq {
private static Object color;public static void main(String[] args) {
JsqFrame f = new JsqFrame();
f.setBackground(Color.pink);}
}class JsqFrame extends Frame {
double d1, d2;
int op = -1;
TextField tf;
CalPanelL p1;
//CalPanelR p2;//Constructor
JsqFrame() {
super("简易计算器-王均亮制作"); setLayout(new FlowLayout());
setResizable(false);
setSize(280,290);
tf = new TextField(19);
tf.setEditable(false);
tf.setBackground(new Color(108,118,103));
tf.setForeground(Color.white);
tf.setFont(new Font("Arial",Font.BOLD,16));
add(tf);
p1 = new CalPanelL();
// p2 = new CalPanelR();
add(p1);
// add(p2);
setVisible(true);
}
class CalButton extends Button {
CalButton(String s){
super(s);
setBackground(Color.gray);
}
}
class CalPanelL extends Panel {
CalButton b0, b1, b2, b3,
b4, b5, b6, b7,
b8, b9, bPN, bPoint;
CalButton bAdd, bSub, bMul, bDiv,
bLn, bEqual, bCE, bQuit;
CalPanelL() {
setLayout(new GridLayout(6,3));
setFont(new Font("TimesRoman",Font.BOLD,16));
b0 = new CalButton("0");
b1 = new CalButton("1");
b2 = new CalButton("2");
b3 = new CalButton("3");
b4 = new CalButton("4");
b5 = new CalButton("5");
b6 = new CalButton("6");
b7 = new CalButton("7");
b8 = new CalButton("8");
b9 = new CalButton("9");
bPN = new CalButton("+/-");
bPoint = new CalButton(".");
bAdd = new CalButton("+");
bSub = new CalButton("-");
bMul = new CalButton("*");
bDiv = new CalButton("/");
bLn = new CalButton("ln");
bEqual = new CalButton("=");
bCE = new CalButton("CE");
bQuit = new CalButton("退出");
// 加入按钮
add(b7); b7.addActionListener(new PressB7());
add(b8); b8.addActionListener(new PressB8());
add(b9); b9.addActionListener(new PressB9()); add(bAdd); bAdd.addActionListener(new PressBAdd());
add(b4); b4.addActionListener(new PressB4());
add(b5); b5.addActionListener(new PressB5());
add(b6); b6.addActionListener(new PressB6());
add(bSub); bSub.addActionListener(new PressBSub());
add(b1); b1.addActionListener(new PressB1());
add(b2); b2.addActionListener(new PressB2());
add(b3); b3.addActionListener(new PressB3());
add(bMul); bMul.addActionListener(new PressBMul());
add(b0); b1.addActionListener(new PressB0()); add(bPoint); bPoint.addActionListener(new PressBPoint());
add(bPN); bPN.addActionListener(new PressBPN()); ;
add(bDiv); bDiv.addActionListener(new PressBDiv()); add(bCE); bCE.addActionListener(new PressBCE()); add(bLn); bLn.addActionListener(new PressBLn()); add(bEqual); bEqual.addActionListener(new PressBEqual()); add(bQuit); bQuit.addActionListener(new PressBQuit());
}
}
class PressBAdd implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
d1 = Double.parseDouble(tf.getText());
op = 0;
tf.setText("");
} catch(Exception ee) {}
}
}class PressBSub implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
d1 = Double.parseDouble(tf.getText());
op = 1;
tf.setText("");
} catch(Exception ee) {}
}
}
class PressBMul implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
d1 = Double.parseDouble(tf.getText());
op = 2;
tf.setText("");
} catch(Exception ee) {}
}
}class PressBDiv implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
d1 = Double.parseDouble(tf.getText());
op = 3;
tf.setText("");
} catch(Exception ee) {}
}
}class PressBEqual implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double result = 0;
d2 = Double.parseDouble(tf.getText());
switch(op) {
case 0:
result = d1 + d2;
break;
case 1:
result = d1 - d2;
break;
case 2:
result = d1 * d2;
break;
case 3:
result = d1 / d2;
break;
case 4:
result = Math.pow(d1,d2);
break;
default:
}
tf.setText(String.valueOf(result));
} catch(Exception ee) {}
}
}class PressBLn implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double x = Double.parseDouble(tf.getText());
double y;
y = Math.log(x);
tf.setText(y+"");
} catch(Exception ee) {}
}
}class PressBCE implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf.setText("");
}
}class PressBPN implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String text = tf.getText();
if (text != "") {
if(text.charAt(0) == '-')
tf.setText(text.substring(1));
else if(text.charAt(0) >= '0' && text.charAt(0) <= '9')
tf.setText("-"+text.substring(0));
else if(text.charAt(0) == '.')
tf.setText("-0"+text.substring(0));
}
} catch(Exception ee) { }
}
}class PressBPoint implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
if(text.lastIndexOf(".") == -1)
tf.setText(text+".");
}
}
class PressB0 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"0");
}
}class PressB1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"1");
}
}class PressB2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"2");
}
}class PressB3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"3");
}
}class PressB4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"4");
}
}class PressB5 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"5");
}
}class PressB6 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"6");
}
}class PressB7 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"7");
}
}class PressB8 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"8");
}
}class PressB9 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text+"9");
}
}class PressBQuit implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}}
谁能帮我注释下啊!有挺多地方我不明白是什么意思
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货