import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Calculator extends JFrame{
private JTextField tf1;
private JTextField tf2;
private Container cp;
private static  String str="";
private String btn[]=
{
"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","."
};
JButton b[]=new JButton[16];

public Calculator(){
setTitle("计算器");
setSize(300,200);
cp=getContentPane();
cp.setLayout(new BorderLayout());

JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();

Font ft1=new Font("Times",Font.PLAIN,20);
Font ft2=new Font("",Font.PLAIN,14);

tf1=new JTextField(26);
jp1.add(tf1);
tf2=new JTextField(18);

JLabel label=new JLabel("22");
jp2.add(label);
label.setFont(ft2);
jp2.add(tf2);

jp3.setLayout(new GridLayout(4,4));

for(int j=0;j<btn.length;j++){
b[j]=new JButton(btn[j]);
jp3.add(b[j]);
b[j].setFont(ft1);
b[j].addActionListener(new CalButton());


}
cp.add(jp1,BorderLayout.NORTH);
cp.add(jp2,BorderLayout.SOUTH);
cp.add(jp3,BorderLayout.CENTER);

addWindowListener(new WindowDestroyer());

}
}
class WindowDestroyer extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}


}
class CalButton implements ActionListener{

     public void actionPerformed(ActionEvent e)
{
str=str+e.getActionCommand();
tf1.setText(str);
tf2.setText(e.getActionCommand());

}
}
public class ch10ex11
{
public static void main(String args[])
{
Calculator win=new Calculator();
win.setVisible(true);
}
}
一编译,运行就会提示出现如下错误:
 
F:\JAVA试验\ch10ex11.java:66: cannot resolve symbol
symbol  : variable str 
location: class CalButton
        str=str+e.getActionCommand();
                ^
F:\JAVA试验\ch10ex11.java:66: cannot resolve symbol
symbol  : variable str 
location: class CalButton
        str=str+e.getActionCommand();
                    ^
F:\JAVA试验\ch10ex11.java:67: cannot resolve symbol
symbol  : variable str 
location: class CalButton
        tf1.setText(str);
                            ^
F:\JAVA试验\ch10ex11.java:67: cannot resolve symbol
symbol  : variable tf1 
location: class CalButton
        tf1.setText(str);
                ^
F:\JAVA试验\ch10ex11.java:68: cannot resolve symbol
symbol  : variable tf2 
location: class CalButton
        tf2.setText(e.getActionCommand());
                ^
5 errors

解决方案 »

  1.   

    楼主是在写一个内部类的时候,写到了类的外边了吧!   改正如下:import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Calculator extends JFrame{
    private JTextField tf1;
    private JTextField tf2;
    private Container cp;
    private static  String str="";
    private String btn[]=
    {
    "0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","."
    }; JButton b[]=new JButton[16];public Calculator(){
    setTitle("计算器");
    setSize(300,200);
    cp=getContentPane();
    cp.setLayout(new BorderLayout()); JPanel jp1=new JPanel();
    JPanel jp2=new JPanel();
    JPanel jp3=new JPanel(); Font ft1=new Font("Times",Font.PLAIN,20);
    Font ft2=new Font("",Font.PLAIN,14); tf1=new JTextField(26);
    jp1.add(tf1);
    tf2=new JTextField(18); JLabel label=new JLabel("22");
    jp2.add(label);
    label.setFont(ft2);
    jp2.add(tf2); jp3.setLayout(new GridLayout(4,4)); for(int j=0;j<btn.length;j++)
    {
       b[j]=new JButton(btn[j]);
       jp3.add(b[j]);
       b[j].setFont(ft1);
           b[j].addActionListener(new CalButton());
        }
       
    cp.add(jp1,BorderLayout.NORTH);
    cp.add(jp2,BorderLayout.SOUTH);
    cp.add(jp3,BorderLayout.CENTER); addWindowListener(new WindowDestroyer()); }//end Calculator()
      class WindowDestroyer extends WindowAdapter{ public void windowClosing(WindowEvent e)
    {
    System.exit(0);
        }
      }//end class WindowDestroyer
      class CalButton implements ActionListener{     public void actionPerformed(ActionEvent e)
    {
    str=str+e.getActionCommand();
    tf1.setText(str);
    tf2.setText(e.getActionCommand()); }
      }//end class CalButton
    }//end class Calculatorpublic class ch10ex11
    {
    public static void main(String args[])
    {
        Calculator win=new Calculator();
         win.setVisible(true);
    }
    }