鍍金池/ 教程/ Java/ Swing JMenuBar類
Swing JRadioButton
Swing Layout布局
Swing JTextField
Swing JLabel
Swing開發(fā)環(huán)境安裝
Swing JComponent類
SWING ActionEvent事件類
Swing JScrollBar
Swing教程首頁
Swing JFileChooser
SWING ContainerEvent事件處理類
Swing AdjustmentListener接口
Swing WindowAdapter類
SWING ComponentListener接口
Swing JMenuBar類
Swing JPanel類及實例
Swing
SWING AdjustmentEvent事件處理
Swing JWindow類及實例
Swing介紹
Swing MouseMotionAdapter類
SWING WindowEvent事件類
Swing ItemListener接口
SWING MouseEvent事件類
Swing JMenu類
SWING KeyEvent事件類
Swing MouseMotionListener接口
Swing JProgressBar
SWING AWTEvent事件類
Swing JRadioButtonMenuItem類及例子
Swing KeyAdapter類
SWING ActionListener接口
Swing LayoutManager接口
SWING MouseMotionEvent事件處理
Swing Component類
Swing Container類
Swing CardLayout布局
Swing MouseAdapter類
Swing JSlider
Swing KeyListener接口
Swing FocusAdapter類
Swing JColorChooser
Swing ImageIcon
Swing JComboBox
Swing GroupLayout布局類
Swing JOptionPane
Swing GridBagLayout布局類
Swing JMenuItem類
Swing MouseListener Interface
Swing事件適配器
SWING ComponentEvent處理類
SWING PaintEvent事件類
Swing FlowLayout布局類
SWING Event事件類
Swing FocusListener接口
Swing控件
Swing JButton
Swing JCheckBox
Swing SpringLayout布局類
Swing容器
Swing事件處理
Swing事件監(jiān)聽器
Swing Menu菜單類
Swing JList
Swing ContainerListener接口
Swing LayoutManager2接口
Swing JFrame類和實例
Swing JTextArea
SWING InputEvent事件類
Swing BorderLayout布局
Swing WindowListener接口
Swing JCheckboxMenuItem類及例子
Swing JSpinner
Swing GridLayout布局類
Swing JPopupMenu類及實例

Swing JMenuBar類

JMenuBar類提供了一個菜單欄的實現(xiàn)。

類聲明

以下是聲明 javax.swing.JMenuBar類:

public class JMenuBar
   extends JComponent
      implements Accessible, MenuElement

類構造函數(shù)

S.N. 構造函數(shù) & 描述
1 JMenuBar() 
Creates a new menu bar.

類方法

JMenu add(JMenu c) 
追加指定的菜單,在菜單欄的末尾。
S.N. 方法 & 描述
1 void addNotify() 
Overrides JComponent.addNotify to register this menu bar with the current keyboard manager.
1 AccessibleContext getAccessibleContext() 
Gets the AccessibleContext associated with this JMenuBar.
2 Component getComponent() 
Implemented to be a MenuElement.
3 Component getComponentAtIndex(int i) 
Deprecated. replaced by getComponent(int i)
4 int getComponentIndex(Component c) 
Returns the index of the specified component.
5 JMenu getHelpMenu() 
Gets the help menu for the menu bar.
6 Insets getMargin() 
Returns the margin between the menubar's border and its menus.
7 JMenu getMenu(int index) 
Returns the menu at the specified position in the menu bar.
8 int getMenuCount() 
Returns the number of items in the menu bar.
9 SingleSelectionModel getSelectionModel() 
Returns the model object that handles single selections.
10 MenuElement[] getSubElements() 
Implemented to be a MenuElement -- returns the menus in this menu bar.
11 MenuBarUI getUI() 
Returns the menubar's current UI.
12 String getUIClassID() 
Returns the name of the L&F class that renders this component.
13 boolean isBorderPainted() 
Returns true if the menu bars border should be painted.
14 boolean isSelected() 
Returns true if the menu bar currently has a component selected.
15 void menuSelectionChanged(boolean isIncluded) 
Implemented to be a MenuElement -- does nothing.
16 protected void paintBorder(Graphics g) 
Paints the menubar's border if BorderPainted property is true.
17 protected String paramString() 
Returns a string representation of this JMenuBar.
18 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) 
Subclassed to check all the child menus.
19 void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) 
Implemented to be a MenuElement -- does nothing.
20 void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager) 
Implemented to be a MenuElement -- does nothing.
21 void removeNotify() 
Overrides JComponent.removeNotify to unregister this menu bar with the current keyboard manager.
22 void setBorderPainted(boolean b) 
Sets whether the border should be painted.
23 void setHelpMenu(JMenu menu) 
Sets the help menu that appears when the user selects the "help" option in the menu bar.
24 void setMargin(Insets m) 
Sets the margin between the menubar's border and its menus.
25 void setSelected(Component sel) 
Sets the currently selected component, producing a a change to the selection model.
26 void setSelectionModel(SingleSelectionModel model) 
Sets the model object to handle single selections.
27 void setUI(MenuBarUI ui) 
Sets the L&F object that renders this component.
28 void updateUI() 
Resets the UI property with a value from the current look and feel.

方法繼承

這個類從以下類繼承的方法:

  • javax.swing.JComponent

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

JMenuBar 例子

選擇使用任何編輯器創(chuàng)建以下java程序在 D:/ > SWING > com > yiibai > gui >

SwingMenuDemo.java
package com.yiibai.gui;

import java.awt.*;
import java.awt.event.*;

public class SwingMenuDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel; 

   public SwingMenuDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      SwingMenuDemo  swingMenuDemo = new SwingMenuDemo();     
      swingMenuDemo.showMenuDemo();
   }
   
   private void prepareGUI(){
      mainFrame = new JFrame("Java SWING Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      headerLabel = new JLabel("",JLabel.CENTER );
      statusLabel = new JLabel("",JLabel.CENTER);        

      statusLabel.setSize(350,100);
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(Wind