java.lang.Object --java.awt.Component --java.awt.Container --javax.swing.JComponent --javax.swing.JMenuBar
在介绍JMenu组件前,咱们先介绍JMenuBar组件,JMenuBar组件的功能是用来摆入JMenu组件.当咱们创建完许多的JMenu组件后, 须要经过JMenuBar组件来将JMenu组件加入到窗口中.虽然咱们由下表中看出JMenuBar组件只有一种构造方式,可是它对于构造一个菜 单来讲是个不可缺乏的组件. java
JMenuBar():创建一个新的JMenuBar; 因为构造一个空的JMenuBar而后设置到窗口上对于窗口来讲是没有意义的,所以JMenuBar须要结合至少一个以上的JMenu组件才 会在画面上显现出视觉的效果,因此JMenuBar的构造方法及范例咱们留到JMenu的第一个范例中再加以说明. cors
java.lang.Object --java.awt.Component --java.awt.Container --javax.swing.JComponent --javax.swing.AbstractButton --javax.swing.JMenuItem --javax.swing.JMenu 函数
JMenu组件是用来存放和整合JMenuItem的组件,这个组件也是在构成一个菜单中不可或缺的组件之一.JMenu能够是单一层次的结 构也能够是一个层次式的结构,要使用何种形式的结构取决于界面设计上的须要而定,以下表所示: 工具
在看过JMenu的构造函数以后,咱们先来看一个具备图标菜单的范例: ui
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; import com.incors.plaf.alloy.*; import com.incors.plaf.alloy.themes.glass.*; public class JMenu1 extends JFrame { JTextArea theArea = null; public JMenu1() { super("JMenu1"); theArea = new JTextArea(); theArea.setEditable(false); getContentPane().add(new JScrollPane(theArea)); JMenuBar MBar = new JMenuBar(); // 调用自行编写的buildFileMenu()方法来构造JMenu. JMenu mfile = buildFileMenu(); MBar.add(mfile); // 将JMenu加入JMenuBar中. setJMenuBar(MBar);// 将JMenuBar设置到窗口中. }// end of JMenu1() public JMenu buildFileMenu() { JMenu thefile = new JMenu("File"); thefile.setIcon(new ImageIcon("icons/file.gif")); return thefile; }// end of buildFileMenu() public static void main(String[] args) { SwingUtil.setLookAndFeel(); JFrame F = new JMenu1(); F.setSize(400, 200); F.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });// end of addWindowListener F.setVisible(true); } // end of main }// end of class JMenu1 class SwingUtil { public static final void setLookAndFeel() { try { Font font = new Font("JFrame", Font.PLAIN, 12); Enumeration keys = UIManager.getLookAndFeelDefaults().keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); if (UIManager.get(key) instanceof Font) { UIManager.put(key, font); } } AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true"); AlloyTheme theme = new GlassTheme(); LookAndFeel alloyLnF = new AlloyLookAndFeel(theme); UIManager.setLookAndFeel(alloyLnF); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } } }