这个程序的框架是网上找来的,自己改了些东西。实现了三种不同类型的菜单,并且实现了菜单的快捷键。但是个人感觉不太好,并且无法进行VIsual的操作。打算今天再作一个用SWT的,代码明天晚上之前放上来。
/**
* @author bestseal
* @date 2007.10.10
* @version 1.0.0
* a menu programme using awt
*
*/
import java.awt.*;
import java.awt.event.KeyEvent;
public class MenuTest extends Frame {
/*name all the items for the Edit menu*/
String[] ArrayEdit= { "Copy", "Paste", "Delete","Find"};
/* name the textfiled*/
TextField text = new TextField("This is a Menu programme.",80); // @jve:decl-index=0:visual-constraint="18,126"
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("File");
Menu menuEdit = new Menu("Edit");
Menu menuSave= new Menu("Save");
/* add check box:*/
CheckboxMenuItem[] checkboxSave = { new CheckboxMenuItem("Save"),
new CheckboxMenuItem("Save as") };
/*add keyboard shortcuts*/
MenuShortcut oShortcut= new MenuShortcut(KeyEvent.VK_O, true); /*if true, add shift*/ // @jve:decl-index=0:
MenuShortcut eShortcut = new MenuShortcut(KeyEvent.VK_E, false);/*false with out shift*/ // @jve:decl-index=0:
MenuItem[] ArrayFile = { new MenuItem("Open",oShortcut ) ,
new MenuItem("Exit",eShortcut) };
/*function: constructor function*/
public MenuTest()
{
/* add menu to EditMenu*/
for (int i = 0; i < ArrayEdit.length; i++)
{
menuEdit.add(new MenuItem(ArrayEdit[i]));
/*every two item add a list separator */
if ((i+1)% 2 == 0)
{
menuEdit.addSeparator();
}
}
/*add menu to checkbox Save*/
for (int i = 0; i < checkboxSave.length; i++)
{
menuSave.add(checkboxSave[i]);
}
menuFile.add(menuSave);
/*add menu to FileMenu*/
for (int i = 0; i < ArrayFile.length; i++)
{
menuFile.add(ArrayFile[i]);
}
/* compose the frame and the menu*/
menuBar.add(menuFile);
menuBar.add(menuEdit);
add("Center",text);
setMenuBar(menuBar);
/*textfiled read only*/
text.setEditable(false);
}
/*fucnction :close windows to exit*/
public boolean handleEvent(Event evt)
{
if (evt.id == Event.WINDOW_DESTROY)
System.exit(0);
else
return super.handleEvent(evt);
return true;
}
/*function: action the menu*/
public boolean action(Event evt, Object arg) {
/*test if target is a instance of Menuitem*/
if (evt.target instanceof MenuItem)
{
if (arg.equals("Open"))
{
String str = text.getText();
boolean chosen = false;
for (int i = 0; i < ArrayEdit.length; i++)
if (str.equals(ArrayEdit[i]))
chosen = true;
if (!chosen)
text.setText("Choose a Menu!");
else
text.setText("Opening " + str );
}
else if (evt.target.equals(ArrayFile[1]))
System.exit(0); /*exit programme*/
/*CheckboxMenuItems cannot use String
* matching; you must match the target:
* */
else if (evt.target.equals(checkboxSave[0]))
text.setText("saving...."+ checkboxSave[0].getState());
else if (evt.target.equals(checkboxSave[1]))
text.setText("save as..."+ checkboxSave[1].getState());
else
text.setText(arg.toString());
} else
return super.action(evt, arg);
return true;
}
public static void main(String[] args) {
MenuTest menu = new MenuTest();
menu.resize(600, 400);
menu.show();
}
} // @jve:decl-index=0:visual-constraint="20,17"
* @author bestseal
* @date 2007.10.10
* @version 1.0.0
* a menu programme using awt
*
*/
import java.awt.*;
import java.awt.event.KeyEvent;
public class MenuTest extends Frame {
/*name all the items for the Edit menu*/
String[] ArrayEdit= { "Copy", "Paste", "Delete","Find"};
/* name the textfiled*/
TextField text = new TextField("This is a Menu programme.",80); // @jve:decl-index=0:visual-constraint="18,126"
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("File");
Menu menuEdit = new Menu("Edit");
Menu menuSave= new Menu("Save");
/* add check box:*/
CheckboxMenuItem[] checkboxSave = { new CheckboxMenuItem("Save"),
new CheckboxMenuItem("Save as") };
/*add keyboard shortcuts*/
MenuShortcut oShortcut= new MenuShortcut(KeyEvent.VK_O, true); /*if true, add shift*/ // @jve:decl-index=0:
MenuShortcut eShortcut = new MenuShortcut(KeyEvent.VK_E, false);/*false with out shift*/ // @jve:decl-index=0:
MenuItem[] ArrayFile = { new MenuItem("Open",oShortcut ) ,
new MenuItem("Exit",eShortcut) };
/*function: constructor function*/
public MenuTest()
{
/* add menu to EditMenu*/
for (int i = 0; i < ArrayEdit.length; i++)
{
menuEdit.add(new MenuItem(ArrayEdit[i]));
/*every two item add a list separator */
if ((i+1)% 2 == 0)
{
menuEdit.addSeparator();
}
}
/*add menu to checkbox Save*/
for (int i = 0; i < checkboxSave.length; i++)
{
menuSave.add(checkboxSave[i]);
}
menuFile.add(menuSave);
/*add menu to FileMenu*/
for (int i = 0; i < ArrayFile.length; i++)
{
menuFile.add(ArrayFile[i]);
}
/* compose the frame and the menu*/
menuBar.add(menuFile);
menuBar.add(menuEdit);
add("Center",text);
setMenuBar(menuBar);
/*textfiled read only*/
text.setEditable(false);
}
/*fucnction :close windows to exit*/
public boolean handleEvent(Event evt)
{
if (evt.id == Event.WINDOW_DESTROY)
System.exit(0);
else
return super.handleEvent(evt);
return true;
}
/*function: action the menu*/
public boolean action(Event evt, Object arg) {
/*test if target is a instance of Menuitem*/
if (evt.target instanceof MenuItem)
{
if (arg.equals("Open"))
{
String str = text.getText();
boolean chosen = false;
for (int i = 0; i < ArrayEdit.length; i++)
if (str.equals(ArrayEdit[i]))
chosen = true;
if (!chosen)
text.setText("Choose a Menu!");
else
text.setText("Opening " + str );
}
else if (evt.target.equals(ArrayFile[1]))
System.exit(0); /*exit programme*/
/*CheckboxMenuItems cannot use String
* matching; you must match the target:
* */
else if (evt.target.equals(checkboxSave[0]))
text.setText("saving...."+ checkboxSave[0].getState());
else if (evt.target.equals(checkboxSave[1]))
text.setText("save as..."+ checkboxSave[1].getState());
else
text.setText(arg.toString());
} else
return super.action(evt, arg);
return true;
}
public static void main(String[] args) {
MenuTest menu = new MenuTest();
menu.resize(600, 400);
menu.show();
}
} // @jve:decl-index=0:visual-constraint="20,17"