Swing menu and toolbar (1)

Source: Internet
Author: User

 

The first two chapters of this book describe some low-level Swing components. This chapter will go deep into Swing menu-oriented components. Menus and toolbar make our programs more friendly by providing visual Command Options. Although the Swing component can support command sequences with multiple buttons, menus are designed to provide graphical selection with the mouse instead of using the keyboard.

The use of menu components to be discussed in this chapter is as follows:

  • For cascading menus, we can create a JMenu component and add it to JMenuBar.
  • For the optional menu in JMenu, we can create a JMenuItem component and add it to JMenu.
  • To create a sub-menu, we can add a new JMenu to the JMenu and add the JMenuItem option to the new menu.
  • Then, when a JMenu is selected, the system displays its current component set in JPopupMenu.

In addition to the basic JMenuItem elements, this chapter will discuss other menu items, such as JCheckBoxMenuItem and JRadioButtonMenuItem. We can place these two menu items in JMenu. We will also discuss the JSeparator class, which can logically group menu items. We will learn how to use the JPopupMenu class to provide support for the pop-up menu after the JMenu is selected, or any component environment. Similar to the abstract button, each menu element also has a shortcut key used for keyboard selection. We will also learn about the support for keyboard shortcuts, so that you can avoid traversing in a multi-level single dish.

In addition to menu-related components, this chapter describes the selected JMenuBar model and the specific event-related classes of the menu. The selected model interface we want to know is the SingleSelectionModel interface, and its default DefaultSingleSelectionModel. We will discuss menu-specific listeners, event MenuListener/MenuEvent, MenuKeyListener/MenuKeyEvent, and MenuDragMouseListener/MenuDragMouseEvent. In addition, we will learn how to use Popup and PopupFactory to create other pop-up components and use the toolbar through the JToolBar class.

6.1 use menu

Let's first look at an example of how menu components are combined. To start learning, create a form with a menu bar, as shown in 6-1.

This simple menu example has the following features:

  • On the menu bar, there are two common menus: File and Edit. Under the File menu, we are familiar with New, Open, Close, and Exit. Under the Edit menu, it is the Cut, Copy, Paste and Find Sub-menus and a Find option. The option sub-menu will contain the search direction sub-menu -- forward and backward -- and a case-sensitive switch.
  • In different menu locations, menu delimiters divide options into logical sets.
  • Each menu option has an associated hotkey, which allows you to browse and select the keyboard. The hot key allows you to select a menu through the keyboard. For example, you can press Alt-F on Windows to open the File menu.
  • In addition to keyboard hotkeys, the keys associated with multiple options can be used as keyboard shortcuts. Unlike the hotkey, the shortcut key can directly activate a menu option, even if the menu option is not visible.
  • The option sub-menu has an icon associated with it. Although only one icon is displayed in Figure 6-1, all menu components can have one icon, except the jaggregator and JPopupMenu components.

Note: For this example, these menu options will not do anything meaningful, but only the menu options to be output will be selected. For example, if the Copy option is Selected from the Edit menu, Selected: Copy is displayed.

LIST 6-1 shows the complete code of the sample class generated in Figure 6-1.

 

/**
*
*/
Package net. ariel. ch06; import java. awt. EventQueue;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. awt. event. KeyEvent; import javax. swing. ButtonGroup;
Import javax. swing. Icon;
Import javax. swing. ImageIcon;
Import javax. swing. JCheckBoxMenuItem;
Import javax. swing. JFrame;
Import javax. swing. JMenu;
Import javax. swing. JMenuBar;
Import javax. swing. JMenuItem;
Import javax. swing. JRadioButtonMenuItem;
Import javax. swing. KeyStroke ;/**
* @ Author mylxiaoyi
*
*/
Public class MenuSample {static class MenuActionListener implements ActionListener {
Public void actionreceivmed (ActionEvent event ){
System. out. println ("Selected:" + event. getActionCommand ());
}
}
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Runnable runner = new Runnable (){
Public void run (){
MenuActionListener menuListener = new MenuActionListener ();
JFrame frame = new JFrame ("Menu Sample ");
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar (); JMenu fileMenu = new JMenu ("File ");
FileMenu. setMnemonic (KeyEvent. VK_F );
MenuBar. add (fileMenu); JMenuItem newMenuItem = new JMenuItem ("New", KeyEvent. VK_N );
NewMenuItem. addActionListener (menuListener );
FileMenu. add (newMenuItem); JMenuItem openMenuItem = new JMenuItem ("Open", KeyEvent. VK_O );
OpenMenuItem. addActionListener (menuListener );
FileMenu. add (openMenuItem); JMenuItem closeMenuItem = new JMenuItem ("Close", KeyEvent. VK_C );
CloseMenuItem. addActionListener (menuListener );
FileMenu. add (closeMenuItem); fileMenu. addSeparator (); JMenuItem saveMenuItem = new JMenuItem ("Save", KeyEvent. VK_S );
SaveMenuItem. addActionListener (menuListener );
FileMenu. add (saveMenuItem); fileMenu. addSeparator (); JMenuItem exitMenuItem = new JMenuItem ("Exit", KeyEvent. VK_X );
ExitMenuItem. addActionListener (menuListener );
FileMenu. add (exitMenuItem); JMenu editMenu = new JMenu ("Edit ");
EditMenu. setMnemonic (KeyEvent. VK_E );
MenuBar. add (editMenu); JMenuItem cutMenuItem = new JMenuItem ("Cut", KeyEvent. VK_T );
CutMenuItem. addActionListener (menuListener );
KeyStroke ctrlXKeyStroke = KeyStroke. getKeyStroke ("control X ");
CutMenuItem. setAccelerator (ctrlXKeyStroke );
EditMenu. add (cutMenuItem); JMenuItem copyMenuItem = new JMenuItem ("Copy", KeyEvent. VK_C );
CopyMenuItem. addActionListener (menuListener );
KeyStroke ctrlCKeyStroke = KeyStroke. getKeyStroke ("control C ");
CopyMenuItem. setAccelerator (ctrlCKeyStroke );
EditMenu. add (copyMenuItem); JMenuItem pasteMenuItem = new JMenuItem ("Paste", KeyEvent. VK_P );
PasteMenuItem. addActionListener (menuListener );
KeyStroke ctrlVKeyStroke = KeyStroke. getKeyStroke ("control V ");
PasteMenuItem. setAccelerator (ctrlVKeyStroke );
EditMenu. add (pasteMenuItem); editMenu. addSeparator (); JMenuItem findMenuItem = new JMenuItem ("Find", KeyEvent. VK_F );
FindMenuItem. addActionListener (menuListener );
KeyStroke f3KeyStroke = KeyStroke. getKeyStroke ("F3 ");
FindMenuItem. setAccelerator (f3KeyStroke );
EditMenu. add (findMenuItem); JMenu findOptionsMenu = new JMenu ("Options ");
Icon atIcon = new ImageIcon ("at.gif ");
FindOptionsMenu. setIcon (atIcon );
FindOptionsMenu. setMnemonic (KeyEvent. VK_O); ButtonGroup directionGroup = new ButtonGroup (); fig = new JRadioButtonMenuItem ("Forward", true );
ForwardMenuItem. addActionListener (menuListener );
ForwardMenuItem. setMnemonic (KeyEvent. VK_F );

Related Article

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.