本帖最后由 u011386820 于 2013-07-12 17:06:01 编辑

解决方案 »

  1.   

    Swing提供一个工具条组件,根据工具条的方向,工具条组件包含一行按钮或一列按钮。工具条通常提供对普通特性的简单访问。工具条还可以是悬浮的,这样,可以把工具条在窗口中任意拖动或拖到一个单独的窗口中。
      

  2.   

    程序中的工具条是可以任意拖动的package bbs_713;import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Test extends JApplet {
    Icon dukeStanding = new ImageIcon("duke_standing.gif"),
    dukeWaving = new ImageIcon("duke_waving.gif"),
    dukeStandingSmall = new ImageIcon("duke_standing_small.gif"),
    dukeWavingSmall = new ImageIcon("duke_waving_small.gif"); public void init() {
    Container contentPane = getContentPane();
    Action[] actions = { new NewAction(), new OpenAction(),
    new CutAction(), new CopyAction(), new PasteAction(),
    new ExitAction() };
    JToolBar toolbar = new JToolBar();
    JMenuBar menubar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JRadioButton menubarDuke = new JRadioButton(dukeStandingSmall), menuDuke = new JRadioButton(
    dukeStandingSmall), toolbarDuke = new JRadioButton(dukeStanding); menuDuke.setRolloverIcon(dukeWavingSmall);
    menubarDuke.setRolloverIcon(dukeWavingSmall);
    toolbarDuke.setRolloverIcon(dukeWaving);
    menubar.add(menubarDuke);
    toolbar.add(toolbarDuke);
    fileMenu.add(menuDuke);
    for (int i = 0; i < actions.length; ++i) {
    fileMenu.add(actions[i]);
    if (i != actions.length - 1)
    toolbar.add(actions[i]);
    if (i == 2 || i == actions.length - 2) {
    toolbar.addSeparator();
    fileMenu.addSeparator();
    }
    }
    menubar.add(fileMenu);
    contentPane.add(toolbar, BorderLayout.NORTH);
    getRootPane().setJMenuBar(menubar);
    System.out.println(contentPane.getClass().getName());
    LayoutManager lm = contentPane.getLayout();
    System.out.println(lm.getClass());
    } class NewAction extends AbstractAction {
    public NewAction() {
    super("New ...", new ImageIcon("new.gif"));
    } public void actionPerformed(ActionEvent event) {
    showStatus("new");
    }
    } class OpenAction extends AbstractAction {
    public OpenAction() {
    super("Open ...", new ImageIcon("open.gif"));
    } public void actionPerformed(ActionEvent event) {
    showStatus("open");
    }
    } class CutAction extends AbstractAction {
    public CutAction() {
    super("Cut", new ImageIcon("cut.gif"));
    } public void actionPerformed(ActionEvent event) {
    showStatus("cut");
    }
    } class CopyAction extends AbstractAction {
    public CopyAction() {
    super("Copy", new ImageIcon("copy.gif"));
    } public void actionPerformed(ActionEvent event) {
    showStatus("copy");
    }
    } class PasteAction extends AbstractAction {
    public PasteAction() {
    super("Paste", new ImageIcon("paste.gif"));
    } public void actionPerformed(ActionEvent event) {
    showStatus("paste");
    }
    } class ExitAction extends AbstractAction {
    public ExitAction() {
    super("Exit");
    putValue(Action.SMALL_ICON, dukeWavingSmall);
    } public void actionPerformed(ActionEvent event) {
    System.exit(0);
    }
    }
    }