复制代码
- //:Menus.java
- //Amenu-buildingsystem;alsodemonstrates
- //iconsinlabelsandmenuitems.
- packagec13.swing;
- importjava.awt.*;
- importjava.awt.event.*;
- importjavax.swing.*;
- publicclassMenusextendsJPanel{
- staticfinalBoolean
- bT=newBoolean(true),
- bF=newBoolean(false);
- //Dummyclasstocreatetypeidentifiers:
- staticclassMType{MType(inti){}};
- staticfinalMType
- mi=newMType(1),//Normalmenuitem
- cb=newMType(2),//Checkboxmenuitem
- rb=newMType(3);//Radiobuttonmenuitem
- JTextFieldt=newJTextField(10);
- JLabell=newJLabel("IconSelected",
- Faces.faces[0],JLabel.CENTER);
- ActionListenera1=newActionListener(){
- publicvoidactionPerformed(ActionEvente){
- t.setText(
- ((JMenuItem)e.getSource()).getText());
- }
- };
- ActionListenera2=newActionListener(){
- publicvoidactionPerformed(ActionEvente){
- JMenuItemmi=(JMenuItem)e.getSource();
- l.setText(mi.getText());
- l.setIcon(mi.getIcon());
- }
- };
- //Storemenudataas"resources":
- publicObject[][]fileMenu={
- //Menunameandaccelerator:
- {"File",newCharacter(F)},
- //Nametypeaccellistenerenabled
- {"New",mi,newCharacter(N),a1,bT},
- {"Open",mi,newCharacter(O),a1,bT},
- {"Save",mi,newCharacter(S),a1,bF},
- {"SaveAs",mi,newCharacter(A),a1,bF},
- {null},//Separator
- {"Exit",mi,newCharacter(x),a1,bT},
- };
- publicObject[][]editMenu={
- //Menuname:
- {"Edit",newCharacter(E)},
- //Nametypeaccellistenerenabled
- {"Cut",mi,newCharacter(t),a1,bT},
- {"Copy",mi,newCharacter(C),a1,bT},
- {"Paste",mi,newCharacter(P),a1,bT},
- {null},//Separator
- {"SelectAll",mi,newCharacter(l),a1,bT},
- };
- publicObject[][]helpMenu={
- //Menuname:
- {"Help",newCharacter(H)},
- //Nametypeaccellistenerenabled
- {"Index",mi,newCharacter(I),a1,bT},
- {"Usinghelp",mi,newCharacter(U),a1,bT},
- {null},//Separator
- {"About",mi,newCharacter(t),a1,bT},
- };
- publicObject[][]optionMenu={
- //Menuname:
- {"Options",newCharacter(O)},
- //Nametypeaccellistenerenabled
- {"Option1",cb,newCharacter(1),a1,bT},
- {"Option2",cb,newCharacter(2),a1,bT},
- };
- publicObject[][]faceMenu={
- //Menuname:
- {"Faces",newCharacter(a)},
- //Optinallastelementisicon
- {"Face0",rb,newCharacter(0),a2,bT,
- Faces.faces[0]},
- {"Face1",rb,newCharacter(1),a2,bT,
- Faces.faces[1]},
- {"Face2",rb,newCharacter(2),a2,bT,
- Faces.faces[2]},
- {"Face3",rb,newCharacter(3),a2,bT,
- Faces.faces[3]},
- {"Face4",rb,newCharacter(4),a2,bT,
- Faces.faces[4]},
- };
- publicObject[]menuBar={
- fileMenu,editMenu,faceMenu,
- optionMenu,helpMenu,
- };
- staticpublicJMenuBar
- createMenuBar(Object[]menuBarData){
- JMenuBarmenuBar=newJMenuBar();
- for(inti=0;i<menuBarData.length;i++)
- menuBar.add(
- createMenu((Object[][])menuBarData[i]));
- returnmenuBar;
- }
- staticButtonGroupbgroup;
- staticpublicJMenu
- createMenu(Object[][]menuData){
- JMenumenu=newJMenu();
- menu.setText((String)menuData[0][0]);
- menu.setMnemonic(
- ((Character)menuData[0][1]).charValue());
- //Createredundantly,incasethereare
- //anyradiobuttons:
- bgroup=newButtonGroup();
- for(inti=1;i<menuData.length;i++){
- if(menuData[i][0]==null)
- menu.add(newJSeparator());
- else
- menu.add(createMenuItem(menuData[i]));
- }
- returnmenu;
- }
- staticpublicJMenuItem
- createMenuItem(Object[]data){
- JMenuItemm=null;
- MTypetype=(MType)data[1];
- if(type==mi)
- m=newJMenuItem();
- elseif(type==cb)
- m=newJCheckBoxMenuItem();
- elseif(type==rb){
- m=newJRadioButtonMenuItem();
- bgroup.add(m);
- }
- m.setText((String)data[0]);
- m.setMnemonic(
- ((Character)data[2]).charValue());
- m.addActionListener(
- (ActionListener)data[3]);
- m.setEnabled(
- ((Boolean)data[4]).booleanValue());
- if(data.length==6)
- m.setIcon((Icon)data[5]);
- returnm;
- }
- Menus(){
- setLayout(newBorderLayout());
- add(createMenuBar(menuBar),
- BorderLayout.NORTH);
- JPanelp=newJPanel();
- p.setLayout(newBorderLayout());
- p.add(t,BorderLayout.NORTH);
- p.add(l,BorderLayout.CENTER);
- add(p,BorderLayout.CENTER);
- }
- publicstaticvoidmain(Stringargs[]){
- Show.inFrame(newMenus(),300,200);
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |