単純なプログラムを書換えよう(Java編)−無名クラス

ActionListenerとWindowAdapterを無名クラスにします.

//Main.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

public class Main{
  class View{
    private Label value=new Label("0");

    public View(){
      Frame frame=new Frame();
      Panel buttons=new Panel();
      Button inc=new Button("inc");
      Button dec=new Button("dec");
      buttons.add(inc);
      buttons.add(dec);
      inc.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent actionEvent){
          value.setText(Integer.toString(Integer.parseInt(value.getText())+1));
        }
      });
      dec.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent actionEvent){
          value.setText(Integer.toString(Integer.parseInt(value.getText())-1));
        }
      });
      frame.add(buttons,BorderLayout.SOUTH);
      frame.add(value,BorderLayout.CENTER);
      frame.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent windowEvent){
          System.exit(0);
        }
      });
      frame.pack();
      frame.show();
    }
  }
  public static void main(String[] args){
    Main.View view=new Main().new View();
  }
}
//end

Viewも無名クラスにしましょう.

//Main.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

public class Main{
  public static void main(String[] args){
    Main view=new Main(){
      private Label value=new Label("0");
      {
        Frame frame=new Frame();
        Panel buttons=new Panel();
        Button inc=new Button("inc");
        Button dec=new Button("dec");
        buttons.add(inc);
        buttons.add(dec);
        inc.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent actionEvent){
            value.setText(Integer.toString(Integer.parseInt(value.getText())+1));
          }
        });
        dec.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent actionEvent){
            value.setText(Integer.toString(Integer.parseInt(value.getText())-1));
          }
        });
        frame.add(buttons,BorderLayout.SOUTH);
        frame.add(value,BorderLayout.CENTER);
        frame.addWindowListener(new WindowAdapter(){
          public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
          }
        });
        frame.pack();
        frame.show();
      }
    };
  }
}
//end