实验十三-————自带菜单的self-introduction

  实验要求:

  在上次的简历练习中加入菜单下拉!!!

  效果预览:

 

  其他效果图类同,这里不一一展示,请自行测试

    源代码粘贴

  1 import java.awt.*; 
  2 import java.awt.event.ActionEvent; 
  3 import java.awt.event.ActionListener; 
  4  
  5 import javax.swing.*; 
  6 public class MenuDemo extends JFrame implements ActionListener{ 
  7     JLabel la1;
  8    /////////////////////
 9  /////////////////////
10  /////////////////////
11 public MenuDemo(){ 12 this.setTitle("图形用户界面"); 13 this.setBounds(600, 240, 500, 240); 14 this.setVisible(true); 15 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 //this.setLocationRelativeTo(null); 17 //创建菜单栏 18 JMenuBar jmb = new JMenuBar(); 19 //不能设定位置,会自动放在JFrame的最上部 20 this.setJMenuBar(jmb); 21 //添加菜单 22 JMenu menu1 = new JMenu("Simple-Introduction"); 23 menu1.setVisible(isDisplayable()); 24 JMenu menu2 = new JMenu("Display"); 25 JMenu menu3 = new JMenu("Other"); 26 //创建一系列的菜单项,可添加监听 27 JMenuItem item1 = new JMenuItem("Name"); 28 JMenuItem item2 = new JMenuItem("Gender"); 29 JMenuItem item3 = new JMenuItem("Add"); 30 JMenuItem item4 = new JMenuItem("Identity"); 31 JMenuItem item5 = new JMenuItem("Character"); 32 JMenuItem item6 = new JMenuItem("Interest"); 33 JMenuItem item7 = new JMenuItem("About"); 34 //添加菜单项至菜单上 35 menu1.add(item1); 36 menu1.add(item2); 37 menu1.add(item3); 38 menu2.add(item4); 39 menu2.add(item5); 40 menu2.add(item6); 41 menu3.add(item7); 42 //将菜单加入至菜单条 43 jmb.add(menu1); 44 jmb.add(menu2); 45 jmb.add(menu3); 46 //给菜单项添加监听,执行相应的功能 47 item1.addActionListener(this); 48 item2.addActionListener(this); 49 item3.addActionListener(this); 50 item4.addActionListener(this); 51 item5.addActionListener(this); 52 item6.addActionListener(this); 53 item7.addActionListener(this); 54 } 55 //事件监听 56 public void actionPerformed(ActionEvent e){ 57 //引发监听的内容,根据Item的内容不同执行不同的功能! 58 String str = e.getActionCommand(); 59 if("Name".equals(str))//相当于"Name"==str 60 { 61 System.out.println("Name被点击,内容正在创建..."); 62 la1=new JLabel("Li MYuan"); 63 la1.setVisible(true); 64 this.getContentPane().add(la1); 65 } 66 else if("Gender".equals(str)){ 67 System.out.println("Gender被点击,内容正在创建..."); 68 this.remove(la1); 69 la1=new JLabel("I Am Male !"); 70 this.getContentPane().add(la1); 71 } 72 else if("Add".equals(str)){ 73 System.out.println("Add被点击,内容正在创建..."); 74 this.remove(la1); 75 la1=new JLabel("Xining City,Qinghai Province ."); 76 this.getContentPane().add(la1); 77 } 78 else if("Identity".equals(str)){ 79 System.out.println("Identity被点击,内容正在创建..."); 80 this.remove(la1); 81 la1=new JLabel("A Aophomore of Qinghai Normal University ."); 82 this.getContentPane().add(la1); 83 } 84 else if("Character".equals(str)){ 85 System.out.println("Character被点击,内容正在创建..."); 86 this.remove(la1); 87 la1=new JLabel("Brave,Careful,Virtuous"); 88 this.getContentPane().add(la1); 89 } 90 else if("Interest".equals(str)){ 91 System.out.println("Interest被点击,内容正在创建..."); 92 this.remove(la1); 93 la1=new JLabel("Listen to Songs,Watching Moives,Reading Books"); 94 this.getContentPane().add(la1); 95 } 96 else{ 97 System.out.println("About被点击,内容正在创建..."); 98 this.remove(la1); 99 la1=new JLabel("This is a self-introduction written in JAVA ."); 100 this.getContentPane().add(la1); 101 } 102 }
103  public static void main(String[] args){
104  new MenuDemo();
105  } 106}

  心得:

    事件监听可用接口ActionListener,实现actionPerformed(ActionEvent e)方法来实现

该GUI页面在执行代码时不直接显示菜单栏,要刷新以下才显示(把窗口放大一点才会显示),不知如何改进

菜单栏JMenuBar——>菜单JMenu——>菜单项JMenuItem 可添加动作监听

@勤奋的lu3
原文地址:https://www.cnblogs.com/lul3/p/11070665.html