你这个程序有许多的错误.
public Container container;
container=getContentPane();
你把上两句改为:
Container c=getContentPane();
试一下.
你最好把整个程序给我发过来.
import java.awt.*;
import java.awt.event.*;
public class Homework2 extends Frame implements ActionListener,KeyListener
{
Label label;
Button n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,addition,subtraction,multiplication,division,equle,C;
Font f;
char ch;
String regA,regB;
char op;
double sum;
Homework2()
{
setTitle("简易计算器");
setLayout(null);
regA=new String("");
regB=new String("");
label=new Label("0",Label.RIGHT);
label.setBackground(Color.black);
f=new Font("arial",Font.BOLD,12);
label.setForeground(Color.white);
label.setFont(f);
n1=new Button("1");
n2=new Button("2");
n3=new Button("3");
n4=new Button("4");
n5=new Button("5");
n6=new Button("6");
n7=new Button("7");
n8=new Button("8");
n9=new Button("9");
n0=new Button("0");
addition=new Button("+");
multiplication=new Button("×");
subtraction=new Button("-");
division=new Button("÷");
equle=new Button("=");
C=new Button("C");
add(label);
add(n1);
add(n2);
add(n3);
add(n4);
add(n5);
add(n6);
add(n7);
add(n8);
add(n9);
add(n0);
add(addition);
add(subtraction);
add(multiplication);
add(subtraction);
add(division);
add(equle);
add(C);
n1.addActionListener(this);
n2.addActionListener(this);
n3.addActionListener(this);
n4.addActionListener(this);
n5.addActionListener(this);
n6.addActionListener(this);
n7.addActionListener(this);
n8.addActionListener(this);
n9.addActionListener(this);
n0.addActionListener(this);
addition.addActionListener(this);
subtraction.addActionListener(this);
multiplication.addActionListener(this);
division.addActionListener(this);
equle.addActionListener(this);
C.addActionListener(this);
n1.addKeyListener(this);
n2.addKeyListener(this);
n3.addKeyListener(this);
n4.addKeyListener(this);
n5.addKeyListener(this);
n6.addKeyListener(this);
n7.addKeyListener(this);
n8.addKeyListener(this);
n9.addKeyListener(this);
n0.addKeyListener(this);
addition.addKeyListener(this);
subtraction.addKeyListener(this);
multiplication.addKeyListener(this);
division.addKeyListener(this);
equle.addKeyListener(this);
C.addKeyListener(this);
label.setBounds(30,50,115,25);
n1.setBounds(30,100,25,25);
n2.setBounds(60,100,25,25);
n3.setBounds(90,100,25,25);
n4.setBounds(30,130,25,25);
n5.setBounds(60,130,25,25);
n6.setBounds(90,130,25,25);
n7.setBounds(30,160,25,25);
n8.setBounds(60,160,25,25);
n9.setBounds(90,160,25,25);
n0.setBounds(60,190,25,25);
addition.setBounds(120,100,25,25);
subtraction.setBounds(120,130,25,25);
multiplication.setBounds(120,160,25,25);
division.setBounds(120,190,25,25);
equle.setBounds(90,190,25,25);
C.setBounds(30,190,25,25);
addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
});
setSize(179,250);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==n1)
{ch='1';}
else if(e.getSource()==n2)
{ch='2';}
else if(e.getSource()==n3)
{ch='3';}
else if(e.getSource()==n4)
{ch='4';}
else if(e.getSource()==n5)
{ch='5';}
else if(e.getSource()==n6)
{ch='6';}
else if(e.getSource()==n7)
{ch='7';}
else if(e.getSource()==n8)
{ch='8';}
else if(e.getSource()==n9)
{ch='9';}
else if(e.getSource()==n0)
{ch='0';}
else if(e.getSource()==addition)
{ch='+';}
else if(e.getSource()==subtraction)
{ch='-';}
else if(e.getSource()==multiplication)
{ch='*';}
else if(e.getSource()==division)
{ch='/';}
else if(e.getSource()==equle)
{ch='=';}
else if(e.getSource()==C)
{ch='c';}
processEvent(ch);
}
public void keyTyped(KeyEvent e)
{
char ch=e.getKeyChar();
processEvent(ch);
}
public void keyPressed(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
public void processEvent(char ch)
{
switch(ch)
{
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
regA=regA+ch;
label.setText(regA);
break;
case 'c':case 'C':
regA="";
regB="";
label.setText("0");
break;
case '=':
getAnswer(regA,regB);
label.setText(String.valueOf(sum));
break;
default:
regB=regA;
op=ch;
regA="";
regB=label.getText();
break;
}
}
public double getAnswer(String regA,String regB)
{
double num1,num2;
num1=Double.parseDouble(regA);
num2=Double.parseDouble(regB);
switch(op)
{
case '+':
sum=num1+num2;
break;
case '-':
sum=num2-num1;
break;
case '*':
sum=num2*num1;
break;
case '/':
sum=num2/num1;
break;
}
return sum;
}
public static void main(String args[])
{
Homework2 hw2=new Homework2();
}
}
方法内的变量前不能用public private protected修饰