swing里的菜单栏,官方提供了一系列API,这里我总结了一些简单用法。
先看效果图:
菜单栏
JMenuBar:菜单项
JMenu:菜单
JMenuItem:菜单选项
工具栏
JToolBar:工具栏
JButton:工具按钮
右键菜单
JPopupMenu,弹出式菜单,右键点击某个控件,根据上下文弹出一个菜单
JPopupMenu:弹出菜单
JMenuItem:菜单选项
创建一个包用于放图片资源文件
源码:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
public class MyFrame extends JFrame
{
//菜单项
JMenuBar menuBar = new JMenuBar();
//菜单项列表
List itemList = new ArrayList<>();
//创建工具栏
JToolBar toolBar = new JToolBar();
//右键弹出菜单
JPopupMenu popup = new JPopupMenu();
/**
* 为了方便,我将创建菜单、创建菜单选项和工具按钮写成了三个方法
* 分别为:createMenu()、createMenuItem()和toolButton()
* */
public MyFrame(String title)
{
super(title);
//总体布局分为两个部分,两个盒子。垂直分布
Box root = Box.createVerticalBox();
setContentPane(root);
//添加菜单项(文件)
if(!itemList.isEmpty()) //列表只用一次,所以每次清空
itemList.clear();
JMenuItem newFile = createMenuItem(null, "newFile", "新建");
JMenuItem openFile = createMenuItem(null, "openFile", "打开文件...");
JMenuItem saveFile = createMenuItem(null, "saveFile", "保存");
JMenuItem saveAsFile = createMenuItem(null, "saveAsFile", "另存为...");
itemList.add(newFile);
itemList.add(openFile);
itemList.add(saveFile);
itemList.add(saveAsFile);
JMenu file = createMenu("文件", itemList);
menuBar.add(file);
menuBar.setPreferredSize(new Dimension(9999, 18));
//添加菜单项(编辑)
if(!itemList.isEmpty())
itemList.clear();
JMenuItem copy = createMenuItem(null, "copy", "复制");
JMenuItem paste = createMenuItem(null, "paste", "粘贴");
JMenuItem delete = createMenuItem(null, "delete", "删除");
itemList.add(copy);
itemList.add(paste);
itemList.add(delete);
JMenu edit = createMenu("编辑", itemList);
menuBar.add(edit);
//添加菜单项(资源)
if(!itemList.isEmpty())
itemList.clear();
JMenuItem images = createMenuItem(null, "images", "图片");
itemList.add(images);
JMenu source = createMenu("图片", itemList);
menuBar.add(source);
//添加菜单项(帮助)
if(!itemList.isEmpty())
itemList.clear();
JMenuItem regards = createMenuItem(null, "regards", "关于");
JMenuItem message = createMenuItem(null, "message", "详细信息");
itemList.add(regards);
itemList.add(message);
JMenu help = createMenu("帮助", itemList);
menuBar.add(help);
Box line1 = Box.createHorizontalBox();
line1.add(menuBar);
line1.setMaximumSize(new Dimension(9999, 20));
line1.setPreferredSize(new Dimension(9999, 18));
/**
* 添加工具栏
* */
Box line2 = Box.createHorizontalBox();
JButton i_new = toolButton("icon_new.png", "New", "新建");
JButton i_copy = toolButton("icon_copy.png", "copy", "复制");
JButton i_paste = toolButton("icon_paste.png", "paste", "粘贴");
JButton i_import = toolButton("icon_import.png", "import", "导入");
JButton i_export = toolButton("icon_export.png", "export", "导出");
toolBar.add(i_new);
toolBar.add(i_copy);
toolBar.add(i_paste);
toolBar.add(i_import);
toolBar.add(i_export);
line2.add(Box.createHorizontalStrut(0));
line2.add(toolBar);
toolBar.setMaximumSize(new Dimension(9999, 30));
line2.setMaximumSize(new Dimension(9999, 30));
line2.setPreferredSize(new Dimension(9999, 30));
root.add(line1);
root.add(Box.createVerticalStrut(1));
root.add(line2);
//添加右键菜单
JMenuItem newItem = createMenuItem(null, "newFile", "新建文件");
JMenuItem openItem = createMenuItem(null, "openFile", "打开文件");
JMenuItem copyItem = createMenuItem(null, "copyFile", "拷贝");
JMenuItem pasteItem = createMenuItem(null, "paste", "粘贴");
popup.add(newItem);
popup.add(openItem);
popup.add(copyItem);
popup.add(pasteItem);
root.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
if(e.getButton() == MouseEvent.BUTTON3)
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
//创建菜单
protected JMenu createMenu(String text, List item) //一次性添加多个Item,所以用List集合
{
JMenu menu = new JMenu(text);
if(item != null)
{
for(JMenuItem i: item)
{
menu.add(i);
}
}
return menu;
}
//创建菜单选项
protected JMenuItem createMenuItem(String filePath, String action, String text)
{
JMenuItem item = new JMenuItem(text);
//ActionCommand为命令码,用于区别各个选项,后用于监听事件
item.setActionCommand(action);
//设置监听事件
item.addActionListener(listener);
if(filePath != null)
{
String fileName = "/images/" + filePath;
//获取资源文件
URL url = getClass().getResource(fileName);
//设置图标
item.setIcon(new ImageIcon(url));
}
return item;
}
//创建工具栏
protected JButton toolButton(String filePath, String action, String toolText)
{
String fileName = "/images/" + filePath;
//获取资源文件
URL url = getClass().getResource(fileName);
JButton button = new JButton();
button.setActionCommand(action);
button.setToolTipText(toolText);
button.setFocusable(false);
//设置图标
button.setIcon(new ImageIcon(url));
//设置监听事件
button.addActionListener(listener);
return button;
}
//监听事件
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
//获取该控件命令码
String action = e.getActionCommand();
//通过对话框打印
JOptionPane.showMessageDialog(MyFrame.this, action);
}
};
}