Wednesday, April 27, 2011

rewritten using inner classes


import java.awt.*;
   import java.awt.event.*;
   //Top-most class
   class ImageViewer {
      public static final void main(String[] args){
         ImageViewer im = new ImageViewer();
      }
      public ImageViewer(){
         CommandPanel cp = new CommandPanel();
      }
      public void displayImage(){
         // Code to display an image
      }
      class CommandPanel extends Frame{
         CommandPanel(){
            createGUI();
         }
         private void createGUI(){
            setLayout(new FlowLayout());
            Button dispBtn = new Button("Display");
            DisplayCommand dispC = new DisplayCommand();
            dispBtn.addActionListener(dispC);
            // Other components
            add(dispBtn);
            pack();
            setSize(100,100);
            setVisible(true);
         }
         class DisplayCommand implements ActionListener {
            public void actionPerformed(ActionEvent e){
               displayImage();
            }
         } // End of DisplayCommand
      } // End of CommandPanel
    } // End of ImageViewer

No comments:

Post a Comment