import java.awt.Container; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.KeyStroke; //================================================= // File Name : Jmenu_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : Jmenu_demo public class Jmenu_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 JFrame frame = new JFrame("窗体"); //定义窗体 JTextArea text = new JTextArea(); //定义文本域 text.setEditable(true); //定义文本组件可编辑 frame.getContentPane().add(new JScrollPane(text)); //在面板中加入文本框及滚动条 JMenu menuFile = new JMenu("文件"); //定义JMenu组件 //定义显示图标 menuFile.setIcon(new ImageIcon("/home/common/software/database/123.jpg")); JMenuBar menuBar = new JMenuBar(); //定义JMenu组件 //定义显示图标 JMenuItem newMenu = new JMenuItem("新建",new ImageIcon("/home/common/software/database/123.jpg")); //定义显示图标 JMenuItem openMenu = new JMenuItem("打开",new ImageIcon("/home/common/software/database/123.jpg")); newMenu.setMnemonic('N'); openMenu.setMnemonic('O'); newMenu.setAccelerator(KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK)); openMenu.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK)); menuFile.add(newMenu); //加入菜单项 menuFile.addSeparator(); //加入分割线 menuFile.add(openMenu); //加入菜单项 menuBar.add(menuFile); //加入JMenu frame.addWindowListener(new WindowAdapter(){ //加入动作监听 public void windowClosing(WindowEvent e) { //窗口关闭时触发,按下关闭按钮 // TODO 自动生成的方法存根 System.out.println("windowClosing-->窗口关闭"); System.exit(1); } }); frame.setJMenuBar(menuBar); //在窗体中加入JMenuBar组件 frame.setVisible(true); frame.setLocation(300,200); //设置显示位置 frame.setSize(300, 180); } }
import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.KeyStroke; //================================================= // File Name : Jmenu_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : Jmenu_demo public class Jmenu_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 JFrame frame = new JFrame("窗体"); //定义窗体 JTextArea text = new JTextArea(); //定义文本域 text.setEditable(true); //定义文本组件可编辑 frame.getContentPane().add(new JScrollPane(text)); //在面板中加入文本框及滚动条 JMenu menuFile = new JMenu("文件"); //定义JMenu组件 //定义显示图标 menuFile.setIcon(new ImageIcon("/home/common/software/database/123.jpg")); JMenuBar menuBar = new JMenuBar(); //定义JMenu组件 //定义显示图标 JMenuItem newMenu = new JMenuItem("新建",new ImageIcon("/home/common/software/database/123.jpg")); //定义显示图标 JMenuItem openMenu = new JMenuItem("打开",new ImageIcon("/home/common/software/database/123.jpg")); newMenu.setMnemonic('N'); openMenu.setMnemonic('O'); newMenu.setAccelerator(KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK)); openMenu.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK)); menuFile.add(newMenu); //加入菜单项 menuFile.addSeparator(); //加入分割线 menuFile.add(openMenu); //加入菜单项 menuBar.add(menuFile); //加入JMenu newMenu.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 text.append("选择了newMenu"); } }); openMenu.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 text.append("选择了openMenu"); } }); frame.addWindowListener(new WindowAdapter(){ //加入动作监听 public void windowClosing(WindowEvent e) { //窗口关闭时触发,按下关闭按钮 // TODO 自动生成的方法存根 System.out.println("windowClosing-->窗口关闭"); System.exit(1); } }); frame.setJMenuBar(menuBar); //在窗体中加入JMenuBar组件 frame.setVisible(true); frame.setLocation(300,200); //设置显示位置 frame.setSize(300, 180); } }