In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Java GUI programming menu components how to use the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that we read this Java GUI programming menu components how to use the article will have a harvest, let's take a look at it.
Common menu-related components are given in the following table:
Menu component name function MenuBar menu bar, menu container. Menu menu component, a container for menu items. It is also a subclass of Menultem, so you can use the PopupMenu context menu component (right-click menu component) Menultem menu item component as a menu item. CheckboxMenuItem check box menu item component
The following is a diagram of the integration system of common menu-related components:
Menu-related components use:
1. Prepare menu item components, which can be MenuItem and its subclass objects
two。 Prepare the menu component Menu or PopupMenu (right-click the pop-up submenu) and add the menu item component prepared in the first step
3. Prepare the menu bar component MenuBar and add the menu component Menu prepared in step 2
4. Add the menu bar component prepared in step 3 to the window object for display.
Tips:
1. If you want to add a split line between the menu items of a menu, you only need to call Menu's add (new MenuItem (-)).
two。 If you want to associate a shortcut function with a menu item, you only need to set it when you create a menu item object. For example, if you associate a ctrl+shif+/ shortcut to a menu item, you only need: new MenuItem ("menu item name", new MenuShortcut (KeyEvent.VK_Q,true)).
Case 1:
Use the menu components commonly used in awt to complete the following effect
Demo code 1:
Import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class SimpleMenu {/ / create window private Frame frame = new Frame ("Test menu related components here"); / / create menu bar component private MenuBar menuBar = new MenuBar (); / / create file menu component private Menu fileMenu = new Menu ("File") / / create edit menu component private Menu editMenu = new Menu ("Edit"); / / create new menu item private MenuItem newItem = new MenuItem ("New"); / / create save menu item private MenuItem saveItem = new MenuItem ("Save"); / / create exit menu item private MenuItem exitItem = new MenuItem ("exit") / / create auto-wrap menu item private CheckboxMenuItem autoWrap = new CheckboxMenuItem ("auto-wrap"); / / create copy menu item private MenuItem copyItem = new MenuItem ("copy"); / / create paste menu item private MenuItem pasteItem = new MenuItem ("paste"); / / create format menu private Menu formatMenu = new Menu ("format") / / create comment menu item private MenuItem commentItem = new MenuItem ("comment"); / / create uncomment menu item private MenuItem cancelItem = new MenuItem ("uncomment"); / / create a text field private TextArea ta = new TextArea (6,40) Public void init () {/ / define menu event listeners ActionListener listener = new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {String command = e.getActionCommand (); ta.append ("Click" + command+ "menu\ n") If (command.equals ("exit")) {System.exit (0);}; / / register listeners commentItem.addActionListener (listener) for comment menu items and exit menu items; exitItem.addActionListener (listener) / / add menu items fileMenu.add (newItem) to file menu fileMenu; fileMenu.add (saveItem); fileMenu.add (exitItem); / / add menu items editMenu.add (autoWrap) to edit menu editMenu; editMenu.add (copyItem); editMenu.add (pasteItem); / / add menu items formatMenu.add (commentItem) to format menu formatMenu FormatMenu.add (cancelItem); / / add the format menu to the edit menu as secondary menus editMenu.add (new MenuItem ("-")); editMenu.add (formatMenu); / / add the file menu and edit menu to the menu bar menuBar.add (fileMenu); menuBar.add (editMenu) / / set the menu bar to the frame window frame.setMenuBar (menuBar); / / add the text field to the frame frame.add (ta); / / set the best size of the frame and see frame.pack (); frame.setVisible (true);} public static void main (String [] args) {new SimpleMenu (). Init ();}}
Case 2:
Use PopupMenu to achieve the following effect:
The idea of realization is:
1. Create a PopubMenu menu component
two。 Create multiple MenuItem menu items and add them to PopupMenu
3. Add PopupMenu to the target component
4. To right-click the component where the PopubMenu menu appears, register the mouse listening event and pop up the menu when the user releases the right button.
Demo code 2:
Import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;public class PopupMenuTest {private Frame frame = new Frame ("Test PopupMenu here"); / / create PopubMenu menu private PopupMenu popupMenu = new PopupMenu (); / / create menu bar private MenuItem commentItem = new MenuItem ("comment"); private MenuItem cancelItem = new MenuItem ("uncomment"); private MenuItem copyItem = new MenuItem ("copy") Private MenuItem pasteItem = new MenuItem ("Save"); / / create a text field private TextArea ta = new TextArea ("I love China!!" , 6,40); / / create a Panel private Panel panel = new Panel (); public void init () {/ / add menu items to PopupMenu popupMenu.add (commentItem); popupMenu.add (cancelItem); popupMenu.add (copyItem); popupMenu.add (pasteItem); / / set panel size panel.setPreferredSize (new Dimension (300,100)) / / add PopupMenu to panel panel.add (popupMenu); / / register the mouse event panel.addMouseListener (new MouseAdapter () {@ Override public void mouseReleased (MouseEvent e) {boolean flag = e.isPopupTrigger () for panel) / / determine whether the current mouse action triggers the PopupMenu operation if (flag) {/ / Let the PopupMenu display on the panel, and follow the mouse event to display the popupMenu.show (panel, e.getX (), e.getY ();}) / / add ta to the middle area of frame frame.add (ta); / / add panel to frame.add (panel, BorderLayout.SOUTH) at the bottom of frame; / / set the optimal size of frame and make it visible Frame.pack (); frame.setVisible (true);} public static void main (String [] args) {new PopupMenuTest (). Init ();}} this is the end of the article on "how to use Java GUI programming menu components". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use Java GUI programming menu components". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.