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_第1张图片
JMenu.gif

补充资源位置:

JMenu_第2张图片
Paste_Image.png

你可能感兴趣的:(JMenu)