是的,我就是在一本书的例子里看到的!
它的光盘所属的代码里也是这么写的,可是我编译的时候,它报:
 cannot resolve symbol
symbol  : class ToolBarAction
错误

解决方案 »

  1.   

    import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;public class ToolDemo extends JFrame{ protected JPopupMenu popupMenu; protected JToolBar toolbar; JFrame frame;
    Action openAction; Action saveAction; Action closeAction; Action exitAction; protected void createActionObjects(){ frame = this;
    ImageIcon icon; icon = new ImageIcon("open.gif"); openAction = new AbstractAction("Open",icon){ public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(null,"Command: Open"); } }; icon = new ImageIcon("save.gif"); saveAction = new AbstractAction("Save",icon){ public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(frame,"Command: "+"Save"); } }; icon = new ImageIcon("close.gif"); closeAction = new AbstractAction("Close",icon){ public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(frame,"Command: "+"Close"); } }; icon = new ImageIcon("exit.gif"); exitAction = new AbstractAction("Exit",icon){ public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(frame,"Command: "+"Exit"); System.exit(0); } }; } class PopupHandler extends MouseAdapter{ public void mousePressed(MouseEvent e){ if (e.isPopupTrigger()) popupMenu.show(e.getComponent(),e.getX(),e.getY()); } public void mouseReleased(MouseEvent e){ if (e.isPopupTrigger()) popupMenu.show(e.getComponent(),e.getX(),e.getY()); } } protected void createPopupMenu(){ popupMenu = new JPopupMenu(); popupMenu.add(openAction); popupMenu.add(saveAction); popupMenu.add(closeAction); popupMenu.addSeparator(); popupMenu.add(exitAction); } protected void createToolbar(){ toolbar = new JToolBar(); toolbar.add(openAction); toolbar.add(saveAction); toolbar.add(closeAction); toolbar.addSeparator(); toolbar.add(exitAction); } public ToolDemo(){ try{ UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) {} addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); createActionObjects(); createPopupMenu(); createToolbar(); addMouseListener(new PopupHandler()); Container content = getContentPane(); content.setLayout(new BorderLayout()); content.add(toolbar,BorderLayout.NORTH); toolbar.setFloatable(true); content.add(new JLabel("Click inside the window")); } public static void main(String args[]){ ToolDemo app = new ToolDemo(); app.setTitle("ToolDemo"); app.setSize(320,240); //app.pack();
    app.show(); }}