当前位置:编程学习 > JAVA >>

Java JMenu_1

\\


package com.han; 
 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
 
public class JMenu_1 extends JFrame { 
 
    /**
     * 
     */ 
    private static final long serialVersionUID = -5106828427995282232L; 
 
    public JMenu_1() { 
        // TODO Auto-generated constructor stub  
        JMenuBar menuBar = new JMenuBar(); 
        setJMenuBar(menuBar); 
         
        JMenu menu = new JMenu("菜单名称"); 
        menuBar.add(menu); 
        JMenuItem menuItem = new JMenuItem("菜单项名称"); 
        menu.add(menuItem); 
        JMenu menuSub = new JMenu("子菜单名称"); 
        menu.add(menuSub); 
        JMenuItem menuSub1Item = new JMenuItem("子菜单项名称"); 
        menuSub.add(menuSub1Item); 
         
        JMenu menu2 = new JMenu("菜单名称2"); 
        menuBar.add(menu2); 
        JMenuItem menuItem2 = new JMenuItem("菜单项名称2"); 
        menu2.add(menuItem2); 
        JMenu menuSub2 = new JMenu("子菜单名称2"); 
        menu2.add(menuSub2); 
        JMenuItem menuSub1Item2 = new JMenuItem("子菜单项名称2"); 
        menuSub2.add(menuSub1Item2); 
         
        menuItem.addActionListener(new ActionListener(){ 
 
            @Override 
            public void actionPerformed(ActionEvent e) { 
                // TODO Auto-generated method stub  
                JMenuItem menuItem = (JMenuItem) e.getSource(); 
                System.out.println("您单击的菜单项是:" + menuItem.getText()); 
            } 
             
        }); 
    } 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub  
        JMenu_1 frame = new JMenu_1(); 
        frame.setTitle("创建菜单栏"); 
        frame.setVisible(true); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setBounds(100, 100, 400, 200); 
    } 
 

 


摘自 Gaowen_HAN的专栏



补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,