import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;public class JPopupMenu1 extends JFrame{
    JTextArea theArea=null;
    static final String ComboStr[]={"Times New Roman","Dialog","宋体","黑体","楷体"};
    JPopupMenu Popup=null;//声明一个全局(Global)并设置初始值为null的JPopupMenu组件
    
    public JPopupMenu1(){
      super("JPopupMenu1");
      theArea=new JTextArea();
      theArea.setEditable(false);
      this.getContentPane().add(new JScrollPane(theArea),BorderLayout.CENTER);
      JMenuBar MBar=new JMenuBar();
      MBar.setOpaque(true);
      JMenu mfile=buildFileMenu();
      JToolBar theBar=buildToolBar();
      this.getContentPane().add(theBar,BorderLayout.NORTH);
      PopupPanel pp=new PopupPanel();
      this.getContentPane().add(pp,BorderLayout.SOUTH);//将PopupPanel组件放置到窗口中
      
      MBar.add(mfile);
      setJMenuBar(MBar);
    }//end of JPopupMenu1()
    
    /*****************************************************************
        分别建立程序中菜单,工具栏和事件处理模式的部分,我们在此不再加以说明
    ******************************************************************/
    public JMenu buildFileMenu(){
      JMenu thefile=new JMenu("File");
      thefile.setMnemonic('F');
      
      JMenuItem newf=new JMenuItem("New",new ImageIcon("open.jpg"));
      JMenuItem open=new JMenuItem("Open",new ImageIcon("open.jpg"));
      JMenuItem close=new JMenuItem("Close",new ImageIcon("open.jpg"));
      JMenuItem quit=new JMenuItem("Exit",new ImageIcon("open.jpg"));
      
      newf.setMnemonic('N');
      open.setMnemonic('O');
      close.setMnemonic('L');
      quit.setMnemonic('X');
      
      newf.setAccelerator(KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK,false));
      open.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK,false));
      close.setAccelerator(KeyStroke.getKeyStroke('L',java.awt.Event.CTRL_MASK,false));
      quit.setAccelerator(KeyStroke.getKeyStroke('X',java.awt.Event.CTRL_MASK,false));
      
      newf.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
          theArea.append("- MenuItem New Performed -\n");
        }
      });
      
      open.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
          theArea.append("- MenuItem Open Performed -\n");
        }
      });
      
      close.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
          theArea.append("- MenuItem Close Performed -\n");
        }
      });
      
      quit.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
          System.exit(0);
        }
      });
      
      thefile.add(newf);
      thefile.add(open);
      thefile.add(close);
      thefile.addSeparator();
      thefile.add(quit);
      
      return thefile;
    }//end of buildFileMenu()
    
    public JToolBar buildToolBar(){
      JToolBar toolBar=new JToolBar();
      toolBar.setFloatable(true);
      
      ToolBarAction tba_new=new ToolBarAction("new",new ImageIcon("3.gif"));
      ToolBarAction tba_open=new ToolBarAction("open",new ImageIcon("3.gif"));
      ToolBarAction tba_close=new ToolBarAction("close",new ImageIcon("3.gif"));
      
      JButton JB;
      JB=toolBar.add(tba_new);
      JB.setActionCommand("#ToolBar_NEW performed!");
      JB.setToolTipText((String)tba_new.getValue(Action.NAME));
      JB=toolBar.add(tba_open);
      JB.setActionCommand("#ToolBar_OPEN performed!");
      JB.setToolTipText((String)tba_open.getValue(Action.NAME));
      JB=toolBar.add(tba_close);
      JB.setActionCommand("#ToolBar_CLOSE performed!");
      JB.setToolTipText((String)tba_close.getValue(Action.NAME));
      
      toolBar.addSeparator();
      
      ToolBarAction tba_B=new ToolBarAction("bold",new ImageIcon("3.gif"));
      ToolBarAction tba_I=new ToolBarAction("italic",new ImageIcon("3.gif"));
      ToolBarAction tba_U=new ToolBarAction("underline",new ImageIcon("3.gif"));
      JB=toolBar.add(tba_B);
      JB.setActionCommand("#ToolBar_Bold performed!");
      JB.setToolTipText((String)tba_B.getValue(Action.NAME));
      JB=toolBar.add(tba_I);
      JB.setActionCommand("#ToolBar_Italic performed!");
      JB.setToolTipText((String)tba_I.getValue(Action.NAME));
      JB=toolBar.add(tba_U);
      JB.setActionCommand("#ToolBar_Underline performed!");
      JB.setToolTipText((String)tba_U.getValue(Action.NAME));
      
      toolBar.addSeparator();
      JLabel JLfont=new JLabel("Font Type");
      toolBar.add(JLfont);
      toolBar.addSeparator();
      JComboBox jcb=new JComboBox(ComboStr);
      jcb.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
          theArea.append("*ComboBox "+((JComboBox)e.getSource()).getSelectedItem()+"performed!\n");
        }
      });
      toolBar.add(jcb);
      
      return toolBar;
    }//end of buildToolBar()
    
    /************************************************************************
        分别建立程序中菜单,工具栏和事件处理模式的部分,我们在此不再加以说明。
    ************************************************************************/
    public static void main(String[] args){
      new JPopupMenu1();
      JFrame F=new JPopupMenu1();
      F.setSize(430,200);
      F.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
          System.exit(0);
        }
      });//end of addWindowListener
      F.setVisible(true);
    }//end of main
    
    class ToolBarAction extends AbstractAction{
      public ToolBarAction(String name,Icon icon){
        super(name,icon);
      }
      
      public void actionPerformed(ActionEvent e){
        try{
          theArea.append(e.getActionCommand()+"\n");
        }catch(Exception ex){
        }
      }//end of inner class ToolBarAction