【上机作业】【java】2010.5.11 菜单栏的应用

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class MenuExam extends JFrame implements ActionListener

{ JMenuBar bar1;

JMenu m_file,m_edit,m_format, m_view,m_help;

JMenuItem m_new,m_open,m_save,m_save_as,m_option,m_print,m_exit,m_cut,m_copy,m_paste,m_auto,m_font,m_state,m_help_theme,m_about;

public MenuExam()

{super(“学生管理系统”);

//生成菜单条

bar1=new JMenuBar();

//生成菜单

m_file=new JMenu(“文件”);

m_edit=new JMenu(“编辑”);

m_format=new JMenu(“格式”);

m_view=new JMenu(“查看”);

m_help=new JMenu(“帮助”);

//生成菜单项

m_new=new JMenuItem(“新建”);

m_new.setAccelerator(KeyStroke.getKeyStroke(‘N’, java.awt.Event.CTRL_MASK, false));

m_open=new JMenuItem(“打开”);

m_open.setAccelerator(KeyStroke.getKeyStroke(‘O’, java.awt.Event.CTRL_MASK, false));

//打开菜单项加入事件处理程序

m_open.addActionListener(this);

m_save=new JMenuItem(“保存”);

m_save.setAccelerator(KeyStroke.getKeyStroke(‘S’, java.awt.Event.CTRL_MASK, false));

m_save_as=new JMenuItem(“另存为”);

m_option=new JMenuItem(“页面设置”);

m_print=new JMenuItem(“打印”);

m_print.setAccelerator(KeyStroke.getKeyStroke(‘P’, java.awt.Event.CTRL_MASK, false));

m_file.addSeparator();

m_exit=new JMenuItem(“退出”);

m_cut=new JMenuItem(“剪切”);

m_copy=new JMenuItem(“复制”);

m_paste=new JMenuItem(“粘贴”);

m_auto=new JMenuItem(“自动换行”);

m_auto.setMnemonic(KeyEvent.VK_W);

m_font=new JMenuItem(“字体”);

m_state=new JMenuItem(“状态栏”);

m_help_theme=new JMenuItem(“帮助主题”);

m_about=new JMenuItem(“关于记事本”);

//将菜单项加入到相应的菜单中

m_file.add(m_new);

m_file.add(m_open);

m_file.add(m_save);

m_file.add(m_save_as);

m_file.addSeparator();

m_file.add(m_option);

m_file.add(m_print);

m_file.addSeparator();

m_file.add(m_exit);

m_edit.add(m_cut);

m_edit.add(m_copy);

m_edit.add(m_paste);

m_format.add(m_auto);

m_format.add(m_font);

m_view.add(m_state);

m_help.add(m_help_theme);

m_help.addSeparator();

m_help.add(m_about);

//菜单加入到菜单条

bar1.add(m_file);

bar1.add(m_edit);

bar1.add(m_format);

bar1.add(m_view);

bar1.add(m_help);

//整个菜单加入窗体上

this.setJMenuBar(bar1);

//设置窗体大小

this.setSize(800,600);

//设置窗体可见

this.setVisible(true);

}

public void actionPerformed(ActionEvent e)

{//判断事件源

if(e.getSource().equals(m_open))

{//执行打开操作

JFileChooser jf=new JFileChooser();

jf.showOpenDialog(null);

}

}

public static void main(String args[])

{

new MenuExam();

}

}

类似文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注