内部类

来源:互联网 发布:诸葛亮网络用语含义 编辑:程序博客网 时间:2024/04/30 08:23

class  TFMath
{
 public static void main(String[] args)
 {
  new TFMath().launchFrame();
 }
}

class myFrame extends Frame
{
 public void launchFrame(){
  TextField num1 = new TextField(10);
  TextField num2 = new TextField(10);
        TextField num3 = new TextField(15);
  Button btaEqual = new Button(" ");
  Label mylabel = new Label("+");  //文本编辑
        btaEqual.addActionListener(new mymonitor());
  setLayout(new FlowLayout());     //流水线式布局
  add(num1);
  add(num2);
  add(num3);
  add(btaEqual);
  add(mylabel);
  pack();
  setVisible(true);

 }
  //内部类,归 myFrame 类调用,即只允许外部包装类访问
  //可以轻松地访问外部包装类的成员变量
  class mymonitor{  
 public void actionPerformed(ActionEvent e){
  int n1 = Integer.parseInt(f.num1.getText());
  int n2 = Integer.parseInt(f.num2.getText());
  f.num3.setText(" " + (n1 + n2));
   }
}

 

原创粉丝点击