packagecom.yiibai.gui;importjava.awt.*;importjava.awt.event.*;publicclassSwingMenuDemo{privateJFramemainFrame;privateJLabelheaderLabel;privateJLabelstatusLabel;privateJPanelcontrolPanel;publicSwingMenuDemo(){prepareGUI();}publicstaticvoidmain(String[]args){SwingMenuDemoswingMenuDemo=newSwingMenuDemo();swingMenuDemo.showMenuDemo();}privatevoidprepareGUI(){mainFrame=newJFrame("Java SWING Examples");mainFrame.setSize(400,400);mainFrame.setLayout(newGridLayout(3,1));headerLabel=newJLabel("",JLabel.CENTER);statusLabel=newJLabel("",JLabel.CENTER);statusLabel.setSize(350,100);mainFrame.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventwindowEvent){System.exit(0);}});controlPanel=newJPanel();controlPanel.setLayout(newFlowLayout());mainFrame.add(headerLabel);mainFrame.add(controlPanel);mainFrame.add(statusLabel);mainFrame.setVisible(true);}privatevoidshowMenuDemo(){//create a menu barfinalJMenuBarmenuBar=newJMenuBar();//create menusJMenufileMenu=newJMenu("File");JMenueditMenu=newJMenu("Edit");finalJMenuaboutMenu=newJMenu("About");finalJMenulinkMenu=newJMenu("Links");//create menu itemsJMenuItemnewMenuItem=newJMenuItem("New");newMenuItem.setMnemonic(KeyEvent.VK_N);newMenuItem.setActionCommand("New");JMenuItemopenMenuItem=newJMenuItem("Open");openMenuItem.setActionCommand("Open");JMenuItemsaveMenuItem=newJMenuItem("Save");saveMenuItem.setActionCommand("Save");JMenuItemexitMenuItem=newJMenuItem("Exit");exitMenuItem.setActionCommand("Exit");JMenuItemcutMenuItem=newJMenuItem("Cut");cutMenuItem.setActionCommand("Cut");JMenuItemcopyMenuItem=newJMenuItem("Copy");copyMenuItem.setActionCommand("Copy");JMenuItempasteMenuItem=newJMenuItem("Paste");pasteMenuItem.setActionCommand("Paste");MenuItemListenermenuItemListener=newMenuItemListener();newMenuItem.addActionListener(menuItemListener);openMenuItem.addActionListener(menuItemListener);saveMenuItem.addActionListener(menuItemListener);exitMenuItem.addActionListener(menuItemListener);cutMenuItem.addActionListener(menuItemListener);copyMenuItem.addActionListener(menuItemListener);pasteMenuItem.addActionListener(menuItemListener);finalJCheckBoxMenuItemshowWindowMenu=newJCheckBoxMenuItem("Show About",true);showWindowMenu.addItemListener(newItemListener(){publicvoiditemStateChanged(ItemEvente){if(showWindowMenu.getState()){menuBar.add(aboutMenu);}else{menuBar.remove(aboutMenu);}}});finalJRadioButtonMenuItemshowLinksMenu=newJRadioButtonMenuItem("Show Links",true);showLinksMenu.addItemListener(newItemListener(){publicvoiditemStateChanged(ItemEvente){if(menuBar.getMenu(3)!=null){menuBar.remove(linkMenu);mainFrame.repaint();}else{menuBar.add(linkMenu);mainFrame.repaint();}}});//add menu items to menusfileMenu.add(newMenuItem);fileMenu.add(openMenuItem);fileMenu.add(saveMenuItem);fileMenu.addSeparator();fileMenu.add(showWindowMenu);fileMenu.addSeparator();fileMenu.add(showLinksMenu);fileMenu.addSeparator();fileMenu.add(exitMenuItem);editMenu.add(cutMenuItem);editMenu.add(copyMenuItem);editMenu.add(pasteMenuItem);//add menu to menubarmenuBar.add(fileMenu);menuBar.add(editMenu);menuBar.add(aboutMenu);menuBar.add(linkMenu);//add menubar to the framemainFrame.setJMenuBar(menuBar);mainFrame.setVisible(true);}classMenuItemListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){statusLabel.setText(e.getActionCommand()+" JMenuItem clicked.");}}}
Java点击jmenuitem事件_Swing JRadioButtonMenuItem类及例子
最新推荐文章于 2021-03-04 16:57:03 发布