📄 mymenu.java
字号:
package Demo;
import java.awt.*;
import java.awt.event.*;
class Demo
{
Frame f=new Frame("demo");
public static void main(String args[])
{
Demo Demo=new Demo();
Demo.init();
}
public void init()
{
String s[][]={{"程序","计算器","信息录入","退出"},{"帮助","帮助主题","关于"}};
MyMenu myMenu=new MyMenu(f,s);
myMenu.showMenu();
f.setSize(500,400);
f.setBackground(Color.lightGray);
f.setLocation(100,80);
f.addWindowListener(new WindowEventProcess());
f.show();
}
}
/**
*生成下拉式菜单
*/
public class MyMenu
{
Frame f;
String s[][];
Menu menu[];
MenuItem item[][];
public MyMenu(Frame f,String s[][])
{
this.f=f;
this.s=s;
menu=new Menu[s.length];
item=new MenuItem[s.length][];
}
public void showMenu()
{
MenuBar mb=new MenuBar();
f.setMenuBar(mb);
for(int i=0;i<s.length;i++)
{
menu[i]=new Menu(s[i][0]);
mb.add(menu[i]);
for(int j=0;j<s[i].length-1;j++)
{
item[i]=new MenuItem[s[i].length-1];
item[i][j]=new MenuItem(s[i][j+1]);
menu[i].add(item[i][j]);
item[i][j].addActionListener(new ProcessEvent(f));
}
}
menu[0].insertSeparator(menu[0].getItemCount()-1);
}
}
class WindowEventProcess extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
/**
*菜单事件处理
*/
class ProcessEvent extends WindowAdapter implements ActionListener
{
Frame f;
public ProcessEvent(Frame f)
{
this.f=f;
}
public ProcessEvent()
{
}
/**
*关闭对话框
**/
public void windowClosing(WindowEvent e)
{
((Dialog)e.getSource()).dispose();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("退出"))
{
System.exit(0);
}
/*自己实现菜单事件处理
if(e.getActionCommand().equals("计算器"))
{
new Calc(f).showCalc();
}
if(e.getActionCommand().equals("信息录入"))
{
new InputStu(f).showInputStu();
}
if(e.getActionCommand().equals("关于"))
{
new About(f).showAbout();
}
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -