Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing Applet via click exit button

Status
Not open for further replies.

Eytch

Programmer
Jan 29, 2003
60
0
0
US
Can anyone tell me the code to use to close an applet by clicking an exit button within the applet. System.exit(0); does end the program.
Thanks,
Tripoli
 
Here is one of my complete examples, create menubar, add items to it and add actionlistaners to the items....so they work:

Code:
import java.awt.*; 
import java.awt.event.*; 
 

public class TheMenu extends Frame
{ 
  public TheMenu()
  { 
    super("Main window, select below..."); 
    setSize(300, 80); 
    setResizable(false);
    
    FileMenu fileMenu = new FileMenu(this);  
 
    MenuBar mb = new MenuBar(); 
    
    mb.add(fileMenu);  
    setMenuBar(mb); 
    
[b]    addWindowListener(new WindowAdapter()
    { 
      public void windowClosing(WindowEvent e) { 
        exit(); 
      } 
    });
  }
   
  public void exit()
  { 
    System.out.println("Application is exiting.");
    setVisible(false);
    dispose();
    System.exit(0);
  }[/b] 
 
  public static void main(String [] args)
  { 
    TheMenu w = new TheMenu(); 
    w.setVisible(true); 
  } 
} 


class FileMenu extends Menu implements ActionListener { 
  TheMenu mw;
  public FileMenu(TheMenu m)
  { 
    super("File"); 
    mw = m; 
    
    MenuItem mi; 
    
    add(mi = new MenuItem("New entry")); 
    mi.addActionListener(this); 
    
    add(mi = new MenuItem("Find")); 
    mi.addActionListener(this); 
    
    addSeparator();
    
    [b]add(mi = new MenuItem("Exit")); 
    mi.addActionListener(this); [/b]
  } 
 
  [b]public void actionPerformed(ActionEvent e)
  { 
    String item = e.getActionCommand(); 
    
    System.out.println("You have selected: " + item); 
    [/b]
    if (item.equals("New entry"))
    {
      CardLayoutManager c1=new CardLayoutManager();
      c1.setVisible(true);
      c1.pack();
      c1.show();
    }  
    else if (item.equals("Find"))  
    {
      FindForm c2=new FindForm();
      c2.setVisible(true);
      c2.pack();
      c2.show();
    }
    [b]else     // Hit Exit  //
    {
      mw.exit(); 
    }    [/b]
  } 
}

Those in bold is what you need. Better take a look at the whole little program.


-bclt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top