欢迎来到天天文库
浏览记录
ID:46952192
大小:368.00 KB
页数:23页
时间:2019-12-01
《《使用菜单与工具栏》PPT课件》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、2.4使用菜单与工具栏主讲教师:董婷一、知识能力目标1.掌握菜单的使用和设计;2.掌握工具栏的使用和设计;3.能编写带有菜单和工具栏的GUI程序二、项目16菜单控制字体和颜色使用级联菜单控制字体和文字颜色菜单JMenu复选菜单项JCheckBoxMenuItem菜单栏JMenuBar菜单项JMenuItem1.程序代码//TestMenu.javaimportjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassTestMenuextendsJFrameimplementsActionListener{JMe
2、nuBarjmb=newJMenuBar();JMenufontmenu=newJMenu("字体");JMenuhelpmenu=newJMenu("帮助");JMenustylemenu=newJMenu("样式");JMenucolormenu=newJMenu("颜色");JMenuItemexitmenu=newJMenuItem("退出");JMenuItemaboutmenu=newJMenuItem("关于");JCheckBoxMenuItemboldMenuItem=newJCheckBoxMenuItem("粗体");JCheckBoxMenuItemitali
3、cMenuItem=newJCheckBoxMenuItem("斜体");JMenuItemredmenu=newJMenuItem("红色");JMenuItembluemenu=newJMenuItem("蓝色");JMenuItemgreenmenu=newJMenuItem("绿色");JTextAreatextDemo=newJTextArea("示例文字");intbold,italic;publicTestMenu(){this.setJMenuBar(jmb);jmb.add(fontmenu);jmb.add(helpmenu);fontmenu.add(style
4、menu);fontmenu.add(colormenu);fontmenu.addSeparator();//添加分隔线fontmenu.add(exitmenu);helpmenu.add(aboutmenu);stylemenu.add(boldMenuItem);stylemenu.add(italicMenuItem);colormenu.add(redmenu);colormenu.add(bluemenu);colormenu.add(greenmenu);this.getContentPane().add(textDemo);}publicstaticvoidmain
5、(String[]args){TestMenuframe=newTestMenu();frame.regListener();frame.setTitle("菜单控制字体和颜色");frame.setSize(320,280);frame.show();}publicvoidregListener(){//为菜单注册监听器italicMenuItem.addActionListener(this);boldMenuItem.addActionListener(this);redmenu.addActionListener(this);bluemenu.addActionListene
6、r(this);greenmenu.addActionListener(this);exitmenu.addActionListener(this);}publicvoidactionPerformed(ActionEvente){if(e.getActionCommand().equals("红色"))textDemo.setForeground(Color.red);elseif(e.getActionCommand().equals("蓝色"))textDemo.setForeground(Color.blue);elseif(e.getActionCommand().equa
7、ls("绿色"))textDemo.setForeground(Color.green);if(e.getActionCommand().equals("粗体"))bold=(boldMenuItem.isSelected()?Font.BOLD:Font.PLAIN);if(e.getActionCommand().equals("斜体"))italic=(italicMenuItem.isSelected()?Font.ITALIC:Font.PLAI
此文档下载收益归作者所有