Java Swing 界面菜单栏使用案例 JMenu JMenuBar
程序员文章站
2022-06-28 23:04:27
package 简单文件管理系统;import javax.swing.*;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;public class Test{ public static void main(String args[]){ //创建主窗口 JFrame frame = new JFrame("简单文件系统"); ....
package 简单文件管理系统;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Test{
public static void main(String args[]){
//创建主窗口
JFrame frame = new JFrame("简单文件系统");
//创建菜单栏
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
//创建一级菜单
JMenu menuFile = new JMenu("文件操作"), menuDirectory = new JMenu("目录操作"), menuInformation = new JMenu("系统信息");
menuBar.add(menuFile);
menuBar.add(menuDirectory);
menuBar.add(menuInformation);
//添加“文件操作”菜单下的二级菜单
JMenu itemCreate = new JMenu("新建文件");
JMenu itemInformation = new JMenu("查看属性");
JMenu itemDelete = new JMenu("删除文件");
menuFile.add(itemCreate);
menuFile.add(itemInformation);
menuFile.add(itemDelete);
//添加“系统信息”菜单下的二级菜单
JMenu itemList = new JMenu("文件列表");
JMenu itemGraph = new JMenu("位示图情况");
menuInformation.add(itemList);
menuInformation.add(itemGraph);
//添加“目录操作”菜单下的二级菜单
JMenu itemNewDirectory = new JMenu("创建目录");
JMenu itemSetDirectory = new JMenu("设置目录");
menuDirectory.add(itemNewDirectory);
menuDirectory.add(itemSetDirectory);
//设置二级菜单点击事件
itemCreate.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
//设置窗口显示属性
frame.setSize(400,300);
frame.setLocation(250, 250);
frame.setVisible(true);
frame.validate();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
实现后的效果图如下:
本文地址:https://blog.csdn.net/weixin_45792450/article/details/112000536