資源描述:
《javaswing做菜單欄》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、用eclipse開發(fā)的一個簡單菜單窗口,源代碼如下:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassMenuTest{publicstaticvoidmain(String[]args){MenuJFramemenu=newMenuJFrame();menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);menu.setVisible(true);}}classMenuJFrameextendsJFram
2、e{privatefinalintWidth=480;privatefinalintHeight=640;privateActionsaveAction;privateActionsaveAsAction;publicMenuJFrame(){setSize(Width,Height);JMenuFile=newJMenu("file");JMenuItemopen=File.add(newTestAction("open"));open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,I
3、nputEvent.CTRL_MASK));File.addSeparator();saveAction=newTestAction("save");JMenuItemsave=File.add(saveAction);File.addSeparator();saveAsAction=newTestAction("saveAs");JMenuItemsaveAs=File.add(saveAsAction);File.addSeparator();File.add(newAbstractAction("exit"){publicvoidacti
4、onPerformed(ActionEvente){System.exit(0);}});JMenuEdit=newJMenu("Edit");Actioncut=newTestAction("cut");cut.putValue(Action.SMALL_ICON,newImageIcon("cut.gif"));Actioncopy=newTestAction("copy");cut.putValue(Action.SMALL_ICON,newImageIcon("copy.gif"));Actionpaste=newTestAction(
5、"paste");cut.putValue(Action.SMALL_ICON,newImageIcon("copy.gif"));Edit.add(cut);Edit.add(copy);Edit.add(paste);JMenuoption=newJMenu("option");finalJCheckBoxMenuItemReadOnly=newJCheckBoxMenuItem("Read-Only");ReadOnly.addActionListener(newActionListener(){publicvoidactionPerfo
6、rmed(ActionEvente){booleancontrol=!(ReadOnly.isSelected());saveAction.setEnabled(control);saveAsAction.setEnabled(control);}});ButtonGroupgroup=newButtonGroup();JRadioButtonMenuItemInsert=newJRadioButtonMenuItem("Insert");Insert.setSelected(true);JRadioButtonMenuItemOverType
7、=newJRadioButtonMenuItem("OverType");group.add(Insert);group.add(OverType);JMenuOption=newJMenu("Option");Option.add(ReadOnly);Option.addSeparator();Option.add(Insert);Option.add(OverType);Edit.addSeparator();Edit.add(Option);JMenuHelp=newJMenu("Help");Help.setMnemonic('H');
8、JMenuItemIndex=newJMenuItem("Index");Index.setMnemonic('I');Help.add(Index)