java核心技术卷 之工具栏

来源:互联网 发布:在线c语言编程器 编辑:程序博客网 时间:2024/05/24 01:08

        工具栏的特殊之处在于可以将它随处移动。可以将它拖拽到框架的四个边框上,如图所示。释放鼠标按钮后,工具栏将会停靠在新的位置上。


         注释:工具栏只有位于采用边框布局或者任何支持North、East、South和West约束布局 管理器的容器内才能够被拖拽。

        工具栏可以完全脱离框架。这样的工具栏将包含在自己的框架中。当关闭包含工具栏的框架时,它会回到原始的框架中。 编写创建工具栏的代码非常容易,并且可以将组件添加到工具栏中:

JToolBar bar - new JToolBar(); 

bar.add(blueButton);

         JToolBar类还有一个用来添加Action对象的方法,可以用 Action对象填充工具栏: 

bar.add(blueAction);

         这个动作的小阁标将会出现在工具栏中可以用分隔符将按钮分组:

bar.addSeparator();

         当工具栏没有停靠时,可以指定工具标题:
         bar = new JToolBar(titleString);

示例程序:

import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * @version 1.13 2007-06-12 * @author Cay Horstmann */public class ToolBarTest{   public static void main(String[] args)   {      EventQueue.invokeLater(new Runnable()         {            public void run()            {               ToolBarFrame frame = new ToolBarFrame();               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);               frame.setVisible(true);            }         });   }}/** * A frame with a toolbar and menu for color changes. */class ToolBarFrame extends JFrame{   public ToolBarFrame()   {      setTitle("ToolBarTest");      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);      // add a panel for color change      panel = new JPanel();      add(panel, BorderLayout.CENTER);      // set up actions      Action blueAction = new ColorAction("Blue", new ImageIcon("../blue-ball.gif"), Color.BLUE);      Action yellowAction = new ColorAction("Yellow", new ImageIcon("//yellow-ball.gif"),            Color.YELLOW);      Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED);      Action exitAction = new AbstractAction("Exit", new ImageIcon("exit.gif"))         {            public void actionPerformed(ActionEvent event)            {               System.exit(0);            }         };      exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit");      // populate tool bar      JToolBar bar = new JToolBar();      bar.add(blueAction);      bar.add(yellowAction);      bar.add(redAction);      bar.addSeparator();      bar.add(exitAction);      add(bar, BorderLayout.NORTH);      // populate menu      JMenu menu = new JMenu("Color");      menu.add(yellowAction);      menu.add(blueAction);      menu.add(redAction);      menu.add(exitAction);      JMenuBar menuBar = new JMenuBar();      menuBar.add(menu);      setJMenuBar(menuBar);   }   public static final int DEFAULT_WIDTH = 300;   public static final int DEFAULT_HEIGHT = 200;   private JPanel panel;   /**    * The color action sets the background of the frame to a given color.    */   class ColorAction extends AbstractAction   {      public ColorAction(String name, Icon icon, Color c)      {         putValue(Action.NAME, name);         putValue(Action.SMALL_ICON, icon);         putValue(Action.SHORT_DESCRIPTION, name + " background");         putValue("Color", c);      }      public void actionPerformed(ActionEvent event)      {         Color c = (Color) getValue("Color");         panel.setBackground(c);      }   }}

运行结果:


原创粉丝点击