package cn.itcast;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
/*
* 多级菜单
*/
public class FrameDemo6_1 {
public static void main(String[] args) {
// 创建窗体对象并设置属性
Frame f = new Frame("多级菜单");
f.setBounds(400, 200, 400, 300);
f.setLayout(new FlowLayout());
// 创建菜单栏
MenuBar mb = new MenuBar();
// 创建菜单
Menu m1 = new Menu("File");
Menu m2 = new Menu("upperTitle");
// 创建菜单项
MenuItem mi1 = new MenuItem("好好学习");
MenuItem mi2 = new MenuItem("天天向上");//好好学习称为标签
MenuItem mi3 = new MenuItem("恢复标题");
MenuItem mi4 = new MenuItem("打开记事本");
MenuItem mi5 = new MenuItem("退出系统");
* 多级菜单
*/
public class FrameDemo6_1 {
public static void main(String[] args) {
// 创建窗体对象并设置属性
Frame f = new Frame("多级菜单");
f.setBounds(400, 200, 400, 300);
f.setLayout(new FlowLayout());
// 创建菜单栏
MenuBar mb = new MenuBar();
// 创建菜单
Menu m1 = new Menu("File");
Menu m2 = new Menu("upperTitle");
// 创建菜单项
MenuItem mi1 = new MenuItem("好好学习");
MenuItem mi2 = new MenuItem("天天向上");//好好学习称为标签
MenuItem mi3 = new MenuItem("恢复标题");
MenuItem mi4 = new MenuItem("打开记事本");
MenuItem mi5 = new MenuItem("退出系统");
String name = f.getTitle();
// 添加菜单组件
m2.add(mi1);
m2.add(mi2);
m2.add(mi3);
mb.add(m2);
// 添加菜单组件
m2.add(mi1);
m2.add(mi2);
m2.add(mi3);
mb.add(m2);
m1.add(m2);
m1.add(mi4);
m1.add(mi5);
mb.add(m1);
// 设置菜单栏
f.setMenuBar(mb);
m1.add(mi4);
m1.add(mi5);
mb.add(m1);
// 设置菜单栏
f.setMenuBar(mb);
// 设置窗体关闭
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(0);
}
});
mi1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
f.setTitle(mi1.getLabel());
}
});
mi3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
f.setTitle(name);
}
});
mi4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Runtime r = Runtime.getRuntime();
try {
r.exec("calc");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
mi5.addActionListener(new ActionListener() {
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(0);
}
});
mi1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
f.setTitle(mi1.getLabel());
}
});
mi3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
f.setTitle(name);
}
});
mi4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Runtime r = Runtime.getRuntime();
try {
r.exec("calc");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
mi5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
// 设置窗体可见
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
// 设置窗体可见
f.setVisible(true);
}
}