import java.awt.BorderLayout;
import java.awt.CheckboxMenuItem;
import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.PopupMenu;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TextMenu { //定义主类
public static void main(String arge[]) {
MyMenuFrame mf = new MyMenuFrame();
}
}
class MyMenuFrame extends Frame { //定义窗口类
MenuBar m_MenuBar; //定义菜单
Menu menuFile, menuEdit, sub_Edit_Font; //定义菜单项
MenuItem mi_File_Open, mi_File_Close, mi_File_Exit; //file中的菜单子项
MenuItem mi_Edit_Cut, mi_Edit_Copy, mi_Edit_Paste; //edit中的菜单子项
CheckboxMenuItem mi_Font_Style, mi_Font_Size; //定义选择式的菜单子项
PopupMenu popM; //定义弹出式菜单
MenuItem pi_Left, pi_Center, pi_Right; //定义弹出式菜单的子项
TextArea ta; //定义文本域
String ta_Font_Name;
int ta_Font_Style, new_Style, ta_Font_Size, new_Size;
MyMenuFrame() { //构造函数
super("拥有菜单的窗口"); //指定窗口标题
ta = new TextArea("\n\n\n\t\t\t 目前没有执行菜单命令", 5, 30);
ta.addMouseListener(new HandleMouse()); //文本域响应鼠标事件
add(ta, BorderLayout.CENTER); //将文本域加到窗体的中央位置
m_MenuBar = new MenuBar(); //穿件菜单条
menuFile = new Menu("文件"); //创建“文件”菜单项
mi_File_Open = new MenuItem("打开"); //依次创建菜单子项
mi_File_Open.setShortcut(new MenuShortcut(KeyEvent.VK_0)); //指定快捷键
mi_File_Close = new MenuItem("关闭");
mi_File_Exit = new MenuItem("退出");
mi_File_Open.addActionListener(new HandleAct()); //是菜单项响应动作事件
mi_File_Close.addActionListener(new HandleAct());
mi_File_Exit.addActionListener(new HandleAct());
menuFile.add(mi_File_Open); //吧菜单子项加入到菜单项中
menuFile.add(mi_File_Close);
menuFile.addSeparator(); //加一条航向分割线
menuFile.add(mi_File_Exit);
m_MenuBar.add(menuFile); //吧“文件”菜单项加入菜单条
menuEdit = new Menu("编辑");
mi_Edit_Cut = new MenuItem("剪切"); //创建选择菜单子项
mi_Edit_Copy = new MenuItem("复制");
mi_Edit_Paste = new MenuItem("粘贴");
sub_Edit_Font = new Menu("字体"); //创建菜单项
mi_Font_Size = new CheckboxMenuItem("粗体"); //隐含为未选中
mi_Font_Style = new CheckboxMenuItem("大字体");
mi_Edit_Cut.addActionListener(new HandleAct()); //注册监听者
mi_Edit_Copy.addActionListener(new HandleAct());
mi_Edit_Paste.addActionListener(new HandleAct());
mi_Font_Size.addItemListener(new HandleItem1());
mi_Font_Style.addItemListener(new HandleItem2());
sub_Edit_Font.add(mi_Font_Size); //向子菜单加入菜单命令
sub_Edit_Font.add(mi_Font_Style);
menuEdit.add(mi_Edit_Cut);
menuEdit.add(mi_Edit_Copy);
menuEdit.add(mi_Edit_Paste);
menuEdit.addSeparator(); //加入分割条
menuEdit.add(sub_Edit_Font); //加入带有子猜得到菜单项
m_MenuBar.add(menuEdit); //吧“编辑”菜单项加入菜单条
this.setMenuBar(m_MenuBar); //吧菜单条加入这个frame容器
popM = new PopupMenu(); //创建太初菜单
pi_Left = new MenuItem("左对齐"); //为弹出窗体创建菜单子项
pi_Left.addActionListener(new HandleAct()); //是弹出菜单响应动作事件
popM.add(pi_Left); //为弹出初菜单加入菜单子项
pi_Center = new MenuItem("居中");
pi_Center.addActionListener(new HandleAct());
popM.add(pi_Center);
pi_Right = new MenuItem("右对齐");
pi_Right.addActionListener(new HandleAct());
popM.add(pi_Right);
ta.add(popM); //将弹出菜单加载文本域上
addWindowListener(new HandleClose()); //将frame注册给窗口事件监听者
setSize(400, 200); //置frame初始化大小
setVisible(true); //置frame可见
ta_Font_Name = (ta.getFont()).getName(); //取文本域字体
ta_Font_Style = (ta.getFont()).getStyle();
new_Size = ta_Font_Style;
ta_Font_Size = (ta.getFont()).getSize();
new_Size = ta_Font_Size;
}
class HandleAct implements ActionListener { //内部类(处理菜单命令)
@Override
public void actionPerformed(ActionEvent e) { //响应动作事件
// TODO Auto-generated method stub
if (e.getActionCommand() == "退出") { //"退出"菜单命令
dispose(); //释放窗体
System.exit(0); //退出程序
} else //执行其他命令式只显示菜单项名称
ta.setText("\n\n\n\t\t\t"+"执行菜单命令" + e.getActionCommand());
}
}
class HandleMouse extends MouseAdapter { //内部类(处理鼠标事件)
public void mouseReleased(MouseEvent e) { //鼠标按键松开事件弹出菜单
if (e.isPopupTrigger()) //检查鼠标事件是否由弹出菜单引发
popM.show((Component) e.getSource(), e.getX(), e.getY());
} //将弹出菜单显示在用户鼠标单击的位置
}
class HandleItem1 implements ItemListener { //内部类(处理粗体)
@Override
public void itemStateChanged(ItemEvent e) { //响应选择型菜单项
// TODO Auto-generated method stub
if (((CheckboxMenuItem) e.getSource()).getState()) { //查看是否选中
//选择处理
new_Size = Font.BOLD;
ta.setFont(new Font(ta_Font_Name, ta_Font_Style, ta_Font_Size));
ta.setText("\n\n\n\t\t\t" + "你选择了粗体");
} else { //未选择处理
new_Style = ta_Font_Style;
ta.setFont(new Font(ta_Font_Name, ta_Font_Style, ta_Font_Size));
ta.setText("\n\n\n\t\t\t" + "你没有选择粗体");
}
}
}
class HandleItem2 implements ItemListener { //内部类(处理“大字体”)
@Override
public void itemStateChanged(ItemEvent e) { //响应选择型菜单项
// TODO Auto-generated method stub
if (((CheckboxMenuItem) e.getSource()).getState()) { //查看是否选中
//选中处理
new_Size = ta_Font_Size * 2;
ta.setFont(new Font(ta_Font_Name, new_Size, new_Style));
ta.setText("\n\n\n\t\t\t" + "你选择了大体");
} else { //未选中处理
new_Size = ta_Font_Size;
ta.setFont(new Font(ta_Font_Name, new_Style, new_Size));
ta.setText("\n\n\n\t\t\t" + "你没选择大体");
}
}
}
class HandleClose extends WindowAdapter { //处理窗口事件
public void windowClosing(WindowEvent e) { //响应窗口关闭框
dispose(); //释放窗体所占资源
System.exit(0); //退出程序
}
}
}