窗体菜单栏的创建与使用

来源:互联网 发布:linux运维之道 pdf 编辑:程序博客网 时间:2024/05/18 01:43

------------------siwuxie095

  

  

  

  

  

  

  

工程名:TestSwingMenuBar

包名:com.siwuxie095.menubar

类名:MyFrame.java

  

  

工程结构目录如下:

  

  

  

  

MyFrame.java:

  

package com.siwuxie095.menubar;

  

import java.awt.BorderLayout;

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

  

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.JPanel;

import javax.swing.JRadioButtonMenuItem;

import javax.swing.JSeparator;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

import javax.swing.border.EmptyBorder;

  

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

import javax.swing.ImageIcon;

  

public class MyFrameextends JFrame {

  

private JPanel contentPane;

  

/**

* Launch the application.

*/

public staticvoid main(String[] args) {

EventQueue.invokeLater(new Runnable() {

publicvoid run() {

try {

MyFrame frame =new MyFrame();

frame.setVisible(true);

}catch (Exception e) {

e.printStackTrace();

}

}

});

}

  

/**

* Create the frame.

*/

public MyFrame() {

 

//设定成 Windows 样式的 LookAndFeel

try {

UIManager.setLookAndFeel(new WindowsLookAndFeel());

}catch (UnsupportedLookAndFeelException e) {

e.printStackTrace();

}

 

 

 

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100,100, 450,300);

 

//创建一个 JMenuBar

JMenuBar menuBar =new JMenuBar();

//将这个 JMenuBar 设定为当前窗体的菜单栏

setJMenuBar(menuBar);

 

JMenu mnFile =new JMenu("File");

menuBar.add(mnFile);

 

JMenuItem mntmOpen =new JMenuItem("Open");

mntmOpen.setIcon(new ImageIcon(MyFrame.class.getResource("/javax/swing/plaf/metal/icons/ocean/directory.gif")));

mnFile.add(mntmOpen);

 

JMenuItem mntmClose =new JMenuItem("Close");

mntmClose.setIcon(new ImageIcon(MyFrame.class.getResource("/javax/swing/plaf/metal/icons/ocean/close.gif")));

//对菜单项:Close添加 actionPerformed事件

//点击 Close 关闭程序(注意:添加鼠标点击事件无效)

mntmClose.addActionListener(new ActionListener() {

publicvoid actionPerformed(ActionEvent e) {

System.exit(0);

}

});

mnFile.add(mntmClose);

 

JMenu mnEdit =new JMenu("Edit");

menuBar.add(mnEdit);

 

JMenuItem mntmUndo =new JMenuItem("Undo");

mnEdit.add(mntmUndo);

 

JMenuItem mntmRedo =new JMenuItem("Redo");

mnEdit.add(mntmRedo);

 

JMenu mnColor =new JMenu("Color");

mnEdit.add(mnColor);

 

JMenuItem mntmRed =new JMenuItem("Red");

mnColor.add(mntmRed);

 

JMenuItem mntmGreen =new JMenuItem("Green");

mnColor.add(mntmGreen);

 

JMenuItem mntmBlue =new JMenuItem("Blue");

mnColor.add(mntmBlue);

 

JMenu mnHelp =new JMenu("Help");

menuBar.add(mnHelp);

 

JCheckBoxMenuItem chckbxmntmC1 =new JCheckBoxMenuItem("C1");

chckbxmntmC1.setSelected(true);

mnHelp.add(chckbxmntmC1);

 

JCheckBoxMenuItem chckbxmntmC2 =new JCheckBoxMenuItem("C2");

//对菜单项:C2添加 actionPerformed事件

chckbxmntmC2.addActionListener(new ActionListener() {

publicvoid actionPerformed(ActionEvent e) {

//输出该选项是否被选中

System.out.println(chckbxmntmC2.getState());

}

});

mnHelp.add(chckbxmntmC2);

 

JCheckBoxMenuItem chckbxmntmC3 =new JCheckBoxMenuItem("C3");

chckbxmntmC3.setSelected(true);

mnHelp.add(chckbxmntmC3);

 

//分隔线,可以使用代码实现,

//也可以使用控件(组件) JSeparator 拖动实现

JSeparator separator =new JSeparator();

mnHelp.add(separator);

 

JRadioButtonMenuItem rdbtnmntmR1 =new JRadioButtonMenuItem("R1");

mnHelp.add(rdbtnmntmR1);

 

JRadioButtonMenuItem rdbtnmntmR2 =new JRadioButtonMenuItem("R2");

rdbtnmntmR2.setSelected(true);

mnHelp.add(rdbtnmntmR2);

 

JRadioButtonMenuItem rdbtnmntmR3 =new JRadioButtonMenuItem("R3");

mnHelp.add(rdbtnmntmR3);

 

ButtonGroup group=new ButtonGroup();

group.add(rdbtnmntmR1);

group.add(rdbtnmntmR2);

group.add(rdbtnmntmR3);

 

 

contentPane =new JPanel();

contentPane.setBorder(new EmptyBorder(5,5, 5,5));

setContentPane(contentPane);

contentPane.setLayout(new BorderLayout(0,0));

}

}

  

  

  

将窗体JFrame 的 LookAndFeel 设定为 Windows

  

  

向窗体 JFrame 添加一个 JMenuBar,添加时会给出

提示:Menu bar would be placed here

  

  

  

  

添加完成,注意: JMenuBar 是和 contentPane 同级,而不是

在 contentPane 内

  

  

  

  

需要为 JMenuBar 添加元素

  

  

  

  

(1)添加 JMenu 和 JMenuItem

  

JMenu 是一个菜单,而 JMenuItem 是一个菜单元素(菜单项)

  

每一个JMenu 可以包含 JMenu 和 JMenuItem,即 可以有子菜单

  

  

  

(2)添加 JCheckBoxMenuItem 和 JRadioButtonMenuItem

  

JCheckBoxMenuItem 是复选框组成的菜单项,其 selected 属性可以设定默认勾选项

  

「当对元素(菜单项)的状态进行设定时需要用到复选框」

  

  

JRadioButtonMenuItem 是单选按钮组成的菜单项,其 selected 属性可以设定默认勾选项

  

需要将JRadioButtonMenuItem 添加到 ButtonGroup,才能实现单选

  

「ButtonGroup 本身不是控件(组件),不能放到容器中,且不可视」

  

  

  

3)添加分隔线:可以通过控件(组件) JSeparator实现菜单栏的分隔

  

  

  

4)当菜单创建完毕后,还可以设定其字体、对齐方式、图标等,

还要对具体的菜单项指定动作(添加actionPerformed 事件)

  

如:为Close 菜单项添加 actionPerformed 事件,当点击 Close 时,关闭程序

  

  

  

运行程序:

  

  

  

  

  

  

  

  

  

  

  

【made by siwuxie095】

0 0