Swing菜单和菜单项是按钮,因为JMenuItem扩展AbstractButton,JMenu扩展JMenuItem。因此,Swing菜单和菜单项继承了如下功能,即包含文本和(或)图标、显示光标进入时的图标和助记符等。
  JMenuItem和JMenu的父组件,它们最后都归于java.awt.Container,以强调菜单和菜单项都是容器。可以把任何类型的组件(从包含一个动画GIF的标签到JTree的一个实例)添加到一个菜单或菜单项中。
  菜单和菜单项还实现MenuElement接口,以便参与菜单事件的处理。
  工具条和弹出式菜单都扩展JComponent类,即任意组件都可以添加到一个工具条或弹出式菜单中。
  本章将介绍下面的组件:
  •JMenuItem
  •JCheckBoxMenuItem
  •JRadioButtonMenuItem
  •JMenu
  •JPopupMenu
  •JMenuBar

例子 1:在菜单中增加复选菜单
import java.awt.event.KeyEvent; 

import javax.swing.JCheckBoxMenuItem; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 

public class JCheckBoxMenuItemDemo { 

  public static void main(final String args[]) { 
    JFrame frame = new JFrame("MenuSample Example"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JMenuBar menuBar = new JMenuBar(); 

    // File Menu, F - Mnemonic 
    JMenu fileMenu = new JMenu("File"); 
    fileMenu.setMnemonic(KeyEvent.VK_F); 
    menuBar.add(fileMenu); 

    // File->New, N - Mnemonic 
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N); 
    fileMenu.add(newMenuItem); 

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive"); 
    caseMenuItem.setMnemonic(KeyEvent.VK_C); 
    fileMenu.add(caseMenuItem); 
    
    frame.setJMenuBar(menuBar); 
    frame.setSize(350, 250); 
    frame.setVisible(true); 
  } 
} 


例2:菜单事件处理
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 

import javax.swing.AbstractButton; 
import javax.swing.ButtonGroup; 
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.JSeparator; 
import javax.swing.JTextField; 

public class SwingWinMenus extends JFrame { 
  public static int WIDTH = 300; 

  public static int HEIGHT = 300; 

  public static String TITLE = "SwingWin"; 

  Container frameContainer; 

  JTextField textField = new JTextField(50); 

  JMenuBar menuBar = new JMenuBar(); 

  JMenu fileMenu = new JMenu("File"); 

  JMenuItem fileNew = new JMenuItem("New"); 

  JMenuItem fileOpen = new JMenuItem("Open"); 

  JMenuItem fileSave = new JMenuItem("Save"); 

  JMenuItem fileExit = new JMenuItem("Exit"); 

  JMenu editMenu = new JMenu("Edit"); 

  JMenuItem editCut = new JMenuItem("Cut"); 

  JMenuItem editCopy = new JMenuItem("Copy"); 

  JMenuItem editPaste = new JMenuItem("Paste"); 

  JMenu specialMenu = new JMenu("Special"); 

  JCheckBoxMenuItem specialCheck1 = new JCheckBoxMenuItem("Check 1"); 

  JCheckBoxMenuItem specialCheck2 = new JCheckBoxMenuItem("Check 2", true); 

  JSeparator separator = new JSeparator(); 

  JRadioButtonMenuItem specialRadio1 = new JRadioButtonMenuItem("Radio 1"); 

  JRadioButtonMenuItem specialRadio2 = new JRadioButtonMenuItem("Radio 2"); 

  ButtonGroup buttonGroup = new ButtonGroup(); 

  public SwingWinMenus() { 
    super(""); 
    fileMenu.add(fileNew); 
    fileMenu.add(fileOpen); 
    fileMenu.add(fileSave); 
    fileMenu.add(fileExit); 
    editMenu.add(editCut); 
    editMenu.add(editCopy); 
    editMenu.add(editPaste); 
    specialMenu.add(specialCheck1); 
    specialMenu.add(specialCheck2); 
    specialMenu.add(separator); 
    buttonGroup.add(specialRadio1); 
    buttonGroup.add(specialRadio2); 
    specialMenu.add(specialRadio1); 
    specialMenu.add(specialRadio2); 
    menuBar.add(fileMenu); 
    menuBar.add(editMenu); 
    menuBar.add(specialMenu); 
    setJMenuBar(menuBar); 

    frameContainer = getContentPane(); 
    frameContainer.setLayout(null); 
    textField.setBounds(100, 100, 100, 20); 
    frameContainer.add(textField); 

    setupEventHandlers(); 
    setSize(450, 450); 
    setVisible(true); 
  } 

  void setupEventHandlers() { 
    addWindowListener(new WindowHandler()); 
    fileNew.addActionListener(new MenuItemHandler()); 
    fileOpen.addActionListener(new MenuItemHandler()); 
    fileSave.addActionListener(new MenuItemHandler()); 
    fileExit.addActionListener(new MenuItemHandler()); 
    editCut.addActionListener(new MenuItemHandler()); 
    editCopy.addActionListener(new MenuItemHandler()); 
    editPaste.addActionListener(new MenuItemHandler()); 
    specialCheck1.addItemListener(new ItemHandler()); 
    specialCheck2.addItemListener(new ItemHandler()); 
    specialRadio1.addItemListener(new ItemHandler()); 
    specialRadio2.addItemListener(new ItemHandler()); 
  } 

  public static void main(String[] args) { 
    SwingWinMenus app = new SwingWinMenus(); 
  } 

  public class WindowHandler extends WindowAdapter { 
    public void windowClosing(WindowEvent e) { 
      System.exit(0); 
    } 
  } 

  public class MenuItemHandler implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
      String cmd = e.getActionCommand(); 
      if (cmd.equals("Exit")) 
        System.exit(0); 
      else 
        textField.setText(cmd); 
    } 
  } 

  public class ItemHandler implements ItemListener { 
    public void itemStateChanged(ItemEvent e) { 
      AbstractButton button = (AbstractButton) e.getItem(); 
      String label = button.getText(); 
      if (button.isSelected()) 
        label += " true"; 
      else 
        label += " false"; 
      textField.setText(label); 
    } 
  } 
} 

例3:弹出试菜单
import java.awt.Dimension; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenuItem; 
import javax.swing.JPopupMenu; 
import javax.swing.event.PopupMenuEvent; 
import javax.swing.event.PopupMenuListener; 

// Define PopupMenuListener 
class MyPopupMenuListener implements PopupMenuListener { 
  public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) { 
    System.out.println("Canceled"); 
  } 

  public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) { 
    System.out.println("Becoming Invisible"); 
  } 

  public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) { 
    System.out.println("Becoming Visible"); 
  } 
} 

public class PopupMenuListenerDemo { 
  public static void main(final String args[]) { 
    JFrame frame = new JFrame("PopupSample Example"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    // Create popup menu, attach popup menu listener 
    JPopupMenu popupMenu = new JPopupMenu("Title"); 
    popupMenu.setSize(new Dimension(100,50)); 
    PopupMenuListener popupMenuListener = new MyPopupMenuListener(); 

    popupMenu.addPopupMenuListener(popupMenuListener); 

    // Cut 
    JMenuItem cutMenuItem = new JMenuItem("Cut"); 
    popupMenu.add(cutMenuItem); 

    // Copy 
    JMenuItem copyMenuItem = new JMenuItem("Copy"); 
    popupMenu.add(copyMenuItem); 
    // Paste 
    JMenuItem pasteMenuItem = new JMenuItem("Paste"); 
    pasteMenuItem.setEnabled(false); 
    popupMenu.add(pasteMenuItem); 

    // Separator 
    popupMenu.addSeparator(); 

    // Find 
    JMenuItem findMenuItem = new JMenuItem("Find"); 
    popupMenu.add(findMenuItem); 
    JButton label = new JButton(); 
    frame.add(label); 
    label.setComponentPopupMenu(popupMenu); 

    frame.setSize(350, 250); 
    frame.setVisible(true); 
  } 
} 


例 4:给菜单增加快捷方式
import java.awt.event.KeyEvent; 

import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.KeyStroke; 

public class JMenuItemKeyStroke { 

  public static void main(final String args[]) { 
    JFrame frame = new JFrame("MenuSample Example"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JMenuBar menuBar = new JMenuBar(); 

    // File Menu, F - Mnemonic 
    JMenu fileMenu = new JMenu("File"); 
    fileMenu.setMnemonic(KeyEvent.VK_F); 
    menuBar.add(fileMenu); 

    // File->New, N - Mnemonic 
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N); 
    fileMenu.add(newMenuItem); 

    // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator 
    JMenuItem cutMenuItem = new JMenuItem("Cut", KeyEvent.VK_T); 
    KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke("control X"); 
    cutMenuItem.setAccelerator(ctrlXKeyStroke); 
    fileMenu.add(cutMenuItem); 

    frame.setJMenuBar(menuBar); 
    frame.setSize(350, 250); 
    frame.setVisible(true); 
  } 
}  

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐