java 添加JMenu例子

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MyJMenu { JFrame myFrame; JMenuBar menubar; JMenu file,edit,view,viewSub,about; JMenuItem newFile,openFile,saveFile,exit, copy,pause, full,part, viewSub_full, viewSub_part, author,help; public MyJMenu() { myFrame=new JFrame("Chevi.C J Menu"); myFrame.setSize(200,300); myFrame.setLocation(400,400); myFrame.setVisible(true); //利用事件监听关闭. myFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); menubar=getMenu(); //注意,是setJMenuBar(),二不是awt的setMenuBar()...!!!! myFrame.setJMenuBar(menubar); } //菜单设置 public JMenuBar getMenu() { menubar=new JMenuBar(); file=new JMenu("file"); edit=new JMenu("edit"); view=new JMenu("view"); viewSub=new JMenu("viewSon"); about=new JMenu("about"); newFile=new JMenuItem("new a file.."); openFile=new JMenuItem("open a file.."); saveFile=new JMenuItem("save.."); exit=new JMenuItem("exit"); file.add(newFile); file.add(openFile); file.add(saveFile); //添加分割线,和awt中的不同,awt中的菜单项可以用"-"表示分割线. file.addSeparator(); file.add(exit); menubar.add(file); copy=new JMenuItem("copy"); pause=new JMenuItem("pause"); edit.add(copy); edit.add(pause); menubar.add(edit); full=new JMenuItem("full screen"); part=new JMenuItem("window style"); view.add(full); view.add(part); viewSub_full=new JMenuItem("second full"); viewSub_part=new JMenuItem("second part"); viewSub.add(viewSub_full); viewSub.add(viewSub_part); //Menu add Menu...... view.add(viewSub); menubar.add(view); author=new JMenuItem("about Author"); help=new JMenuItem("help"); about.add(author); about.add(help); menubar.add(about); return menubar; } public static void main(String[] args) //~!@ { new MyJMenu(); } };

你可能感兴趣的:(java,String,File,Class,import,menu)