JMenuBar只能放在frame最上面?不能放在当中?

解决方案 »

  1.   


    可以你想怎么搞都可以的. 下面的 例子 LayeredPane 使用了 BorderLayout , 你可按自己的需要的换成别的. public class TMenBar extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = -6562446575658301591L;



    static JMenuBar createMenuBar(){


    JMenu a = new JMenu("aa");
    JMenu b = new JMenu("bb");

    a.add(new JMenuItem("1...."));
    a.add(new JMenuItem("2...."));

    b.add(new JMenuItem("1...."));
    b.add(new JMenuItem("2...."));


    JMenuBar bar = new JMenuBar();
    bar.add(a);
    bar.add(b);


    return bar;
    }
    public static void main(String[] args) {

    TMenBar tb = new TMenBar();

    JLayeredPane pane = tb.getRootPane().getLayeredPane();
    pane.setLayout(new BorderLayout());
    pane.add(createMenuBar() , BorderLayout.SOUTH);
    pane.add(createMenuBar() , BorderLayout.CENTER);
    pane.add(createMenuBar() , BorderLayout.EAST);

    tb.setSize(800,400); 
    tb.setLocationRelativeTo(null); 
    tb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    tb.setVisible(true);

    }}
      

  2.   

    可以随便放的,不过如果这样,还不如用JPopMenu或JToolBar更灵活。