Swing Menu + Right-click menu + Two-level menu implementation

Source: Internet
Author: User

Gossip less direct code, here is mainly used to JMenuBar Jmenu,jmenuitem, JPopupMenu (right-click menu), see the document (javax.swing) can easily learn, the interface is a bit messy, but the basic use of these three classes


Import javax.swing.*; import java.awt.event.*; class Mymenu{jframe frame = new JFrame ("Right-click menu"); JMenuItem item1 = new JMenuItem ("submenu 1", New ImageIcon ("Close.gif")); JMenuItem item2 = new JMenuItem ("Sub-menu 2"); JMenuItem item3 = new JMenuItem ("Sub-menu 3"); JPopupMenu menu = new JPopupMenu (); JMenu m = new JMenu (); JPanel panel = new JPanel ();p ublic MyMenu () {         menu.add (new JMenuItem ("select")); Menu.add (New JMenuItem ("Exit")); M.add (it EM1); M.add (ITEM2); Menu.add (m); Menu.add (ITEM3);p Anel.addmouselistener (New Mouseadapter ()  {public void mousereleased (MouseEvent e) {if ( E.ispopuptrigger ()) {menu.show (panel, E.getx (), e.gety ());}}) ;p anel.add (menu); Frame.add (panel); frame.setsize (300,300); frame.setvisible (True);}} Class Tester{public static void Main (String args[]) {  new MyMenu ();}}


Swing Menu + Right-click menu + Two-level menu implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.