📄 menudemo.java
字号:
package com.javaworld.mar2000.sax;
/*
* Sample code for "SAX Appeal", by Mark Johnson, JavaWorld, March 2000.
* Code is may be used for any legal purpose, including commercial
* purposes, with no warranty expressed or implied.
* email: mark.johnson@javaworld.com
*/
import java.awt.*;
import java.awt.event.*;
/**
* Demo class that shows how to use Menu XML.
*/
public class MenuDemo implements MenuItemHandler {
Button _b1;
/**
* Main constructor
*/
public MenuDemo() {
super();
}
/**
* Takes appropriate action when a menu item is activated.
*/
public void itemActivated(MenuItem item, ActionEvent e, String sCommand) {;
System.out.println("Activated: " + sCommand);
if (sCommand.equals("Exit")) {
System.exit(0);
}
}
/**
* Reports that a CheckboxMenuItem has been deactivated.
* @param e java.awt.event.ActionEvent
*/
public void itemDeselected(MenuItem item, ItemEvent e, String sCommand) {
System.out.println("Deselected: " + sCommand);
if (sCommand.equals("Disable Button 1")) {
_b1.setEnabled(true);
}
}
/**
* Reports that a CheckboxMenuItem has been deactivated.
* @param e java.awt.event.ActionEvent
*/
public void itemSelected(MenuItem item, ItemEvent e, String sCommand) {;
System.out.println("Selected: " + sCommand);
if (sCommand.equals("Disable Button 1")) {
_b1.setEnabled(false);
}
}
/**
* Demonstrates how to use "Menu XML"
*/
public static void main(String args[]) {
MenuDemo md = new MenuDemo();
md.runDemo(args);
}
/**
* Builds a menu in code and displays it. This method exists only
* to show the sort of code that Menu XML replaces.
*/
public void manualMenuDemo(String[] args) {
MenuBar menubarTop = new MenuBar();
Menu menuFile = new Menu("File");
Menu menuEdit = new Menu("Edit");
menubarTop.add(menuFile);
menubarTop.add(menuEdit);
menuFile.add(new MenuItem("Open"));
menuFile.add(new MenuItem("Close"));
menuFile.add(new MenuItem("And so on..."));
menuEdit.add(new MenuItem("Cut"));
menuEdit.add(new MenuItem("Paste"));
menuEdit.add(new MenuItem("Delete"));
Frame frame = new Frame("ManualMenuDemo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setMenuBar(menubarTop);
frame.pack();
frame.show();
}
/**
* main() calls this method to run the demo.
*/
public void runDemo(String[] args) {
SaxMenuLoader sml = new SaxMenuLoader();
// Bind names of handlers to the MenuItemHandlers they represent
sml.registerMenuItemHandler("FileHandler", this);
sml.registerMenuItemHandler("EditHandler", this);
sml.registerMenuItemHandler("HelpHandler", this);
sml.registerMenuItemHandler("PopupHandler", this);
sml.registerMenuItemHandler("SubMenu1Handler", this);
sml.registerMenuItemHandler("Button1Enabler", this);
// Parse the file
sml.loadMenus(args[0]);
// If menu load succeeded, show the menu in a frame
MenuBar menubarTop = sml.menubarFind(args[1]);
if (menubarTop != null) {
Frame frame = new Frame("Menu demo 1");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setMenuBar(menubarTop);
_b1 = new Button("Button");
_b1.addMouseListener(new MenuPopper(_b1, sml, "Pop1"));
frame.add(_b1);
frame.pack();
frame.show();
} else {
System.out.println(args[1] + ": no such menu");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -