JMenuBar组件

菜单与工具栏的使用与介绍

12-1:使用JMenuBar组件:

JMenuBar的类层次结构图:
java.lang.Object
    --java.awt.Component
      --java.awt.Container
        --javax.swing.JComponent
          --javax.swing.JMenuBar

在介绍JMenu组件前,我们先介绍JMenuBar组件,JMenuBar组件的功能是用来摆入JMenu组件.当我们建立完许多的JMenu组件后, 需要通过JMenuBar组件来将JMenu组件加入到窗口中.虽然我们由下表中看出JMenuBar组件只有一种构造方式,但是它对于构造一个菜 单来说是个不可缺少的组件.

JMenuBar构造函数:

JMenuBar():建立一个新的JMenuBar; 由于构造一个空的JMenuBar然后设置到窗口上对于窗口来说是没有意义的,因此JMenuBar需要结合至少一个以上的JMenu组件才 会在画面上显现出视觉的效果,所以JMenuBar的构造方法及范例我们留到JMenu的第一个范例中再加以说明.

12-1:使用JMenu组件:

JMenu的类层次结构图:

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构造函数:
  • JMenu():建立一个新的JMenu.
  • JMenu(Action a):建立一个支持Action的新的JMenu.
  • JMenu(String s):以指定的字符串名称建立一个新的JMenu.
  • JMenu(String,Boolean b):以指定的字符串名称建立一个新的JMenu并决定这个菜单是否可以下拉式的属性.

12-1-2:构造JMenu组件:

在看过JMenu的构造函数之后,我们先来看一个具有图标菜单的范例:

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();
		}
	}
}

转载于:https://my.oschina.net/u/347414/blog/158387

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值