import java.awt.*;
import java.awt.event.*;
public class Test {
 public static void main(String[] args) {
   new TM("简易计算器");
   }
}  
   
class TM extends Frame {
   public TM(String s) {
    super(s);
TextField tf1 =new TextField(10);
  Panel p1 = new Panel();
Panel p11 = new Panel(new BorderLayout());
Panel p2 = new Panel();
TextField tf2 = new TextField(10);
TextField tf3 = new TextField(20);
Button b = new Button("=");
Button a1 = new Button("+");
Button a2 = new Button("-");
Button a3 = new Button("*");
Button a4 = new Button("/");
setLayout(new GridLayout(2,1));
TextField tf4 = new TextField(15);
p11.add(a1,BorderLayout.NORTH);
p11.add(a2,BorderLayout.SOUTH);
p11.add(a3,BorderLayout.WEST);
p11.add(a4,BorderLayout.EAST);
p1.add(tf1);
p1.add(p11);
p1.add(tf2);
p1.add(b);
p1.add(tf3);
p2.add(tf4);
  add(p1);
  add(p2);
setVisible(true);
  b.addActionListener(new Monitor(tf1,tf2,tf3,tf4));
  a1.addActionListener(new Monitor1(tf4));
  a2.addActionListener(new Monitor1(tf4));
  a3.addActionListener(new Monitor1(tf4));
  a4.addActionListener(new Monitor1(tf4));
  tf4.setVisible(false);
     this.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e)  {
   setVisible(false);
   System.exit(0);
   }
  });
  
}  
  } 
class Monitor implements ActionListener {
TextField tf1,tf2,tf3,tf4;
public Monitor(TextField tf1,TextField tf2,TextField tf3,TextField tf4) {
this.tf1 = tf1;
this.tf2 = tf2;
this.tf3 = tf3;
this.tf4 = tf4;
 
}
public void actionPerformed(ActionEvent e) {
double n1 = Double.parseDouble(tf1.getText());   
double n2 = Double.parseDouble(tf2.getText());
String s = tf1.getText();
  for(int i = 0; i<s.length; i++) {
  char c = s.charAt(i);
  if(c>='a' && c<='z') {
  tf3.setText("" +"输入出错");
  }
  else if(c>='A' && C<='Z') {
  tf3.setText("" +"输入出错");
  }
 
  }

if(tf4.getText().equals("+")) {
    tf3.setText("" +(n1+n2));
      }
if(tf4.getText().equals("-")) {
tf3.setText("" +(n1-n2));
}
if(tf4.getText().equals("*")) {
tf3.setText("" +(n1*n2));
}
if(tf4.getText().equals("/")) {
tf3.setText("" +(n1/n2));
}


}
}  

class Monitor1 implements ActionListener {
TextField tf4;
public Monitor1(TextField tf4) {
 this.tf4 = tf4;
}
public void actionPerformed(ActionEvent e) {
 tf4.setText(e.getActionCommand()); 
 
}
}    

解决方案 »

  1.   


    public void actionPerformed(ActionEvent e) {
    double n1 = Double.parseDouble(tf1.getText());//在这里已经出现异常,不会继续下面的判断   
    double n2 = Double.parseDouble(tf2.getText());
    String s = tf1.getText();
    for(int i = 0; i<s.length; i++) {
    char c = s.charAt(i);
    if(c>='a' && c<='z') {
    tf3.setText("" +"输入出错");
    }
    else if(c>='A' && C<='Z') {
    tf3.setText("" +"输入出错");
    }改为double n1 = 0;
    double n2 = 0;
    try{
    n1 = Double.parseDouble(tf1.getText()); 
    }
    catch(Exception e1){

    }
    try{
    n2 = Double.parseDouble(tf2.getText()); 
    }
    catch(Exception e1){

    }
      

  2.   

    感谢qianzhimeiying回答
    但还是不行啊,不信你改成你的运行试下
    我用你的代码运行结果:如果前面框输入了含有字母,后面第三个输入框结果就不是预期的“输入出错”,而是其他不可预期的结果。
    还望大哥帮人帮到底,小弟在此先只有诚挚的谢意了。
      

  3.   


    import java.awt.*;
    import java.awt.event.*;public class Test {
    public static void main(String[] args) {
    new TM("简易计算器");
    }
    }class TM extends Frame {
    public TM(String s) {
    super(s);
    TextField tf1 = new TextField(10);
    Panel p1 = new Panel();
    Panel p11 = new Panel(new BorderLayout());
    Panel p2 = new Panel();
    TextField tf2 = new TextField(10);
    TextField tf3 = new TextField(20);
    Button b = new Button("=");
    Button a1 = new Button("+");
    Button a2 = new Button("-");
    Button a3 = new Button("*");
    Button a4 = new Button("/");
    setLayout(new GridLayout(2, 1));
    TextField tf4 = new TextField(15);
    p11.add(a1, BorderLayout.NORTH);
    p11.add(a2, BorderLayout.SOUTH);
    p11.add(a3, BorderLayout.WEST);
    p11.add(a4, BorderLayout.EAST);
    p1.add(tf1);
    p1.add(p11);
    p1.add(tf2);
    p1.add(b);
    p1.add(tf3);
    p2.add(tf4);
    add(p1);
    add(p2);
    setVisible(true);
    b.addActionListener(new Monitor(tf1, tf2, tf3, tf4));
    a1.addActionListener(new Monitor1(tf4));
    a2.addActionListener(new Monitor1(tf4));
    a3.addActionListener(new Monitor1(tf4));
    a4.addActionListener(new Monitor1(tf4));
    tf4.setVisible(false);
    this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    setVisible(false);
    System.exit(0);
    }
    }); }
    }class Monitor implements ActionListener {
    TextField tf1, tf2, tf3, tf4; public Monitor(TextField tf1, TextField tf2, TextField tf3, TextField tf4) {
    this.tf1 = tf1;
    this.tf2 = tf2;
    this.tf3 = tf3;
    this.tf4 = tf4; } public void actionPerformed(ActionEvent e) {
    double n1 = 0;
    try {
    n1 = Double.parseDouble(tf1.getText());
    } catch (Exception e1) { }
    double n2 = 0;
    try {
    n2 = Double.parseDouble(tf2.getText());
    } catch (Exception e1) { }
    String s = tf1.getText();
    for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c >= 'a' && c <= 'z') {
    tf3.setText("" + "输入出错");
    return;
    } else if (c >= 'A' && c <= 'Z') {
    tf3.setText("" + "输入出错");
    return;
    }
    } if (tf4.getText().equals("+")) {
    tf3.setText("" + (n1 + n2));
    }
    if (tf4.getText().equals("-")) {
    tf3.setText("" + (n1 - n2));
    }
    if (tf4.getText().equals("*")) {
    tf3.setText("" + (n1 * n2));
    }
    if (tf4.getText().equals("/")) {
    tf3.setText("" + (n1 / n2));
    } }
    }class Monitor1 implements ActionListener {
    TextField tf4; public Monitor1(TextField tf4) {
    this.tf4 = tf4;
    } public void actionPerformed(ActionEvent e) {
    tf4.setText(e.getActionCommand()); }
    }
      

  4.   

    非常感谢qianzhimeiying
    你真是一个好人,我没有什么可以回报的,只有奉上分数和我诚挚的谢意,希望你能听见