JMenu

package JMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class JMenuDemo {

    public static void main(String[] args) {
        //1.创建窗体
        MyFrame frame = new MyFrame("JMunuDemo");
        //->设置frame大小
        int frameW = 200;
        int frameH = 200;
        frame.setSize(frameW,frameH);
        //->获取当前屏幕size
        int screenW = (int )frame.getToolkit().getScreenSize().getWidth();
        int screenH = (int )frame.getToolkit().getScreenSize().getHeight();
        //->设置frame位置
        frame.setLocation((screenW - frameW)/2,(screenH - frameH)/2);
        
        //2.创建JMenuBar JMenu
        JMenuBar menuBar = new JMenuBar ();
        JMenu menu = new JMenu("文件");
        menu.setIcon(new ImageIcon("file/file.png"));
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);
        
        //3.创建JMenuItem
        JMenuItem openItem = new JMenuItem("打开",new ImageIcon("file/open.png"));
        JMenuItem saveItem = new JMenuItem("保存",new ImageIcon("file/save.png"));
        JMenuItem exitItem = new JMenuItem("退出",new ImageIcon("file/exit.png"));
        exitItem.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
            int option = JOptionPane.showConfirmDialog(null,
                       "您确定退出系统?", "确定退出?", JOptionPane.YES_NO_OPTION,
                       JOptionPane.WARNING_MESSAGE, null);
            
                switch (option) {
                    case JOptionPane.YES_NO_OPTION: 
                        System.exit(1);
                        break;
                 
                }
            }
            
        });
        menu.add(openItem);
        menu.addSeparator();//添加分割线
        menu.add(saveItem);
        menu.addSeparator();//添加分割线
        menu.add(exitItem);
        
        
        frame.setVisible(true);
    }

}
JMenu.gif

补充资源位置:

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容