Difference between revisions of "Exit an application from a menu"

From CodeCodex

(Java)
 
(Implementations)
Line 51: Line 51:
 
}
 
}
 
</highlightsyntax>
 
</highlightsyntax>
 +
 +
=== Tcl ===
 +
<pre class="tcl">
 +
toplevel .menu
 +
pack .menu
 +
menu .menu.menubar
 +
.menu configure -menu .menu.menubar
 +
 +
set m .menu.menubar
 +
menu $m.file
 +
menu $m.help
 +
$m add cascade -menu $m.file -label File
 +
$m add cascade -menu $m.edit -label Help
 +
 +
$m.file add command -label "New" -command {}
 +
$m.file add command -label "Open..." -command {}
 +
$m.file add command -label "Close" -command {}
 +
$m.file add command -label "Exit" -command exit
 +
 +
$m.help add command -label "About" -command {}
 +
</pre>
 
[[Category:Java]]
 
[[Category:Java]]
 +
[[Category:Tcl]]

Revision as of 17:42, 24 February 2011

Implementations

Java

<highlightsyntax language="java122"> import java.awt.*; import java.awt.event.*;

public class ExitFromMenu extends Frame implements ActionListener {

 Menu m = new Menu("Exit From Here");
ExitFromMenu() {
  super("");
  MenuBar mb = new MenuBar();
  mb.add(m);
  MenuItem m1 = m.add(new MenuItem("Exit", 
     new MenuShortcut(KeyEvent.VK_X)));
  m1.setActionCommand("Exit");
  m.addActionListener(this);
  setMenuBar(mb);
  addWindowListener
   (new WindowAdapter() {
       public void windowClosing(WindowEvent e) {
         // unique exit point
         System.out.println("Bye.");
         System.exit(0);
         }
       }
    );
  add(new Label
     ("You can quit by clicking on the 'X'"),"South");
  add(new Label
     ("You can quit by clicking on the menu item 'Exit'"),"Center");
  add(new Label
     ("You can quit with the MenuShortcut 'ctrl-x'"),"North");
  setSize(300, 300);    
  show();
  }
public void actionPerformed(ActionEvent evt) {
  String what = evt.getActionCommand();
  if (what.equals("Exit")) 
    processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
  }
  static public void main(String[] args) {
    new ExitFromMenu();
  }

} </highlightsyntax>

Tcl

toplevel .menu
pack .menu
menu .menu.menubar
.menu configure -menu .menu.menubar

set m .menu.menubar
menu $m.file
menu $m.help
$m add cascade -menu $m.file -label File
$m add cascade -menu $m.edit -label Help

$m.file add command -label "New" -command {}
$m.file add command -label "Open..." -command {}
$m.file add command -label "Close" -command {}
$m.file add command -label "Exit" -command exit

$m.help add command -label "About" -command {}